POJ3461——Oulipo KMP算法的应用
来源:互联网 发布:mac 命令行 重启 编辑:程序博客网 时间:2024/06/09 23:28
题目地址:
戳我戳我~
题目大意:
多组数据询问,每组数据2个字符串,问上面字符串在下面的字符串中出现的次数(匹配的次数);
算法分析:
首先 暴力能做但是显然超时,字符串匹配还有一种更方便更快的方法:KMP!
今天刚学的KMP哇好开心啊~…………
这里我们只需要把KMP的return部分的操作改成cnt++记录一下次数,然后return cnt;即可。
代码很短,主要是为了KMP模板。
我的KMP模板借鉴了黄学长(黄神犇)的博客里的KMP模板。
点我戳黄学长
不多说,代码如下:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int size = 1000010;char s1[size],s2[size];int next[size];int l,ll;void getne(char* a){ memset(next,0,sizeof(next)); int j = 0; for(int i = 2;i <= l;i ++) { while(j && a[i] != a[j+1]) j = next[j]; if(a[i] == a[j+1]) j ++; next[i] = j; }}int find(char* a,char* b){ getne(b); int j = 0; int cnt = 0; for(int i = 1;i <= ll;i ++) { while(j && a[i] != b[j+1]) j = next[j]; if(a[i] == b[j+1]) j ++; if(j == l) cnt ++; } return cnt;}int main(){ int t; scanf("%d",&t); getchar(); for(int i = 1;i <= t;i ++) { gets(s2+1); gets(s1+1); l = strlen(s2+1); ll = strlen(s1+1); int ans = find(s1,s2); printf("%d\n",ans); } return 0;}
0 0
- POJ3461——Oulipo KMP算法的应用
- POJ3461 Oulipo KMP算法应用
- poj3461 Oulipo —— KMP
- C++——KMP算法例题——Oulipo(POJ3461)
- HDU1686 POJ3461 Oulipo KMP算法
- poj3461——Oulipo(KMP)
- poj3461——Oulipo(KMP)
- poj3461——Oulipo(KMP)
- poj3461——Oulipo(kmp)
- Oulipo poj3461 简单的KMP
- POJ3461——Oulipo
- 【KMP】 poj3461 Oulipo
- 【KMP】Oulipo POJ3461
- poj3461 Oulipo (kmp)
- [POJ3461] Oulipo[KMP基础]
- poj3461 Oulipo,Kmp
- POJ3461 Oulipo 【KMP】
- POJ3461 Oulipo KMP
- LeetCode Ugly Number
- 文件上传零碎
- Qt 窗体布局
- [c] poj1003 hangover
- iOS开发中的NSDateFormatter日期格式解析总结
- POJ3461——Oulipo KMP算法的应用
- java中变量存储位置
- JS操作cookie保存当前时间为cookie值
- POJ3046--Ant Counting
- poj 3318
- 关于FrameLayout的理解
- UVA - 11248 Frequency Hopping(网络流+割)
- [笔记]软件体系结构(2)--分层
- Introduction to Java Programming编程题9.5<统计字符串中数字的个数>