题目描述
时间复杂度比较高但是相对容易理解的做法
代码如下
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
int main(){
string s,flag,res;
char f;
cin>>s>>flag;
int q = 0;
if(s.size()>flag.size()){
int n = s.size();
while(n){
f = s[0];
for(int i = 0;i<s.size();i++){
s[i] = s[i+1];
}
s[s.size() - 1] = f;
for(int i = 0;i<s.size();i++){
res += s[i];
if(res == flag){
cout<<"true";
q++;
}
}
res = "";
n--;
}
}else if(flag.size() == s.size()){
if(s==flag){
cout<<"true";
q++;
}else{
int n = flag.size();
while(n){
f = flag[0];
for(int i = 0;i<flag.size();i++){
flag[i] = flag[i+1];
}
flag[flag.size() - 1] = f;
for(int i = 0;i<flag.size();i++){
res += flag[i];
if(res == s){
cout<<"true";
q++;
}
}
res = "";
n--;
}
}
}else{
int n = flag.size();
while(n){
f = flag[0];
for(int i = 0;i<flag.size();i++){
flag[i] = flag[i+1];
}
flag[flag.size() - 1] = f;
for(int i = 0;i<flag.size();i++){
res += flag[i];
if(res == s){
cout<<"true";
q++;
}
}
res = "";
n--;
}
}
if(q==0){
cout<<"false";
}
return 0;
}