题目描述
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
思路
找到A右面有多少个连续BC整体,AC,BA,BB,CC都会阻断交换。从后往前找。
代码
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
char s[N];
int main()
{
scanf("%s",s);
ll ans=0,pre=0;
int len=strlen(s);
for(int i=len-2;i>=0;i--)
{
if(s[i]=='A')
{
if(s[i+1]=='C')
pre=0;
ans+=pre;
}
else
{
if(s[i]=='B'&&s[i+1]=='C') pre++;
if(s[i]=='B'&&s[i+1]=='B') pre=0;
if(s[i]=='B'&&s[i+1]=='A') pre=0;
if(s[i]=='C'&&s[i+1]=='C') pre=0;
}
}
printf("%lld\n",ans);
return 0;
}