//    hdu 1686 KMP模板

 //    没啥好说的,KMP裸题,这里是MP模板

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std; const int MAX_N = ;
const int MAX_M = ;
char T[MAX_N];
char p[MAX_M];
int f[MAX_M];
int n,m; void getfail(){
f[] = f[] = ;
for (int i=;i<m;i++){
int j = f[i];
while(j && p[i]!=p[j])
j = f[j];
f[i+] = (p[i]==p[j]) ? j + : ;
}
} int cmp(){
int cnt = ;
int j = ;
for (int i=;i<n;i++){
while(j && T[i] != p[j])
j = f[j];
if (T[i] == p[j])
j++;
if (j==m){
cnt++;
}
}
return cnt;
} int KMP(){
getfail();
return cmp();
} void input(){
scanf("%s%s",p,T);
n = strlen(T);
m = strlen(p);
printf("%d\n",KMP());
} int main(){
int t;
//freopen("1.txt","r",stdin);
scanf("%d",&t);
while(t--){
input();
}
}

hdu 1686 KMP模板的更多相关文章

  1. Oulipo HDU 1686 KMP模板

    题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...

  2. HDU 1686 & KMP

    题意: 求模板在匹配串所有子串中出现次数. SOL: 本题与普通kmp有一点不同,因为待匹配串中的模板串可能相互包含. 我们考虑正常的kmp是在怎么做的 i = 1 2 3 4 5 6 7 8 9 … ...

  3. HDU 2087 kmp模板题

    s为主串 t为模板串 求t的nextt 加const #include<stdio.h> #include<string.h> #include<algorithm> ...

  4. HDU 1686 (KMP模式串出现的次数) Oulipo

    题意: 求模式串W在母串T中出现的次数,各个匹配串中允许有重叠的部分. 分析: 一开始想不清楚当一次匹配完成时该怎么办,我还SB地让i回溯到某个位置上去. 后来仔细想想,完全不用,直接让模式串向前滑动 ...

  5. Number Sequence HDU 1711 KMP 模板

    题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...

  6. hdu 1686 KMP算法

    题意: 求子串w在T中出现的次数. kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html #include <iostream> #inc ...

  7. hdu 1686 & poj 2406 & poj 2752 (KMP入门三弹连发)

    首先第一题 戳我穿越;http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意好理解,每组输入一个子串和一个母串,问在母串中有多少个子串? 文明人不要暴力 ...

  8. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  9. HDU 1711 Number Sequence(KMP模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...

随机推荐

  1. ThinkPHP的路由形式

    首先解释一下,这里路由的意思是:系统从URL参数中分析出当前请求的分组.控制器.操作 .另外我的网址根目录是放在article目录下的,Thinkphp主要有下面几种路由形式 1.pathinfo路径 ...

  2. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

    今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" ...

  3. C++温习

    string abc; cin >> abc; cin把 空格,制表符,换行符作为终止符,用于提取一个词.如果在abc串中输入的内容是有空格的,流中的内容会被打乱? getline (ci ...

  4. 百度Ueditor

    最近用到了百度Ueditor,也来写一写百度Ueditor的使用教程: 一.从官网下载百度Ueditor,http://ueditor.baidu.com/website/download.html, ...

  5. CentOS6.5 Openssl版本升级

    CentOS6.5  Openssl 升级: 第一步:在openssl官网(https://www.openssl.org/)下载最新版 Ps:个人使用的是openssl-1.0.1u.tar.gz版 ...

  6. React子组件与父组件传值

    一 子组件向父组件传值 //子组件var Child = React.createClass({ render: function(){ return ( <div> 请输入邮箱:< ...

  7. 关于sql注入

    删除表,先猜表名,可以使用下面的语名: Select * from A where A.a = ‘testdata’; drop table A---’; If a field only allow ...

  8. RedHat下安装OPENCV

    1.解压 unzip opencv-2.4.9.zip 2.进入目录,cmake CMakeLists.txt  生成build文件 3.使用命令 make 编译 4.使用命令 make instal ...

  9. RedHat下编译安装Boost

    1.解压boost_1_54_0.tar.gz 2.进入目录后,运行 ./bootstrap.sh ,会生成一个 bjam 的可执行程序 3.运行 ./bjam release install 进行编 ...

  10. [转载] 2. JebAPI 之 jeb.api.dex

    本文转载自: https://www.zybuluo.com/oro-oro/note/142842 1. jeb.api.dex.Dex 这个类代表正在被JEB处理的DEX文件. 要想更好的了解这个 ...