550A Two Substrings
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings “AB” and “BA” (the substrings can go in any order).
出现重叠的情况就两种,因此我们只需要存储AB的位置后再遍历一次数组,遇到BA的时候检测是否与AB出现下列这两种重叠情况。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5;
int main(){
string s;
cin>>s;
vector<int>v;
for(int i=1;i<s.length();i++){
if(s[i-1]=='A' && s[i]=='B'){
v.push_back(i);
}
}
for(int i=0;i<(int)s.length()-1;i++){
if(s[i]=='B' && s[i+1]=='A'){
for(int j:v){
if(j!=i && (i+1 != (j-1))){
cout<<"YES";
return 0;
}
}
}
}
cout<<"NO";
}