鉴于两个字符串,寻找一个字符串的频率,另一个字符串出现。

原版的kmp另一个陷阱。以下凝视了,标不是踩着好,有加班一定几率,也有机会错误,根据不同的字符串可以是详细。

变化看起来像一个,kmp速度是非常快的。

#include <stdio.h>
#include <string.h> const int MAX_TXT = 1000001;
const int MAX_WORD = 10001;
int gWlen, gTlen;
char word[MAX_WORD];
int next[MAX_WORD];
char txt[MAX_TXT]; void getNext()
{
memset(next, 0, sizeof(int) * gWlen);
for (int i = 1, j = 0; i < gWlen; )
{
if (word[i] == word[j]) next[i++] = ++j;
else if (j > 0) j = next[j-1];
else i++;
}
} int kmpSearch()
{
int i = 0, j = 0, ans = 0;
for (; gWlen-j <= gTlen-i; )
{
if (word[j] == txt[i])
{
j++; i++;
if (j == gWlen)
{
ans++;
j = next[j-1];
}
//else i++; i++放这里会超时,是一定几率会出现超时。注意了!
}
else if (j > 0) j = next[j-1];
else i++;
}
return ans;
} int main()
{
int T;
scanf("%d", &T);
getchar();
while (T--)
{
gets(word);
gWlen = strlen(word);
gets(txt);
gTlen = strlen(txt); getNext();
printf("%d\n", kmpSearch());
}
return 0;
}

版权声明:笔者靖心脏,景空间地址:http://blog.csdn.net/kenden23/。只有经过作者同意转载。

HDU 1618 Oulipo KMP解决问题的方法的更多相关文章

  1. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  2. HDU 1867 A + B for you again KMP解决问题的方法

    这是一个典型问题KMP申请书. 结果求增加两个字符串.该法的总和是相同的前缀和后缀也是字符串的字符串,您将可以合并本节. 但是,这个问题是不是问题非常明确的含义,因为不是太清楚,外观这两个字符串的顺序 ...

  3. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

  4. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  5. hdu 1686 Oulipo kmp算法

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...

  6. POJ 3450 Corporate Identity KMP解决问题的方法

    这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...

  7. hdu 1686 Oulipo (kmp)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  8. HDU 1686 Oulipo (KMP 可重叠)

    题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...

  9. 洛谷 P3375 【模板】KMP字符串匹配 || HDU 1686 Oulipo || kmp

    HDU-1686 P3375 kmp介绍: http://www.matrix67.com/blog/archives/115 http://www.cnblogs.com/SYCstudio/p/7 ...

随机推荐

  1. 自定义 matplotlib 设置

    Customizing plots with style sheets import matplotlib as mpl 查看配置文件所在的目录:mpl.get_configdir() 1. 自定义 ...

  2. POJ 2014 Flow Layout 模拟

    http://poj.org/problem?id=2014 嘻嘻2014要到啦,于是去做Prob.ID 为2014的题~~~~祝大家新年快乐~~ 题目大意: 给你一个最大宽度的矩形,要求把小矩形排放 ...

  3. HDU 1248 寒冰王座 完全背包

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1248 中文题,大意就不说了. 第一道完全背包题,跟着背包九讲做的. 和0-1背包的区别在于所不同的是每种 ...

  4. VC6.0 MFC中WebBrowser控件禁止新窗口弹出的解决办法

    http://blog.csdn.net/gnorth/article/details/7258293 分类: WebBrowser MFC 禁止新窗口2012-02-14 15:25 1787人阅读 ...

  5. UI组件之TextView及其子类(三)ToggleButton和Switch

    ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的使用方法都非常类似. C ...

  6. 关于DP与背包

    听说过动态规划(DP)的同学应该都知道有背包问题的存在. 首先我们来了解一下动态规划 基本思想: 动态规划算法通常用于求解具有某种最优性质的问题.在这类问题中, 可能会有很多可行解.没一个解都对应于一 ...

  7. html页面保存数的两种方式

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78866755 需求:微信开发时,在某个页面授权获取用户的openid,但是每次刷新页面 ...

  8. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. PHP移动互联网开发笔记(7)——MySQL数据库基础回顾[1]

    一.数据类型 1.整型 数据类型 存储空间 说明 取值范围 TINYINT 1字节 非常小的整数 带符号值:-128~127无符号值:0~255 SMALLINT 2字节 较小的整数 带符号值:-32 ...

  10. spring boot + gradle + mybatis

    使用intelliJ创建 spring boot + gradle + mybatis站点   Spring boot作为快速入门是不错的选择,现在似乎没有看到大家写过spring boot + gr ...