gdufe acm 1136 查找ACM(加强版)

Problem Description:

 作为一个acmer,应该具备团队合作能力和分析问题能力。给你一个只有a,c和m的字符串,你要依次取3个字母使之恰好为acm。 

比如串
accmmmca 你可以取
12345678
ac_m____
ac__m___
ac___m__
a_cm____
a_c_m___
a_c__m__共6种。

你只要给出给你的串有多少种方案能组成acm。
Input:

输入包含多组测试,对于每组测试,输入一个只有acm3种字母的串(长度<=200000)

Output:

对于每组测试,输出一个整数一行,表示给你的串有多少种方案能组成acm。
Sample Input:

accmmmca
Sample Output:

6


#include <stdio.h>
#include <string.h>
const int maxn=200005;
char s[maxn];
int cnt_a[maxn];
//cnt_a[i]是第i+1'c''a'的个数
int main(){
    //freopen("data.txt","r",stdin);
    //freopen("WA.txt","w",stdout);
    while(scanf("%s",s)!=EOF){
        int len=strlen(s);
        int a=0,cnt=0;
        for(int i=0;i<len;i++){
            if(s[i]=='a')
                a++;
            else if(s[i]=='c'){
                cnt_a[cnt++]=a;
            }
        }//算出每个'c''a'的个数
        int m=0;
        long long ans=0;
        for(int i=len-1;i>=0;i--){
            if(s[i]=='m')
                m++;
            else if(s[i]=='c'){
                ans+=1LL*cnt_a[--cnt]*m;
                //这里可能溢出,所以要转化为long long
            }
        }//算出每个'c''m'的个数,与在该'c'前的'a'的个数相乘,累加
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值