POJ
来源:互联网 发布:怎么用ipad看淘宝直播 编辑:程序博客网 时间:2024/06/15 07:31
求模式串在字符串中出现的次数。
统计次数时,要注意当完成一组匹配之后令j = next【j】,以便开启另一组匹配
#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int maxn = 1000000 + 10;int next[maxn], leno, lenp;char o[maxn], p[maxn];void NEXT() { int i = 0, j = -1; next[0] = -1; while (i < lenp) { if (j == -1 || p[i] == p[j]) { i++, j++; if (p[i] != p[j]) next[i] = j; else next[i] = next[j]; } else j = next[j]; }}void KMP() { int i = 0, j = 0, cnt = 0; while (i < leno && j < lenp) { if (j == -1 || o[i] == p[j]) { i++, j++; } else j = next[j]; if (j == lenp) { j = next[j], cnt++; } } printf("%d\n", cnt);}int main() { int T; scanf("%d", &T); while (T--) { scanf("%s%s", p, o); leno = strlen(o), lenp = strlen(p); NEXT(); KMP(); } return 0;}
阅读全文
1 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Ubuntu下安装中文版man手册
- MachingLearning中的距离和相似性计算以及python实现
- JZOJ__Day 10:【普及模拟】【USACO】iCow播放器
- PMP 第5章 项目范围管理
- MATLAB 之 \(左除) /(右除)
- POJ
- java异常处理机制简要概述
- 【Java学习之路】for、while、do-while的使用
- 冒泡排序
- jQuery操作form表单
- 17暑假预热赛--SDIBTVJ1793
- LeetCode
- jQuery Ajax
- Excel基础函数操作