HDU-2087-KMP-水题

纯KMP

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cmath> using namespace std; int M,N,T;
char A[],B[];
int f[]; void getFail(char *P)
{
f[] = ;f[] = ;
for(int i=;i<M;i++)
{
int j = f[i];
while(j && P[i] != P[j]) j = f[j];
f[i+] = P[i] == P[j] ? j+ : ;
}
} int find(char *T,char *P)
{
getFail(P);
int j=;
int ans = ;
for(int i=;i<N;i++)
{
while(j && T[i] != P[j]) j = f[j];
if( T[i] == P[j] ) j++;
if(j == M) {i += M-;ans++;}
}
return ans;
} int main()
{ while( == scanf("%s %s",A,B))
{
N = strlen(A);M = strlen(B);
printf("%d\n",find(A,B));
}
}
上一篇:无法推动项目起步?Let's try McDonald’s Theory


下一篇:Unity内置的shader include files