题目描述
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.
题意
在一个只包含A、B、C的字符串,有一种操作,可使 “ABC” 变成 ”BCA“,求字符串s的最多操作数。
1≤∣s∣≤200000
思路
易得,该操作是将A与BC交换位置,可用 1、0分别代表“A”、“BC”。题意转化对一个只包含10的序列,将所有的10更新01,即将所有的0放在1前面。假设序列***有k个0,每个0前面有ai个1,则
对于单独B、C,则可看作是两个序列分隔的标志。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
char s[N];
int main(){
scanf("%s",s);
int len=strlen(s);
ll ans=0;
ll cnt=0;
for(int i=0;i<len;){
if(s[i]=='A'){
cnt++;
i++;
}
else if(i+1<len&&s[i]=='B'&&s[i+1]=='C'){
ans+=cnt;
i+=2;
}
else{
cnt=0;
i++;
}
}
printf("%lld\n",ans);
return 0;
}