poj3461KMP算法模板
2012-04-23 10:55
246 查看
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; char a[1000010],b[1001000]; int next[1001000]; int n,m; void GetNext(char ch[],int next[]) { int i=1,j=0; next[1]=0; next[0]=0; while(i<=n) { if(j==0||ch[i]==ch[j]) { ++i;++j;next[i]=j; } else j=next[j]; } } int KMPIndex(char b[],char a[],int next[]) { int i=1,j=1,count=0; if(m<n) return 0; while(i<=m) { if(j==0||a[j]==b[i]) { ++i;++j; } else j=next[j]; if(j==n+1) {count++;j=next[j];} } return count; } int main() { int pos,t; int i,j; scanf("%d",&t);a[0]='#';b[0]='#'; while(t--) { scanf("%s",a+1); n=strlen(a)-1; scanf("%s",b+1); m=strlen(b)-1; memset(next,0,sizeof(next)); GetNext(a,next); pos=KMPIndex(b,a,next); printf("%d\n",pos); } return 0; }
相关文章推荐
- KMP算法模板及问题解决(HDU 1711)(hihocoder 1015 KMP)(HDU 1686)(POJ3461)(HDU1358)
- ACM模板——KMP算法
- KMP算法模板
- POJ3461 Oulipo ——KMP算法——Pku3461
- KMP算法模板
- poj3461 Oulipo (KMP模板题~) 前面哪些也是模板题 O.O
- KMP算法(模板) 4000
- KMP算法(——模板习题与总结)
- HDU1711-Number Sequence-KMP算法(模板)
- POJ3461 Oulipo KMP算法应用
- 模板系列 - 字符串处理 -KMP算法
- KMP算法(各种模板)
- hihoCoder#1015 : KMP算法 (KMP模板)
- Luogu 3375 【模板】KMP字符串匹配(KMP算法)
- KMP算法的几种模板
- 【KMP模板】POJ3461-Oulipo
- KMP算法的next、next value数组代码实现及POJ3461
- KMP算法 模板
- POJ3461 KMP算法
- KMP算法经典模板