POJ 3461 Oulipo(KMP:统计一个串出现的次数)
2014-04-04 12:26
337 查看
POJ 3461 Oulipo(KMP:统计一个串出现的次数)
http://poj.org/problem?id=3461
题意:
给你一个T串和P串,问你P串在T串中出现了多少次.
分析:
直接套用KMP模板即可.
AC代码:
http://poj.org/problem?id=3461
题意:
给你一个T串和P串,问你P串在T串中出现了多少次.
分析:
直接套用KMP模板即可.
AC代码:
#include <iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; char T[1000000+100],P[10000+100]; int f[10000+100]; int cnt;//统计出现的次数 void find(char *T,char *P,int *f) { int n=strlen(T); int m=strlen(P); int j=0; for(int i=0;i<n;i++) { if(j && T[i]!=P[j]) j=f[j]; if(T[i]==P[j]) j++; if(j==m) cnt++; } } void getFail(char *P,int *f) { int m =strlen(P); f[0]=f[1]=0; for(int i=1;i<m;i++) { int j=f[i]; while(j && P[i]!=P[j]) j=f[j]; f[i+1] = P[i]==P[j]?j+1:0; } } int main() { int K; scanf("%d",&K); while(K--) { cnt=0; scanf("%s %s",P,T); getFail(P,f); find(T,P,f); printf("%d\n",cnt); } return 0; }
相关文章推荐
- POJ 3461 Oulipo(KMP统计子串出现次数)
- POJ 3461 Oulipo (KMP字符串匹配·统计p在s中出现次数)
- POJ 3461 Oulipo(KMP统计子串出现次数)
- poj 3461 Oulipo(kmp统计子串出现次数)
- POJ 3461 Oulipo【KMP】【模板题】(找出第一个字符串在第二个字符串中出现次数)
- POJ 3461 Oulipo (KMP,求模版串在文本串中可覆盖出现的次数,constructive)
- [KMP求模式在主串出现次数]POJ 3461 Oulipo
- POJ 3461 Oulipo(KMP 模板 一个串在另一个串出现的次数(可重叠|不可))
- POJ 3461 Oulipo【KMP,子串出现次数,可重叠】
- POJ 3461 Oulipo(KMP求匹配次数)
- POJ 3461 KMP 模板题 字串出现次数
- (串的模式匹配4.6.2)POJ 3461 Oulipo(KMP算法的应用——求一个单词在一行文本中的出现次数)
- poj3461—Oulipo(查找子串在目标串中出现次数)
- HDU 1686 Oulipo (KMP——统计模式串出现次数)
- POJ 3461 Oulipo (求模式串在文本串中出现的次数)
- POJ 3461 Oulipo(模式串在主串中出现的次数)
- POJ-2406 Power Strings(KMP求重复子串出现的最大次数)
- poj 3461 Oulipo(KMP)
- POJ 3461 Oulipo [KMP模板题]
- poj 2945 trie树统计字符串出现次数