//    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. JAVA 多线程和并发学习笔记(一)

    一.进程与线程 1. 进程 当一个程序进入内存运行时,即变成一个进程.进程是处于运行过程中的程序.进程是操作系统进行资源分配和调度的一个独立单位.进程的三个特征: 独立性 独立存在的实体,每个进程都有 ...

  2. javascript立即执行函数 (function(){})()

    看到一段代码: (function(){ var outer = $('#subject'); outer.find('li').on('mouseover', mouseover); })() ( ...

  3. Mac下导出chrome插件

    chrome最强大的功能之一就是插件,有时候需要给小伙伴们共享一些插件,所以需要将自己chrome中的插件打包,在mac下打包插件还是挺费劲的,在此记录. 打开chrome的扩展程序,找到要导出的插件 ...

  4. js按Enter键提交表单

    function exprint(e){ /* var keycode = event.keyCode; if (keycode == "13"){ fm.UserCode.foc ...

  5. spring2.0包说明【转】

    Spring压缩包目录说明 关键字: sring jar 1. Spring压缩包目录说明 aspectj目录下是在Spring框架下使用aspectj的源代码和测试程序文件. Aspectj是jav ...

  6. Selenium+Python+Eclipse网页自动化集成环境配置(附简单的测试程序)

    最近公司在给我们培训,主要是网页自动化测试的,现在的工作每天都是测APP,刚刚入门,不过,当我看了别人写的bug之后,就觉得不会觉得能够发现bug多么多么的厉害了. 前两周的时间一直在搭建自动化测试的 ...

  7. mongo 查找附近点

    db.runCommand({geoNear:"demo", near: { type: "Point" , coordinates: [118.134535, ...

  8. 编程范式 episode 6 实现stack 栈功能_ to do

    //既然在这里开始,那就在这里结束. 实现stack 功能 ____coding _using subfunction to focus on the main aim of current func ...

  9. 删除svn文件的批处理文件

    @echo on color 2f mode con: cols=80 lines=25 @REM @echo 正在清理SVN文件,请稍候...... @rem 循环删除当前目录及子目录下所有的SVN ...

  10. HTML标签CSS默认值研究

    最近写css的时候,发现在div元素里面添加ul元素后发现,ul列表在div里面距离最上方总是有一段空行,当时很奇怪,以为是哪个css出了问题,就把css去掉了,发现问题依旧,然后就查了一下发现htm ...