HDU1686 Oulipo 题解 KMP算法
题目链接: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算法的更多相关文章
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 3461 - Oulipo 经典kmp算法问题
2017-08-13 19:31:47 writer:pprp 对kmp算法有了大概的了解以后,虽然还不够深入,但是已经可以写出来代码,(可以说是背会了) 所以这道题就作为一个模板,为大家使用吧. 题 ...
- hdu 1696 Oulipo(KMP算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题意 查询字符串 $p$ 在字符串 $s$ 中出现了多少次,可重叠. 题解 KMP模板题. Ti ...
- POJ 3461 Oulipo(——KMP算法)
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- [hdu1686] Oulipo【KMP】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1686 保存KMP模版,代码里P是模版串,next[]就是为它建立的.T是文本串,就是一般比较长的.nex ...
- POJ3080 Blue Jeans 题解 KMP算法
题目链接:http://poj.org/problem?id=3080 题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3). 题目分析:KMP字符串匹配 ...
- POJ2185 Milking Grid 题解 KMP算法
题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...
- HDU1711 Number Sequence 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...
- HDU3336 Count the string 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...
随机推荐
- 对List<Map<String, Object>>集合排序
private void mySort(List<Map<String, Object>> list) { //list为待排序的集合,按SEQ字段排序 Comparator& ...
- 二维码识别项目zxing横屏改为竖屏
第1步: 在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改: android:screenOrientation="por ...
- Leetcode908.Smallest Range I最小差值1
给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中. 在此过程之后,我们得到一些数组 B. 返回 B 的最大值 ...
- JQuery-- 实例:小米左右切图,淡入淡出,自动,小圆点触发轮播图
示意图: demo <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- LintCode_408 二进制求和
给定两个二进制字符串,返回他们的和(用二进制表示). 思路 string s = ""; 目标字符串 cp 存储进位;取 0或1 sum = a[i] + b[i] + cp;分为 ...
- C++程序设计教材目录思维导图(增C++Primer)
正在做C++思维导图,大工程,比较艰苦. 先做了三个C++教材目录的思维导图.C++教材不等于C++,这个容易些.看思维导图,整理所学知识,这个可以会. 给出三张图,对应三种教材: 谭浩强. C++程 ...
- Directx11教程(59) tessellation学习(1)
原文:Directx11教程(59) tessellation学习(1) 在D3D11管线中,新增加了3个stage, Hull shader, Tessellator, Domain s ...
- Directx11教程39 纹理映射(9)
原文:Directx11教程39 纹理映射(9) 在myTutorialD3D11_32中,我们在PlaneModelClass中增加一个纹理TextureClass* m_Texture;读 ...
- Kernal Panic - Not syncing : VFS: unable to mount root fs on unknown-block
升级了一下centos6.5 执行了 yum -y update reboot 出现了以下问题: Kernal Panic - Not syncing : VFS: unable to mount r ...
- php 正则学习取反符号~
php 正则学习取反符号~ ~(<a .*?>.*?</a>|<.*?>)~i 先看正则图形,有点偏差,但可以初步看出结果. 关于 ~ 是取反符号,看下面说明.