2021-7-25 Two Substrings
2021/7/25 23:39:21
本文主要是介绍2021-7-25 Two Substrings,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
难度 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 }
这篇关于2021-7-25 Two Substrings的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性
- 2024-05-29哪些无用敏捷指标正在破坏敏捷转型?
- 2024-05-29鸿蒙原生应用再新丁!新华社 入局鸿蒙
- 2024-05-29设计模式 之 迭代器模式(Iterator)