Problem 1989 AntiAC
Accept: 93 Submit: 444
Time Limit: 4000 mSec Memory Limit : 32768 KB
Problem Description
Usually, in programming contests all you wait is “AC” (abbreviation of AekdyCoin).We ?nd that boring.
In this task we do the opposite. We will give you a string consists of only uppercase letters. You should remove some letters so that there is no “AC” in the result string. Return the result string with the longest length. If there is more than one string with the longest length, return lexicographically smallest one.
Input
In the first line there is an integer T, indicates the number of test cases. (T <= 100).In each case, there contains one string. The length of string is no longer than 10000.
Output
For each case, output “Case idx: “ first where idx is the index of the test case start from 1, then output the result string.
Sample Input
3
A
ACA
ACBWCA
Sample Output
Case 1: A
Case 2: AA
Case 3: ABWCA
分析
贪心。把连续的AC块拿出来处理,处理结果必然是多个C+多个A,这样我们需要分别计算A和C的数量(AAACCAAC看作ACAC,记录数量),从而拼接出最长的CA段
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #include<cstring> #include <queue> using namespace std; typedef long long LL; const int maxn = 1e4+5; const int mod = 772002+233; typedef pair<int,int> pii; #define X first #define Y second #define pb push_back #define mp make_pair #define ms(a,b) memset(a,b,sizeof(a)) char s[maxn],ans[maxn]; int sum[maxn],a[maxn],c[maxn]; int main(){ int t; scanf("%d",&t); int cas=1; while(t--){ scanf("%s",s); int tot=0,numA,numC; int len = strlen(s); for(int i=0;i<len;i++){ if(s[i]!=‘A‘){ ans[tot++]=s[i]; continue; } int cnt=0; while(s[i]==‘A‘){ //处理压缩ACAC段 numA=0,numC=0; while(s[i]==‘A‘) numA++,i++; while(s[i]==‘C‘) numC++,i++; sum[++cnt]=numA; //odd sum[++cnt]=numC; //even } c[0]=0; for(int j=2;j<=cnt;j+=2){ c[j]=c[j-2]+sum[j]; } a[cnt+1]=0; for(int j=cnt-1;j>=1;j-=2){ a[j]=a[j+2]+sum[j]; } int tlen=0,clen=0,alen=0; for(int j=0;j<=cnt;j+=2){ if(c[j]+a[j+1]>tlen){ tlen = c[j]+a[j+1]; clen = c[j]; alen = a[j+1]; } } for(int j=0;j<clen;j++){ ans[tot++]=‘C‘; } for(int j=0;j<alen;j++){ ans[tot++]=‘A‘; } ans[tot++]=s[i]; } ans[tot]=0; printf("Case %d: ",cas++); puts(ans); } return 0; }