亲和串

来源:互联网 发布:君子知怕的论证过程 编辑:程序博客网 时间:2024/06/06 12:52


http://acm.hdu.edu.cn/showproblem.php?pid=2203

#include<iostream>
#include<cstring>
using namespace std;
char s1[100010],s2[100010],c[200010],next1[100010];
void getnext(char *s2,int len2)
{
 int i=0,j=-1;
 next1[0]=-1;
 while(i<len2)
 {
  if(j==-1 || s2[i]==s2[j])
  {
   i++;
   j++;
   next1[i]=j;
  }
  else
   j=next1[j];
 }
}
int main()
{//freopen("C:\\Users\\John\\Desktop\\hi.txt","r",stdin);
 int i,j,ok;
 while(cin>>s1>>s2)
 {
  memset(c,0,sizeof(c));
  ok=0;
  strcat(c,s1);
  strcat(c,s1);
  int len1=strlen(c);
  int len2=strlen(s2);
  if(len1<len2)
  {
   cout<<"no"<<endl;
   continue;
  }
  getnext(s2,len2);
  for(i=0,j=0;i<len1;)
  {
   if(j==-1 || c[i]==s2[j])
    i++,j++;
   else
    j=next1[j];
   if(j==len2)
   {
    ok=1;
    break;
   }
  }
  if(ok==1)
   cout<<"yes"<<endl;
  else
   cout<<"no\n";
 }
 return 0;
}

0 0
原创粉丝点击