难度 1500
题目 Codeforces:
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).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
1 #include<iostream> 2 #include<string> 3 #include<string.h> 4 using namespace std; 5 int main() 6 { 7 string s; cin >> s; 8 bool tf1 = 1, tf2 = 1; 9 for (int i = 0; i < s.size()-1; i++) 10 { 11 if (s[i] == ‘A‘ && s[i + 1] == ‘B‘) 12 { 13 if (tf1) 14 for (int j = i + 2; j < s.size(); j++) 15 { 16 if (s[j] == ‘B‘ && s[j + 1] == ‘A‘) 17 { 18 cout << "YES" << endl; 19 return 0; 20 } 21 } 22 if (tf1) tf1 = 0; 23 } 24 else if (s[i] == ‘B‘ && s[i + 1] == ‘A‘) 25 { 26 if (tf2) 27 for (int j = i + 2; j < s.size()-1; j++) 28 { 29 if (s[j] == ‘A‘ && s[j + 1] == ‘B‘) 30 { 31 cout << "YES" << endl; 32 return 0; 33 } 34 } 35 if (tf2)tf2 = 0; 36 } 37 if (!tf1 && !tf2)break; 38 } 39 cout << "NO"; 40 return 0; 41 }
原文:https://www.cnblogs.com/lovetianyi/p/15059438.html