KMP(SOJ2652)
来源:互联网 发布:山下智久石原里美 知乎 编辑:程序博客网 时间:2024/06/06 12:25
(2012-07-14 02:44:44)
SOJ2652:http://cstest.scu.edu.cn/soj/problem.action?id=2652
过的第一道KMP题。
SOJ2652:http://cstest.scu.edu.cn/soj/problem.action?id=2652
过的第一道KMP题。
这道题主要是解决输出所有子串的个数而不是像KMP算法里的仅仅输出第一个子串的位置。
代码:
#include<iostream>#include<cstring>using namespace std;int next[10005];char text[1000005];char pattern[10005];int main(){ int test; scanf("%d",&test); while(test--) { scanf("%s%s",pattern,text); strcat(pattern,"0"); int lP=strlen(pattern); //////////////////////////// int j=0; int k=-1; next[0]=-1; while(pattern[j]) { if(k==-1 || pattern[j]==pattern[k]) { ++j; ++k; if(pattern[j]!=pattern[k]) next[j]=k; else next[j]=next[k]; } else k=next[k]; } j=0; int i=0; int result=0; while(text[i]) { if(text[i]==pattern[j]) { i++; j++; } else if(next[j]==-1) { j=0; i++; } else j=next[j]; if(j==lP-1) { result++; j=next[j]; } } printf("%d\n",result); } return 0;}
0 0
- KMP(SOJ2652)
- poj3461 (KMP&&扩展KMP)
- hihocoder-1015 KMP算法(KMP)
- hihoCoder 1015 KMP算法(kmp)
- (LA3026) Period -- KMP again (KMP 模板)
- KMP(1)--hdu2203(简单KMP模板)
- KMP算法(1):如何理解KMP
- poj3461 (裸kmp)kmp模板
- KMP 算法(1):如何理解 KMP
- KMP 算法(1):如何理解 KMP
- HDU4333 Revolving Digits(KMP+扩展KMP)
- HDU4763 Theme Section(KMP+扩展KMP)
- KMP 算法(1):如何理解 KMP
- hdu6153-A Secret (kmp/扩展kmp)
- KMP 算法(1):如何理解 KMP
- kmp算法(转载)
- KMP算法(转)
- KMP实现(二)
- C#treeView控件单击事件选中节点的问题
- CSS BoxModel 官方解读
- 1064. Complete Binary Search Tree
- tar命令备忘
- USACO 1.2.2 Milking Cows
- KMP(SOJ2652)
- Ruby学习笔记_super
- heritrix 下载、安装、配置
- 海盗喝酒
- 我也开博客啦
- Android中常用的几种字体单位
- 贪心(SOJ4124)
- iOS6.0下奇怪的_UIRecordArgumentOfInvocationAtIndex异常
- 1066. Root of AVL Tree