标签:25 non AB string Substrings 2021 重叠 include BA
难度 1500
题目 Codeforces:
A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytesYou 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).
InputThe only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
OutputPrint "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
Keyword non-overlapping 不重叠的 题目解析 题目大意是给出一个很长的字符串,要求判断里面是否有不重叠的AB和BA 举个例子ABA不行,因为AB和BA的B是重叠的,ABACAB可以,因为第二个AB和第一个BA并不重叠。 所以简单的遍历,找第一个AB后再找第一个BA,如果找不到,那么再重新找第一个BA后找第一个AB,这样就可以避免出现因为先找到一项另一项原本会出现但却因为重叠不成立的情况。 解析完毕,以下是参考代码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 }
标签:25,non,AB,string,Substrings,2021,重叠,include,BA 来源: https://www.cnblogs.com/lovetianyi/p/15059438.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。