poj 3461
来源:互联网 发布:炒股心态 知乎 编辑:程序博客网 时间:2024/05/16 02:25
kmp的模板题,要说一下的就是求next值的过程就是模式串的自匹配过程。因为这次借这道题对kmp有了比较清晰的了解,所以就写在blog里了。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<cstdlib>#include<cmath>#include<algorithm>#include<queue>#include<stack>#include<set>#include<map>using namespace std;const int N=1005000;int next[N],n;char s[N],t[N];void getnext(){ int i=0,j=-1; memset(next,0,sizeof(next)); next[0]=-1; while(t[i]!='\0'){ if (j==-1 || t[i]==t[j]){ i++;j++; if (t[i]!=t[j]) next[i]=j; // 此时 设next[i-1]=k,则说明模式串前k个与i前面的k个相同,而ti!=tj,所以有next[i]=j, else next[i]=next[j]; // 此时,ti!=tj,而主串与ti比较不相等时,没必要与tj再比较,而直接与next[j] 对应的字符相比,所以next[i]与next[j]是想等的。 } else j=next[j]; }}int main(){ int q; cin>>q; while(q--) { scanf("%s %s",t,s); int i=0,j=0; int l=strlen(s); int r=strlen(t); int k=0; getnext(); while(i<l && j<r){ if (j==-1 || s[i]==t[j]){ i++; j++; } else { j=next[j]; } if (j==r) { k++; j=next[j]; } } cout<<k<<endl; }}
0 0
- poj 3461
- POJ 3461
- poj 3461
- poj 3461
- POJ 3461
- poj 3461
- poj 3461
- POJ 3461
- poj 3461
- poj 3461
- POJ 3461
- POJ 3461
- POJ 3461
- POJ 3461
- poj 3461
- POJ 3461
- POJ 3461
- poj 3461
- java中的super关键字
- APP相关的测试点
- webkit API接口文档
- Struts2源码阅读(三)_Dispatcher&ConfigurationProvider
- Java中的关键字final
- poj 3461
- Struts2源码阅读(四)_Dispatcher&ConfigurationProvider续
- GridView显示不完整--GridView去掉滚动条
- Hduoj1686 【KMP】
- LeetCode 128 Reverse Nodes in k-Group
- Java内存分析
- 符号机制 (上)
- Struts2源码阅读(五)_FilterDispatcher核心控制器
- 沸腾女孩:不怕舆论,坚持最美的自己