Problem
Test instructions
Give you a string s1, string S2,s1 loop shift, so S2 is included in S1, then S2 is s1 affinity string
Analysis
Copy the S1 itself over the back.
Method One: Use the STRSTR function.
Method two: KMP algorithm.
Method Three: Use the C + + string's find function.
Code method One:
#include <cstdio> #include <cstring>char s1[200009],s2[100009];int len;int main () {while (~scanf ("%s" , &s1)) { len=strlen (S1); for (int i=0; i<len; i++) s1[len+i]=s1[i]; scanf ("%s", &s2); if (Strstr (S1,S2)-s1>=0) printf ("yes\n"); else printf ("no\n"); memset (s1,0,sizeof (S1)); memset (s2,0,sizeof (S2)); } return 0;}
Method Two:
#include <cstdio> #include <cstring>char s[200005],t[100005];int next[100005],len;void getNext () { int i,k=-1; next[0]=k; for (I=1; t[i];i++) { while (k>=0 && t[i]!=t[k+1]) k=next[k]; if (t[i]==t[k+1]) k++; next[i]=k;} } int KMP () { int i=0,j=0; while (i==-1| | S[j]&&t[i]) { if (i==-1| | S[j]==t[i]) { i++; j + +; } else i=next[i]; } if (! T[i]) return j-i+1; return-1;} int main () { while (~scanf ("%s", s)) { Len=strlen (s); for (int i=len;i<2*len;i++) S[i]=s[i-len]; scanf ("%s", T); GetNext (); if (KMP () <0) printf ("no\n"); else printf ("yes\n"); memset (s,0,sizeof (S)); memset (t,0,sizeof (T)); } return 0;}
Method Three
#include <iostream>using namespace Std;int main () { string p,t; while (cin>>p>>t) { p+=p; if (P.find (T)!=string::npos) cout<< "yes" <<endl; else cout<< "no" <<endl; } return 0;}
"HDU 2203" affinity string