<题目链接>

题目大意:

输入一个T,表示有T组测试数据;

每组测试数据包括一个字符串W,T,T的长度$\le10^6$,$W$的长度$\le 10^4$,计算W匹配到T中成功的次数;

需要注意的是:

第一次将匹配成功的位置得到后,若从匹配到的位置开始算起,会超时,实际上没必要,每次模式串匹配成功时,都将个数+1,然后从next数组的下一位继续开始匹配,直至待匹配串结束

代码如下:实际上就是将kmp裸题稍微改一下。

若两个不同的匹配没有交集则j=0,若存在交集则j=next[j]

#include <bits/stdc++.h>
using namespace std; const int N1 = 1e6+, N2 = 1e4+;
int nxt[N2];
char str1[N1],str2[N2];
int cnt; //匹配的子串个数 inline void get_nxt(int len2){
int i = , j = -;
nxt[] = -;
while (i<len2){
if (j == - || str2[i] == str2[j])nxt[++i]=++j;
else j = nxt[j];
}
}
inline void kmp(int len1, int len2){
int i = , j = ;
get_nxt(len2);
while (i<len1){
if (j == - || str1[i] == str2[j])++i,++j;
else j = nxt[j];
if (j == len2)++cnt,j=nxt[j]; //找到一个匹配的串后,模式串的起点从nxt数组下一个点继续匹配
}
}
int main(){
int n;scanf("%d", &n);getchar();
int len1,len2;
while (n--){
gets(str2);gets(str1);
len1 = strlen(str1);
len2 = strlen(str2);
cnt = ;
kmp(len1, len2);
printf("%d\n", cnt);
}
}

hdu 1686 Oulipo 【KMP】(计算模式串匹配的次数——与已匹配的字串可以有交集)的更多相关文章

  1. HDU - 1686 Oulipo KMP匹配运用

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

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

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

  3. HDU1686 计算模式串匹配的次数

    题目大意: 输入一个T,表示有T组测试数据: 每组测试数据包括一个字符串W,T,T长度大于W小于1000000,w长度小于10000,计算W匹配到T中成功的次数: 这题很明显要用KMP算法,不然很容易 ...

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

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

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

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

  6. hdu 1686 Oulipo kmp算法

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

  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. HDU 1686 Oulipo kmp裸题

    kmp算法可参考 kmp算法 汇总 #include <bits/stdc++.h> using namespace std; const int maxn=1000000+5; cons ...

随机推荐

  1. js实现获取两个日期之间所有日期最简单的方法

    Date.prototype.format = function() { var s = ''; var mouth = (this.getMonth() + 1)>=10?(this.getM ...

  2. VS Code中Matlab插件安装设置

    Install the extension in VS Code Open the command palette using Ctrl+Shift+P Type ext install Matlab ...

  3. 解决git: 'subtree' is not a git command. See 'git --help'.

    一.第一方法 git clone https://github.com/git/git.git cd git/contrib/subtree sudo make prefix=/usr sudo ma ...

  4. makefile 字符串处理函数

    截取自<跟我一起写Makefile> (1)  $(subst <from>, <to>, <text>) 名称: 字符串替换函数 subst 功能: ...

  5. centos 6.5内核编译步骤及配置详解

    1.准备并加压内核安装包:linux-3.13.2.tar.xz # tar xf linux-3.13.2.tar.xz -C /usr/src # cd /usr/src/ # ln -sv li ...

  6. 转载:编译安装Nginx(1.4)《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19617.html 安装Nginx最简单的方式是,进入nginx-1.0.14目录后执行以下3行命令:./configuremakem ...

  7. 关于ftp上传changeWorkingDirectory()方法的路径切换问题

    在上传时 FTPClient提供了upload方法,对于upload(file,path)的第二个参数path ,上传到哪里的这个路径, ftp是利用changeWorkingDirectory()方 ...

  8. AndroidStudio中builde.gradle文件详解

    Android Studio是采用Gradle来构建项目的,一个Android项目中包含两个build.gradle文件,如下图:     (1)最外层目录下build.gradle文件 最外层目录下 ...

  9. 1. let 和 const 命令

    一.简单认识 1. 用let来声明变量,变量作用域就在{}(块级作用域)中 2. 用const声明变量,变量值不可更改 3. 增加了let以后,在声明变量时应该多考虑一下变量的用途,是否希望只在当前代 ...

  10. MyEclipse2014安装插件的几种方式(适用于Eclipse或MyEclipse其他版本)

    农历 乙未 羊年 十一月初九 周六 2015年12月19日 14:29 编辑者:刘军 标题: 服务器的搭建请参考该文:<Win7 x64 svn 服务器搭建> ============== ...