CF1238D (思维 模拟)(1900)
题意:只有A和B组成的长度3e5的串,计算有多少好子字串,好字串:字串中每个位置的字母都可以属于字串中的一个回文串。
- BAAABBB 单独一个字母开头的l,l,r中必须存在和ss[l]相同的字母
- AAAB r不能是单独一个字母
- 我是通过队列维护i之后最近的A和B来模拟的
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false) ,cin.tie(0), cout.tie(0);
//#pragma GCC optimize(3,"Ofast","inline")
#define ll long long
const int N = 1e5 + 5;
const int mod = 998244353;
const ll LNF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
int main () {
int n; cin >> n; string ss; cin >> ss; ss = "*" + ss;
ll sum = 0;
queue<int> posB, posA;
for ( int i = 1; i <= n; ++ i ) {
if(ss[i] == 'B') posB.push(i); else posA.push(i);
}
for ( int i = 1; i <= n; ++ i ) {
if(ss[i] == 'A') {
posA.pop();
if(ss[i + 1] == 'B') {
if(posA.empty()) continue;
sum += n - posA.front() + 1;
continue;
}
sum += n - i;
if(!posB.empty()) sum -= 1;
} else {
posB.pop();
if(ss[i + 1] == 'A') {
if(posB.empty()) continue;
sum += n - posB.front() + 1;
continue;
}
sum += n - i;
if(!posA.empty()) sum -= 1;
}
}
cout << sum << endl;
return 0;
}