POJ3461 Oulipo
来源:互联网 发布:黑暗风格数据地图 编辑:程序博客网 时间:2024/06/14 14:53
一.原题链接:http://poj.org/problem?id=3461
二.题目大意:给一个模式串和一个匹配串,问模式串在匹配串中出现几次。
三.解题思路:KMP模板题:注意KMP每次匹配失效的时候的位移(最好举个例子就不会乱)。每次找到一个记得j = Next[j-1],回到前一个的对称前缀。
四.代码:
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int MAX_N = 10100, INF = 0x3f3f3f3f;int Next[MAX_N];char pattern[MAX_N], match[1000100];void getNext(){ int i, k; Next[0] = 0; for(i = 1; pattern[i]; i++){ k = Next[i-1]; while(pattern[i] != pattern[k] && k > 0) k = Next[k-1]; if(pattern[i] == pattern[k]) Next[i] = k+1; else Next[i] = 0; }}int KMP(){ getNext(); int i = 0, j = 0, cnt = 0; while(match[i]){ while(j > 0 && pattern[j] != match[i]) j = Next[j-1]; if(pattern[j] == match[i]) i++, j++; else i++; if(!pattern[j]){ cnt++; j = Next[j-1]; } } return cnt;}int main(){ //freopen("in.txt", "r", stdin); int T; scanf("%d", &T); while(T--){ scanf("%s%s", pattern, match); printf("%d\n", KMP()); } return 0;}
0 0
- POJ3461:Oulipo
- POJ3461 Oulipo
- poj3461 Oulipo
- poj3461-Oulipo
- poj3461 Oulipo
- POJ3461 Oulipo
- POJ3461 Oulipo
- poj3461 Oulipo
- 【POJ3461】Oulipo
- poj3461 Oulipo
- 【POJ3461】Oulipo
- 【KMP】 poj3461 Oulipo
- 【KMP】Oulipo POJ3461
- poj3461 Oulipo (kmp)
- [POJ3461] Oulipo[KMP基础]
- poj3461 Oulipo,Kmp
- POJ3461 Oulipo 【KMP】
- POJ3461 Oulipo KMP
- 封装OKHttp
- Arduino连接超声波传感器测距
- GIT 02 -git rebase -i 用法
- MBR和GPT概要学习
- iOS中宏(define)/常量(const)/变量的理解
- POJ3461 Oulipo
- FAST解析JSON
- 正则表达式
- 【GOF23设计模式】_中介者模式_同事协作类_内部类实现JAVA245
- Finding where unmanaged exceptions came from
- ACM课程总结
- 竖直时间轴之左右交叉布局
- 关于NETLINK的变化在3.9版本以后
- 面向对象6