(模板)poj3461(kmp模板题)
题目链接: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模板题)的更多相关文章
- kmp模板 && 扩展kmp模板
kmp模板: #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; ...
- POJ3461 KMP 模板题
最近忙着考研复习,所以刷题少了.. 数据结构昨天重新学习了一下KMP算法,今天自己试着写了写,问题还不少,不过KMP算法总归是理解了,以前看v_JULY_v的博客,一头雾水,现在终于懂了他为什么要在算 ...
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- POJ:3461-Oulipo(KMP模板题)
原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...
- POJ Oulipo KMP 模板题
http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4 ...
- POJ Oulipo(KMP模板题)
题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- Oulipo HDU 1686 KMP模板
题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...
- 洛谷P3375 - 【模板】KMP字符串匹配
原题链接 Description 模板题啦~ Code //[模板]KMP字符串匹配 #include <cstdio> #include <cstring> int cons ...
随机推荐
- Find a multiple POJ - 2356 【鸽巢原理应用】
Problem DescriptionThe input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). E ...
- 彻底搞清楚setState
setState最常见的问题是,是异步的还是同步的? setState在React.Component中的函数,是同步函数.但是我们调用的时候,不同的传参和不同的调用位置都会导致不同的结果. 从页面看 ...
- House Lawn Kattis - houselawn
Problem You have just bought a new house, and it has a huge, beautiful lawn. A lawn that needs cutti ...
- 【Python】使用Python处理RAW格式图片,并根据实际情况完成分组打包发送
背景 出游之后,朋友交换的照片格式大多是RAW格式,一些人想要JPG格式,但是百度云盘非会员的下载速度惨不忍睹,所以我想着通过微信群直接传(这个在事后也被证实不能完全解决问题,微信限制了每天传递文件的 ...
- JS获取div高度的方法
有时在写页面时,需要获取一个div的高度.怎么才能获取呢?哈哈,先上结论.有两种方法. offsetHeight .clientHeight getComputedStyle offsetHeight ...
- 有效管理进程的几个linux命令
一般来说,应用程序进程的生命周期有三种主要状态:启动.运行和停止.如果我们想成为称职的管理员,每个状态都可以而且应该得到认真的管理.这八个命令可用于管理进程的整个生命周期. 启动进程 启动进程的最简单 ...
- vuejs是如何编译checkbox数组的v-model的
随便用官方的例子编译了一下, 发现如下 _c("input", { directives: [ { name: "model", rawName: " ...
- IDEA版本控制工具VCS中使用Git,以及快捷键总结(不使用命令)
场景介绍: 工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 ...
- ubuntu上面Parity 安装
sudo wget https://raw.githubusercontent.com/paritytech/parity/master/scripts/parity.service -O /et ...
- python 度分秒转度
#必须是u类型==================u==================== by gisoracle def dmstod(dms): #arcpy.AddMessage(" ...