//    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. [翻译]用神经网络做回归(Using Neural Networks With Regression)

    本文英文原文出自这里, 这个博客里面的内容是Java开源, 分布式深度学习项目deeplearning4j的介绍学习文档. 简介: 一般来说, 神经网络常被用来做无监督学习, 分类, 以及回归. 也就 ...

  2. 双击vbs时,默认cscript运行脚本

    Dim obj_shellset obj_shell = createobject("wscript.shell")host = WScript.FullNameIf LCase( ...

  3. Swift 协议

    /// 一般情况下,定义的协议都必须实现 protocol SomeProtocal { func doSomething() } /// 定义一个类,并且遵守协议 class Teacher:Som ...

  4. angular背景图片问题

    如果背景图片是从后台取得的数据,可以按下面的方式使用: ng-style="{'background':'url(http://xxx/{{item.id}})'}" 还需要加上  ...

  5. eclipse与myeclipse恢复已删除的文件和代码

    1.类文件的恢复 选择项目后右键-->选择Restore from Local history-->出现下面的界面: 勾选后按Restore就恢复了,真的很强大很方便:但我没有就此罢手,我 ...

  6. Jvm支持的最大线程数

    摘自 http://blog.csdn.net/xyls12345/article/details/26482387 JVM最大线程数 (2012-07-04 23:20:15) 转载▼ 标签: jv ...

  7. HttpUtility.UrlEncode

  8. C#DataGridView中的常用技巧

    0(最基本的技巧). 获取某列中的某行(某单元格)中的内容  this.currentposition = this.dataGridView1.BindingContext  [this.dataG ...

  9. UVa 445 - Marvelous Mazes

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  10. props

    // 这里是导入的包 import React, { Component } from 'react'; // 导入需要用到的组件 import { AppRegistry, Text, View } ...