poj3461 Oulipo【KMP】
来源:互联网 发布:mac清理软件破解版 编辑:程序博客网 时间:2024/05/29 06:54
题目链接:
http://poj.org/problem?id=3461
题目大意:
给一个字符串T,表示文章,再给一个字符串W,表示单词。T和W都只包含26个大写英文字母。
现在计算单词W在文章T中出现的次数。W在T中出现的次数必须连续完全匹配,没两次匹配可能
有重叠的部分。
思路:
先求出字符串W的Next[]指针,然后进行匹配,当一次匹配成功后,继续回退到Next[j]向后进行
匹配,直到字符串T的末尾。此时,得到的匹配成功次数为所求,即W在T中出现的次数。
AC代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int MAXN = 1000010;char T[MAXN],W[MAXN];int Next[MAXN],len1,len2;void GetNext(){ int i = 0,j = -1; Next[0] = -1; while(i < len2) { if(j == -1 || W[i] == W[j]) { i++; j++; Next[i] = j; } else j = Next[j]; }}int KMP(){ len1 = strlen(T); len2 = strlen(W); GetNext(); int i = 0,j = 0; int Ans = 0; while(i < len1) { if(j == -1 || T[i] == W[j]) i++,j++; else j = Next[j]; if(j == len2) { Ans++; j = Next[j]; } } return Ans;}int main(){ int N; cin >> N; while(N--) { cin >> W >> T; int ans = KMP(); cout << ans << endl; } return 0;}
0 0
- 【KMP】 poj3461 Oulipo
- 【KMP】Oulipo POJ3461
- poj3461 Oulipo (kmp)
- [POJ3461] Oulipo[KMP基础]
- poj3461 Oulipo,Kmp
- POJ3461 Oulipo 【KMP】
- POJ3461 Oulipo KMP
- poj3461-Oulipo(KMP)
- poj3461 Oulipo【KMP】
- 【KMP】【POJ3461】Oulipo
- POJ3461 Oulipo(kmp)
- poj3461 Oulipo(KMP)
- 【poj3461】Oulipo(KMP)
- poj3461 Oulipo(KMP)
- POJ3461 Oulipo KMP基础
- poj3461 Oulipo(KMP)
- POJ3461 Oulipo KMP裸题
- 【POJ3461】Oulipo(KMP)
- ubuntu开机只能进入grub命令行的解决(WUBI安装)
- 最近做项目遇到的一些小细节
- epoll源码实现分析
- DirectDraw简介
- JavaScript学习方法
- poj3461 Oulipo【KMP】
- Zigbee的串口学习
- Java回调函数
- 如何使用struct_finddata_t结构体
- LeetCode Pascal's Triangle
- netstat命令使用实例
- Ubuntu 14.10(32位)上搭建Hadoop2.5.0单机伪分布式环境
- Android布局中LinearLayout的android:layout_gravity="bottom"不起作用
- 腾讯面试题总结,似乎是面试C++方向的(小巫总结)