题目描述
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;
}