hdu1686 Oulipo【kmp】
2017-02-23 19:19
399 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686
题意:给你字符串W和T,让你求W在T中出现了多少次,可重叠的计算(见样例)
解析:每次匹配完的时候cnt++,然后让j=nex[j]
题意:给你字符串W和T,让你求W在T中出现了多少次,可重叠的计算(见样例)
解析:每次匹配完的时候cnt++,然后让j=nex[j]
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 1000000+100; char a[maxn],b[maxn]; int nex[maxn]; void getNex(char a[]) { int len = strlen(a); memset(nex,-1,sizeof(-1)); int k = -1,i = 0; while(i<len) { if(k==-1 || a[k]==a[i]) { k++,i++; nex[i] = k; } else k = nex[k]; } } int kmp_cnt(char a[],char b[]) { int len1 = strlen(a),len2 = strlen(b); int i=0,j=0; int cnt = 0; getNex(b); while(i<len1 && j<len2) { if(a[i]==b[j]) { i++; j++; if(j==len2) { cnt++; j =nex[j]; } } else if(j==0) i++; else j = nex[j]; } return cnt; } int main(void) { int t; cin>>t; while(t--) { scanf("%s %s",b,a); int ans = kmp_cnt(a,b); printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU 1686 Oulipo (KMP)
- HDU-1686 Oulipo(kmp模板题)
- hdu-1686-Oulipo(KMP)
- KMP——HDU 1686 Oulipo
- hdu 1686 Oulipo KMP 纯手搓
- Oulipo - HDU 1686 (KMP模板题)
- HDU 1686:Oulipo 【KMP】
- hdu 1686 Oulipo -- (KMP模板题)
- HDU 1686 Oulipo(KMP)
- HDU-1686 Oulipo (kmp)
- POj 3461 Oulipo ,HDU 1686 Oulipo(KMP)
- hdu 1686 Oulipo (KMP模板)
- hdu1686 Oulipo kmp
- hdu 1686 Oulipo (kmp)
- hdu 1686 Oulipo KMP匹配次数统计
- hdu 1686 Oulipo(简单KMP)只不过比赛的时候用了string.一直超时,改成char就一遍AC,纠结。。。
- HDU - 1686 Oulipo(KMP)
- KMP——HDU 1686 Oulipo
- hdu 1686 Oulipo(KMP)
- hdu1686 oulipo 【KMP】