poj3461
来源:互联网 发布:vulcan软件 编辑:程序博客网 时间:2024/05/29 16:06
题目还没看:看了翻译版的
虽然学习了KMP,但是面对输入字符串,以及如何计算字符串的长度,我还表示头疼,现在终于可以解决啦
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 1000002;const int M = 10002;int P[M], Count = 0;void Next(char *t, int m){ int j = 0; P[1] = 0; for (int i = 2; i <= m; i++) { while (j>0 && t[j + 1] != t[i]) j = P[j]; if (t[i] == t[j + 1]) j++; P[i] = j; }}void KMP(char *t, char *s, int m, int n){ P[0] = 0; int j = 0; for (int i = 1; i <= n; i++) { while (j>0 && s[i] != t[j + 1]) j = P[j]; if (s[i] == t[j + 1]) j++; if (j == m) { Count++; j = P[j]; } }}int main(){ int n, m, k; scanf("%d", &n); char s[N], t[M]; while (n--) { Count = 0;//每次要重新计数,都死在这里 memset(P,0,sizeof(P)); scanf("%s%s", t + 1, s + 1); int m = strlen(t + 1);//输入字符串 int k = strlen(s + 1); Next(t, m); KMP(t, s, m, k); printf("%d\n", Count); } return 0;}
0 0
- poj3461
- poj3461
- poj3461
- poj3461
- POJ3461
- POJ3461
- poj3461
- poj3461
- POJ3461
- POJ3461:Oulipo
- POJ3461 Oulipo
- poj3461 Oulipo
- poj3461(KMP)
- POJ3461 KMP
- KMP+poj3461
- poj3461 KMP
- poj3461 KMP
- poj3461-Oulipo
- 建议52:及时释放资源
- csu 1723
- VS2010搭建SVN中的VisualSVN
- Hadoop学习之二《Hadoop环境伪分布式环境搭建》
- 12款最常用的Java测试工具
- poj3461
- java集合类
- Python文件读写
- 建议53:必要时应将不再使用的对象引用赋值为null
- 恢复计算机崩溃数据的五款最佳Linux发行版
- Java 向下类型转换
- 最大子数组和
- js中offsetTop,scrollTop,clientTop区别
- 短信猫软件的实现(C#)<一>熟悉串口