POJ 3461 Oulipo(模式串在主串中出现的次数)
2015-04-27 15:51
393 查看
题目链接:http://poj.org/problem?id=3461
题意:给你两个字符串word和text,求出word在text中出现的次数
思路:kmp算法的简单应用,遍历一遍text字符串即可,当前匹配的字符数达到word字符串的长度,即可确定word字符串出现一次了。
code:
题意:给你两个字符串word和text,求出word在text中出现的次数
思路:kmp算法的简单应用,遍历一遍text字符串即可,当前匹配的字符数达到word字符串的长度,即可确定word字符串出现一次了。
code:
#include <cstdio> #include <cstring> using namespace std; const int MAXM = 10005; const int MAXN = 1000005; char word[MAXM]; char text[MAXN]; int next[MAXM]; void GetNext() { int len = strlen(word); int i = 0; int j = -1; next[0] = -1; while (i < len) { if (-1 == j || word[i] == word[j]) next[++i] = ++j; else j = next[j]; } } int Kmp() { int i = 0; int j = 0; int ret = 0; int tlen = strlen(text); int wlen = strlen(word); while (i < tlen) { if (-1 == j || text[i] == word[j]) { ++i; ++j; if (j == wlen) { ++ret; j = next[j]; // 已经匹配成功,重新定位j指向word中的位置 } } else j = next[j]; // 失配时,重新定位j指向word中的位置 } return ret; } int main() { int nCase; scanf("%d", &nCase); while (nCase--) { scanf("%s %s", word, text); GetNext(); printf("%d\n", Kmp()); } return 0; }
相关文章推荐
- [KMP求模式在主串出现次数]POJ 3461 Oulipo
- POJ 3461 Oulipo (求模式串在文本串中出现的次数)
- (串的模式匹配4.6.2)POJ 3461 Oulipo(KMP算法的应用——求一个单词在一行文本中的出现次数)
- POJ 3461 Oulipo(KMP统计子串出现次数)
- POJ 3461 Oulipo(KMP 模板 一个串在另一个串出现的次数(可重叠|不可))
- POJ 3461 Oulipo(KMP统计子串出现次数)
- POJ 3461 Oulipo (KMP字符串匹配·统计p在s中出现次数)
- poj 3461 Oulipo(kmp统计子串出现次数)
- POJ 3461 Oulipo(KMP:统计一个串出现的次数)
- poj3461—Oulipo(查找子串在目标串中出现次数)
- POJ 3461 Oulipo (KMP,求模版串在文本串中可覆盖出现的次数,constructive)
- POJ 3461 Oulipo【KMP】【模板题】(找出第一个字符串在第二个字符串中出现次数)
- POJ 3461 Oulipo【KMP,子串出现次数,可重叠】
- poj 3461 (模式串T在主串S中出现的次数)
- POJ 3461 KMP 模板题 字串出现次数
- (KMP 1.2)hdu 1686 Oulipo(计算模式串在文本串中出现的次数)
- 字符串模式匹配:POJ 3461 Oulipo
- (字符串的模式匹配4.7.18)POJ 1961 Period(求一个串到第i个字符循环节出现的次数)
- HDU 1686 Oulipo (KMP——统计模式串出现次数)
- POJ 3461 Oulipo(自己YY的模式匹配算法)