题目链接: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. 'com.example.mybatisplus.mapper.PersonMapper' that could not be found.

    通常有两种原因,配置原因,或者是mapper相关文件,mapper.java或 mapper.xml内部错误 如果是配置原因 解决方式1 统一配置mapper //import org.mybatis ...

  2. https 非对称加密

  3. C语言学习笔记6-数组

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

  4. 文件对比工具 Beyond Compare 4.2.9中文破解版for win 附通用注册码

    链接: https://pan.baidu.com/s/1yYxPo8nNv0PuOA1ZC9-F1w 提取码: v76g 注册码: --- BEGIN LICENSE KEY --- H1bJTd2 ...

  5. UVALive 4394 String painter ——(区间DP)

    其实这个dp过程有点似懂非懂...代码如下: #include <stdio.h> #include <algorithm> #include <string.h> ...

  6. 浅谈 es6 箭头函数, reduce函数介绍

    今天来谈一下箭头函数, es6的新特性 首先我们来看下箭头函数长什么样子, let result = (param1, param2) => param1+param2; 上述代码 按照以前书写 ...

  7. Flume-自定义 Source

    Source 是负责接收数据到 Flume Agent 的组件. Source 组件可以处理各种类型.各种格式的日志数据,包括 avro.thrift.exec.jms.spooling direct ...

  8. 【MYSQL】存储过程示例

    GROUPEMP_EXISTS: ), ), )) LANGUAGE SQL NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT ' ...

  9. 时间戳 Date.parse()和dateObject.getTime()的区别

    一. Date.parse() parse() 方法可解析一个日期时间字符串,并返回 1970/1/1 午夜距离该日期时间的毫秒数. 解析: 1.parse() 方法为Date 对象的静态方法,一般采 ...

  10. koa中 log4js使用

    一.新建一个log4js.js配置文件 let path = require('path'); // 日志根目录 let baseLogPath = path.resolve(__dirname, ' ...