您的位置:首页 > 其它

亲和串

2015-07-07 16:01 381 查看

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: