AcWing 776. 字符串移位包含问题
原题链接
简单
作者:
小良QAQ
,
2024-09-11 00:36:04
,
所有人可见
,
阅读 1
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string s1, s2;
cin >> s1 >> s2;
if (s1.size() < s2.size()) swap (s1, s2);
for (int i = 0; i < s1.size(); i ++ )
{
s1 = s1.substr(1) + s1[0];
for (int j = 0; j + s2.size() <= s1.size(); j ++ )
for (int k = 0; k <= s2.size(); k++ )
{
if (s1[j + k] != s2[k])
break;
if (k == s2.size())
{
puts ("true");
return 0;
}
}
}
puts ("false");
return 0;
}
第二种简洁写法(自己写的)
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
if (a.size() < b.size()) swap(a, b);
for (int i = 0; a[i]; i ++ )
{
a = a.substr(1) + a[0];//循环位移操作
for (int j = 0; j + b.size() <= a.size(); j ++ )
{
int k = 0;
while (k < b.size() && a[j + k] == b[k]) k ++ ;
if (k == b.size())
{
cout << "true" << endl;
return 0;
}
}
}
puts("false");
return 0;
}