原题链接
- 题意:求字符串中是否存在一个 (AB) 和 (BA),他们不重叠。
- 题解:直接把 (BAB) 和 (ABA) 记录数量的同时,转化为别的字符,然后统计 (AB) 和 (BA) 然后判断答案。
- 代码:
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 5e5 + 9;
const ll mod = 1e9 + 7;
bool vis[N];
char s[N];
int main() {
int ab_cnt = 0;
int ba_cnt = 0;
int aba_cnt = 0;
cin >> (s + 1);
int n = strlen(s + 1);
for (int i = 1; i <n-1; i ++) {
if (s[i] == 'B' && s[i + 1] == 'A' && s[i + 2] == 'B') {
aba_cnt++;
s[i] = s[i + 1] = s[i+2] = '?';
}
if (s[i] == 'A' && s[i + 1] == 'B' &&s[i + 2] == 'A') {
aba_cnt++;
s[i] = s[i + 1] = s[i + 2] = '?';
}
}
for (int i = 1; i < n; i ++) {
if (s[i] == 'A' && s[ i + 1] == 'B') {
ab_cnt++;
}
if (s[i] == 'B' && s[i + 1] == 'A')ba_cnt++;
}
cout << s << endl;
if ( (ab_cnt >= 1&&ba_cnt >= 1) || (ab_cnt >=1 && aba_cnt >= 1 )|| (ba_cnt >= 1 && aba_cnt >= 1)||(aba_cnt > 1)){
cout << "YES
";
} else cout << "NO
";
}