题目链接: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. 地不安装Oracle,plsql远程连接数据库

    由于Oracle的庞大,有时候我们需要在只安装Oracle客户端如plsql.toad等的情况下去连接远程数据库,可是没有安装Oracle就没有一切的配置文件去支持.最后终于发现一个很有效的方法,Or ...

  2. PHP 中的 curl 函数发送 Post 请求应该注意的几点

    public function http_request( $url, $post = '', $timeout = 5 ){ if( empty( $url ) ){return ;}$ch = c ...

  3. CMake学习笔记六-引用cmake文件

    include(${CMAKE_CURRENT_SOURCE_DIR}/../Share/share.cmake)

  4. JavaScript--关于实例对象带不带参数和构造函数带不带参数的关系

    就是一句话: 构造函数创建对象时,也可以带参数,因为可以对对象进行一些属性的初始化,也就是你创建对象时,就带着这些属性,当然你也可以不带参数,后面实例化对象后再进行添加.而且,js函数的参数在定义函数 ...

  5. iOS GCD 使用

    1. dispatch queue的概念 dispatch queue分成以下三种: a)运行在主线程的Main queue,通过dispatch_get_main_queue获取.dispatch_ ...

  6. SpringMvc表单标签库

    HTML密码框 <td><form:label path="password">密码:</form:label></td><t ...

  7. Leaflet地图框架使用手册

    因为要做一个交通仿真项目,需要用到这个地图库,但是查询官方API麻烦,而且这个地图框架的API做的用起来确实太麻烦了..就从网上各种地方查找了一些,方便用, 大多都是复制,,见谅!! L.Map AP ...

  8. bzoj1911 特别行动队

    Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9       斜率优化 推式子 #include< ...

  9. django2.0基础

    一.安装与项目的创建 1.安装 pip install django 2.查看版本 python -m django --version 3.创建项目 django-admin startprojec ...

  10. 洛谷 P3768 简单的数学题 (莫比乌斯反演)

    题意:求$(\sum_{i=1}^{n}\sum_{j=1}^{n}ijgcd(i,j))mod p$(p为质数,n<=1e10) 很显然,推式子. $\sum_{i=1}^{n}\sum_{j ...