POJ 3461 Oulipo (KMP模板题)
2015-02-10 15:39
330 查看
求模式串在给定文本串中出现的次数。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cstdlib> #include<cmath> #include<vector> #include<queue> #include<map> #include<ctime> #define eps (1e-8) using namespace std; typedef long long ll; const int maxn = 1000010; const int maxm = 10010; int f[maxm],m,ans,n; char P[maxm],T[maxn]; void getnext() { f[0] = 0,f[1] = 0; for(int i=1;i<m;i++){ int j = f[i]; while(j && P[j]!=P[i]) j=f[j]; f[i+1] = (P[j]==P[i])?j+1:0; } } void KMP() { n = strlen(T); m = strlen(P); getnext(); int j = 0; for(int i=0;i<n;i++){ while(j && P[j]!=T[i]) j=f[j]; if(P[j]==T[i]) j++; if(j==m) ans++; } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%s%s",P,T); ans = 0; KMP(); printf("%d\n",ans); } return 0; }
相关文章推荐
- poj 3461 Oulipo(KMP模板题)
- POJ 3461 Oulipo【KMP】【模板题】(找出第一个字符串在第二个字符串中出现次数)
- POJ 3461 Oulipo(KMP模板)
- 【哈希&KMP模板题】-POJ-3461-Oulipo
- poj 3461 Oulipo(KMP,喜闻乐见纯模板)
- POJ 3461 Oulipo [KMP模板题]
- POJ 3461 Oulipo(KMP模板)
- POJ 3461 Oulipo(KMP模板题)
- POJ - 3461 Oulipo(KMP模板题)
- POJ 3461 Oulipo KMP模板题
- poj 3461 Oulipo (KMP 模板题)
- poj 3461 Oulipo(KMP模板题)
- KMP模板题 poj 3461 Oulipo
- POJ - 3461 Oulipo(KMP模板题)
- [POJ 3461] Oulipo & KMP模板
- POJ 3461 Oulipo (kmp模板)
- POJ 3461 Oulipo(KMP 模板 一个串在另一个串出现的次数(可重叠|不可))
- poj 3461 Oulipo(kmp 模板题)
- poj 3461 Oulipo(KMP模板题)
- POJ:3461-Oulipo(KMP模板题)