码迷,mamicode.com
首页 > 其他好文 > 详细

FZU - 1989 AntiAC

时间:2018-03-10 14:16:55      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:ext   ram   limit   queue   width   +=   air   max   tar   

  技术分享图片 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;
}

 

FZU - 1989 AntiAC

标签:ext   ram   limit   queue   width   +=   air   max   tar   

原文地址:https://www.cnblogs.com/fht-litost/p/8538466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!