AcWing 4203. 寻找子串
题目描述
给定一个长度为 n 的 01 字符串。
请你判断,其中是否存在子串 1111111 或 0000000。
输入格式
一行,一个 01 字符串。
输出格式
如果存在子串 1111111 或 0000000,则输出 YES,否则输出 NO。
数据范围
所有测试点满足 1≤n≤100。
输入样例1:
001001
输出样例1:
NO
输入样例2:
1000000001
输出样例2:
YES
解题思路
str.find(str1);//在str中查找子串str1,并返回开始的索引值,没有返回-1
代码
#include<iostream>
using namespace std;
int main() {
string str;
cin >> str;
string s1 = "1111111";
string s2 = "0000000";
if (str.find(s1) != -1 || str.find(s2) != -1) {
cout << "YES";
}
else {
cout << "NO";
}
return 0;
}
说明
不可用str.find(s1)>=0判断
find()未找到子串时实际返回特殊值string::npos
string::npos是字符串可储存的最大字符数,通常是无符号int或无符号long的最大取值,如,4294967295
其定义为static const size_type npos = -1;