题目描述
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.
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
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.
参考:SZG大佬的题解
题意
在一个只包含A、B、C的字符串,有一种操作,可使 “ABC” 变成 ”BCA“,求字符串s的最多操作数。
1≤∣s∣≤200000
思路
易得,该操作是将A与BC交换位置,可用 1、0分别代表“A”、“BC”。题意转化对一个只包含10的序列,
将所有的10更新01,即将所有的0放在1前面。假设序列中共有kk个0,每个0前面有ai个1,则ans=∑ai (1,k)
对于单独B、C,则可看作是两个序列分隔的标志。
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 2e5+100; 4 typedef long long ll; 5 char s[N]; 6 int main() 7 { 8 scanf("%s",s); 9 ll cnt = 0 ; 10 ll ans = 0 , i = 0 ; 11 int len = strlen(s); 12 while ( s[i] ) { 13 if( s[i] == 'A' ){ 14 //printf("#1 %d " ,i); 15 cnt ++ ; 16 i++ ; 17 }else if ( s[i] == 'B' && s[i+1] == 'C' ){ 18 //printf("#2 %d " ,i); 19 ans = ans + cnt ; 20 i+=2 ; 21 if( i >= len ) break ; 22 }else if ( s[i] == 'B' || s[i] == 'C' || s[i] == '