用str.substr()进行移位操作
str.substr(pos, len)
pos是起始位置的索引,从这里开始提取子字符串。
len是可选参数,指定要提取的子字符串的长度。如果不提供这个参数,子字符串将包含从pos开始到原字符串末尾的所有字符。
示例
假设我们有一个字符串:
std::string str = "Hello, world!";
调用str.substr(1)将返回从索引1开始到字符串末尾的所有字符,即:
"ello, world!"
这是因为它从’e’开始提取,’e’是在索引1的位置。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a,b;
cin >> a >>b;
if(a.size() < b.size()) swap(a,b); //交换字符串
for(int i = 0; i < a.size(); i ++ ) //该循环移位得到字串a'
{
a = a.substr(1) + a[0]; //移除字符串的第一个字符,再将第一个字符移到末尾
for(int j = 0; j + b.size() <= a.size(); j ++ ) //枚举字串a'的起点
{
int k = 0;
for(; k < b.size(); k ++ ) //枚举对应位置
{
if(a[j + k] != b[k])
break;
}
if(k == b.size())
{
puts("true");
return 0;
}
}
}
puts("false");
return 0;
}