//给定两个字符串s1和s2,要求判定s2是否能够被s1做循环移位得到的字符串包含。
/*#include<iostream>
#include<string.h>
using namespace std;
void main()
{ char src[] ="AABBCD";
char des[] ="CDAA";
int len=strlen(src);
for(int i=0;i<len;i++)
{
char tempchar=src[0];
for(int j=0;j<len-1;j++)
src[j]=src[j+1];
src[len-1]=tempchar;
if(strstr(src,des)==0)
//原型:extern char *strstr(char *haystack,char *needle);用法:#include<strng.h>
{ cout<<"包含"<<endl;
return ;
}
}
// return false;
cout<<"不包含"<<endl;
}
*/
/*可以看出对s1做循环移位所得到的字符串都将是字符串是s1s1的子字符串,
如果s2可以由s1循环移位得到,那么s2一定在s1s1上,可通过调用一次strstr函数得到结果
“提高空间复杂度来换取时间复杂度的降低”*/
#include<iostream>
#include<string.h>
using namespace std;
bool str(char a[],char b[])
{
int x=strlen(b);
int y=strlen(a);
for(int i=0;i<y;i++)
{ int k=i;
for(int j=0;j<x;j++)
{ if(a[k]!=b[j])
{
break;
}
else
{
k++;
}
if (j==x-1)
return true;
}
}
return false;
}
void main()
{ char src[] ="AABBCD";
char des[] ="CDAA";
int len=strlen(src);
for(int i=0;i<len;i++)
{
char tempchar=src[0];
for(int j=0;j<len-1;j++)
src[j]=src[j+1];
src[len-1]=tempchar;
if(str(src,des)==true)
//原型:extern char *strstr(char *haystack,char *needle);用法:#include<strng.h>
{ cout<<"包含"<<endl;
return ;
}
}
// return false;
cout<<"不包含"<<endl;
}