FZU - 1989 AntiAC

, 本文介绍了一种算法,用于从给定的字符串中移除所有出现的AC子串,确保结果字符串中最长且字典序最小。通过将连续的AC块进行特殊处理并计算A和C的数量来实现。
   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 find 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;
}

 

转载于:https://www.cnblogs.com/fht-litost/p/8538466.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值