题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686

题目大意:给你一个子串t和一个母串s,求s中有多少个子串t。

题目分析:KMP模板题。

  • cal_next() :计算字符串t的nxt函数;
  • find_s_has_t_count() :计算s中包含多少个t。

实现代码如下:

#include <cstdio>
#include <string>
using namespace std;
const int maxn = 1001000; int T, n, m, nxt[maxn], ans;
string s, t; // s代表母串,t代表子串
char ch[maxn]; string read() {
scanf("%s", ch);
string tmp_s = ch;
return tmp_s;
} void cal_next() {
m = t.length();
for (int i = 0, j = -1; i < m; i ++) {
while (j != -1 && t[j+1] != t[i]) j = nxt[j];
nxt[i] = (j+1 < i && t[j+1] == t[i]) ? ++j : -1;
}
} void find_s_has_t_count() {
ans = 0, n = s.length(), cal_next();
for (int i = 0, j = -1; i < n; i ++) {
while (j != -1 && t[j+1] != s[i]) j = nxt[j];
if (t[j+1] == s[i]) {
j ++;
if (j >= m-1) {
ans ++;
j = nxt[j];
}
}
}
printf("%d\n", ans);
} int main() {
scanf("%d", &T);
while (T --) {
t = read();
s = read();
find_s_has_t_count();
}
return 0;
}

HDU1686 Oulipo 题解 KMP算法的更多相关文章

  1. POJ 3461 Oulipo[附KMP算法详细流程讲解]

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  2. poj 3461 - Oulipo 经典kmp算法问题

    2017-08-13 19:31:47 writer:pprp 对kmp算法有了大概的了解以后,虽然还不够深入,但是已经可以写出来代码,(可以说是背会了) 所以这道题就作为一个模板,为大家使用吧. 题 ...

  3. hdu 1696 Oulipo(KMP算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题意 查询字符串 $p$ 在字符串 $s$ 中出现了多少次,可重叠. 题解 KMP模板题. Ti ...

  4. POJ 3461 Oulipo(——KMP算法)

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

  5. [hdu1686] Oulipo【KMP】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1686 保存KMP模版,代码里P是模版串,next[]就是为它建立的.T是文本串,就是一般比较长的.nex ...

  6. POJ3080 Blue Jeans 题解 KMP算法

    题目链接:http://poj.org/problem?id=3080 题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3). 题目分析:KMP字符串匹配 ...

  7. POJ2185 Milking Grid 题解 KMP算法

    题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...

  8. HDU1711 Number Sequence 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...

  9. HDU3336 Count the string 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...

随机推荐

  1. vue使用填坑之:model和v-model的区别

    v-model通常用于input的双向数据绑定 <input v-model="parentMsg">,也可以实现子组件到父组件数据的双向数据绑定:首先说说v-mode ...

  2. webpack学习之——Entry Points(入口起点)

    1.Entry property(entry属性) 1.1 Single Entry (Shorthand) Syntax(单个入口语法) 用法:entry: string | Array<st ...

  3. Spring_事务

    事务管理: 用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当做一个单独的工作单元.这些动作要么全部完成,要么全部不起作用 事务的四个关键属性 原子性 一致性 隔离性 持久性 Spring两 ...

  4. arcgis访问百度地图

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. phpcms 允许英文目录有空格

    大家都用过phpcm添加栏目吧,在添加栏目里面,有个选项是 英文目录,这里目录可以用作伪静态功能.这么英文不能有空格等特殊字符.但是如果页面中需要引用包含空格的字符呢,例如,关于我们页面,我要显示英文 ...

  6. LDAP Authentication Handler

    Including the Handler In the pom.xml file for your CAS Maven2 WAR Overlay, add the following depende ...

  7. 【JZOJ4815】【NOIP2016提高A组五校联考4】ksum

    题目描述 输入 输出 样例输入 3 4 1 3 4 样例输出 8 7 4 4 数据范围 样例解释 解法 二分做法 考虑到可以二分第k大的值mid,如果比mid大的区间和数小于或等于mid,那么mid就 ...

  8. homework-//2017-12-27 11:11 星期三

    //2017-12-27 11:11 星期三 const WEEKMAP = { 0:"sunday", 1:"monday", 2:"tuesday ...

  9. dba和表的备份与恢复

    每个oracle数据库应该至少有一名数据库管理员(dba),对于一个小的数据库,一个dba就够了,但是对于一个大的数据库可能需要多个dba分别担负不同的管理职责.那么一个数据库管理员的主要工作是什么呢 ...

  10. 「WC2018」即时战略

    「WC2018」即时战略 考虑对于一条链:直接随便找点,然后不断问即可. 对于一个二叉树,树高logn,直接随便找点,然后不断问即可. 正解: 先随便找到一个点,问出到1的路径 然后找别的点,考虑问出 ...