标签:c++
abcde a3 aaaaaa aa #
0 3
#include<iostream> #include<cstdio> #include<cstring> using namespace std; char s1[1005],s2[1005]; int next[1005]; void getnext(char *s2) { int i=0,j=-1; next[0]=-1; while(i<strlen(s2)) { if(j==-1||s2[i]==s2[j]) { i++; j++; if(s2[i]!=s2[j]) next[i]=j; else next[i]=next[j]; } else j=next[j]; } } int main() { int s,t1,t2; while(scanf("%s",s1)) { s=0; if(s1[0]=='#'&&s1[1]=='\0') break; scanf("%s",s2); t1=strlen(s1); t2=strlen(s2); getnext(s2); if(t2>t1) { cout<<0<<endl; continue; } int i=0,j=0; while(i<t1) { if(j==-1||s1[i]==s2[j]) { i++; j++; } else j=next[j]; if(j==t2) { j=0; s++; } } cout<<s<<endl; } return 0; }
标签:c++
原文地址:http://blog.csdn.net/axuan_k/article/details/37963033