题目链接:https://vjudge.net/problem/POJ-3461

题意:给出主串和模式串,求出模式串在主串中出现的次数。

思路:kmp板子题。复杂度O(n+m)。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; const int maxn=1e6+;
int T,next[maxn],len1,len2;
char s1[maxn],s2[maxn];
//得到next数组
void get_next(){
next[]=-;
int j=-;
for(int i=;i<len2;++i){ //注意这里i一定是从1开始
while(j>-&&s2[j+]!=s2[i]) j=next[j];
if(s2[j+]==s2[i]) ++j;
next[i]=j;
}
}
//输出模式串在主串上依次出现的下标
void kmp_index(){
int j=-;
for(int i=;i<len1;++i){
while(j>-&&s1[i]!=s2[j+]) j=next[j];
if(s1[i]==s2[j+]) ++j;
if(j==len2-){
j=next[j];
printf("%d\n",i-len2+);
}
}
}
//返回模式串在主串上出现的次数
int kmp_count(){
int cnt=,j=-;
for(int i=;i<len1;++i){
while(j>-&&s1[i]!=s2[j+]) j=next[j];
if(s1[i]==s2[j+]) ++j;
if(j==len2-){
++cnt;
j=next[j];
}
}
return cnt;
} int main(){
scanf("%d",&T);
while(T--){
scanf("%s%s",s2,s1);
len1=strlen(s1);
len2=strlen(s2);
get_next();
printf("%d\n",kmp_count());
}
return ;
}

(模板)poj3461(kmp模板题)的更多相关文章

  1. kmp模板 && 扩展kmp模板

    kmp模板: #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; ...

  2. POJ3461 KMP 模板题

    最近忙着考研复习,所以刷题少了.. 数据结构昨天重新学习了一下KMP算法,今天自己试着写了写,问题还不少,不过KMP算法总归是理解了,以前看v_JULY_v的博客,一头雾水,现在终于懂了他为什么要在算 ...

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

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

  4. POJ:3461-Oulipo(KMP模板题)

    原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...

  5. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  6. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  7. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  8. Oulipo HDU 1686 KMP模板

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

  9. 洛谷P3375 - 【模板】KMP字符串匹配

    原题链接 Description 模板题啦~ Code //[模板]KMP字符串匹配 #include <cstdio> #include <cstring> int cons ...

随机推荐

  1. P4148 简单题 k-d tree

    思路:\(k-d\ tree\) 提交:2次 错因:整棵树重构时的严重错误:没有维护父子关系(之前写的是假重构所以没有维护父子关系) 题解: 遇到一个新的点就插进去,如果之前出现过就把权值加上. 代码 ...

  2. word黏贴图片显示不出来

    word图片转存,是指UEditor为了解决用户从word中复制了一篇图文混排的文章粘贴到编辑器之后,word文章中的图片数据无法显示在编辑器中,也无法提交到服务器上的问题而开发的一个操作简便的图片转 ...

  3. java上传文件夹

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用.此控件PC全平台支持包括mac,linux系统的文件上传,文章末尾将附上控件下载与教程链接 ...

  4. 在Vue中使用TypeScript

    TypeScript基础学习 初始化TS项目 Vue装饰器 vue-typescript-admin框架 1.TypeScript基础学习 2.初始化TS项目 3.Vue装饰器 Vue装饰器常用的有下 ...

  5. 1~100卡特兰数(存一下hhhh)

    1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 35357670 129644790 477638700 ...

  6. 【概率论】4-1:随机变量的期望(The Expectation of a Random Variable Part I)

    title: [概率论]4-1:随机变量的期望(The Expectation of a Random Variable Part I) categories: - Mathematic - Prob ...

  7. SDUT2176 -> 递归的函数

    递归的函数                                         Time Limit: 1000 msMemory Limit: 65536 KiB Problem Des ...

  8. windows游戏编程封装窗口类

    本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/22451353 作者:jadeshu   邮箱: jades ...

  9. OpenGL+VS2010环境配置及遇到的问题

    OpenGL+VS2010+GLUT工具包+WIN10系统: 第一步,安装GLUT工具包 Windows环境下的GLUT下载地址:(大小约为150k) http://www.opengl.org/re ...

  10. wordpress爆破脚本的编写

    import requests import sys import queue import threading import time import getopt urll='' users='' ...