题目链接:http://icpc.upc.edu.cn/contest.php?cid=1786
题目描述
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 BB BA CC 都会阻断交换,BC则+1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200100;
char s[N];
int n;
int main() {
while(~scanf("%s", s)) {
n = strlen(s);
ll ans = 0, pre = 0;
for(int i = n - 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;
}