ABC

题目描述
You are given a string s consisting of A, B and C.
Snuke wants to perform the following operation on s as many times as possible:
Choose a contiguous substring of s that reads ABC and replace it with BCA.
Find the maximum possible number of operations.
Constraints
1≤|s|≤200000
Each character of s is A, B and C.

输入
Input is given from Standard Input in the following format:
S

输出
Find the maximum possible number of operations.

样例输入
ABCABC

样例输出
3

提示
You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA. This is the maximum result.

思路
将‘BC’看作是一个整体,每次操作会使‘A’与’BC‘的位置相互换,因此只需要计算’A’后面相邻的‘BC’的个数即可

代码实现

#include<bits/stdc++.h>
using namespace std;

const int N=200005;
const int mod=1000000007;
typedef long long ll;


char s[N];

int main()
{
    scanf("%s",s+1);
    int len=strlen(s+1);
    ll tmp=0,ans=0;
    for(int i=len; i>0; i--)
    {
        if(s[i-1]=='B' && s[i]=='C')
        {
            tmp++;
            i--;
        }
        else if(s[i]=='A') ans+=tmp;
        else tmp=0;
    }
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值