题目链接: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. springboot项目:以run as-->spring boot app方式启动,配置热部署(亲测可用!!!)

    1.在pom.xml中添加热部署依赖 <!-- 热部署 --> <!-- devtools可以实现页面热部署(即页面修改后会立即生效, 这个可以直接在application.prop ...

  2. [USACO15DEC] 最大流Max Flow && Tarjan 线性 LCA 教学?

    题面 显然是树上差分模板题啦,不知道树上差分的童鞋可以去百度一下,很简单. 然后顺带学了一下 tarjan 的 O(N+Q) 离线求LCA的算法 (准确的说难道不应该带个并查集的复杂度吗???) 算法 ...

  3. slf4j、jcl、jul、log4j1、log4j2、logback大总结[转]

    #1 系列目录 jdk-logging.log4j.logback日志介绍及原理 commons-logging与jdk-logging.log4j1.log4j2.logback的集成原理 slf4 ...

  4. electron-vue搭建项目

    原文链接 使用pdf.js插件与LODOP控件实现前端浏览器静默打印PDF文件 lodop官网地址:http://www.lodop.net/download.html 点击下载,文件里有使用手册 e ...

  5. Selenium执行cdp命令,driver.execute_cdp_cmd用法

    Chrome自带的开发者工具DevTools功能非常强大.有时候我们在使用Selenium操作浏览器时需要通过调用一下DevTools的方法来完成一些设置,如模拟移动设备,弱网模拟等等. Seleni ...

  6. TypeError: Data location must be "memory" for return parameter in function, but none was given.

      在用truffle编译智能合约时,报错 TypeError: Data location must be "memory" for return parameter in fu ...

  7. Mapping Pocos

    Mapping Pocos Example Pocos/Mappings public class Note { public int Id { get; set; } public DateTime ...

  8. [MySql]当虚拟机的IP地址自动更换后,JDBC使用原来的配置连不上MySql数据库时所报的异常。

    Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. ...

  9. OpenCL使用CL_MEM_USE_HOST_PTR存储器对象属性与存储器映射

    随着OpenCL的普及,现在有越来越多的移动设备以及平板.超级本等都支持OpenCL异构计算.而这些设备与桌面计算机.服务器相比而言性能不是占主要因素的,反而能耗更受人关注.因此,这些移动设备上的GP ...

  10. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...