hdu-1686(kmp)
题意:前面的都是废话。。。其实直接看输入要求和输出要求就可以了,就是给你两个字符串,问你第一个字符串在第二个字符串中出现几次;
解题思路:kmp。。。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define M 10050
#define N 1005000
using namespace std;
int next1[M];
char s[N];
char t[M];
int slen,tlen;
void get_next()
{
int j=0,k=-1;
next1[0]=-1;
while(j<tlen)
{
if(k==-1||t[j]==t[k])
{
next1[++j]=++k;
}
else
k=next1[k];
}
}
int kmp_cnt()
{
int ans=0,i=0,j=0;
if(slen==1&&tlen==1)
{
if(s[0]==t[0])
return 1;
else
return 0;
}
get_next();
for(int i=0;i<slen;i++)
{
while(j>0&&s[i]!=t[j])
j=next1[j];
if(s[i]==t[j])
j++;
if(j==tlen)
{
ans++;
j=next1[j];
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt;
cin>>tt;
while(tt--)
{
cin>>t;
cin>>s;
tlen=strlen(t);
slen=strlen(s);
cout<<kmp_cnt()<<endl;
}
}
hdu-1686(kmp)的更多相关文章
- Oulipo - HDU 1686 (KMP模板题)
题目大意:题目叙述很多,其实只看输入输出也能明白什么意思,给两个串W,T, 判断T串中包含几个串W. 分析:还是基础的KMP应用....................... 直接上代码. === ...
- Number Sequence HDU 1711(KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...
- HDU 1711(KMP)字符串匹配
链接 HDU 1711 Number Sequence KMP 算法 我以自己理解写的,写的不对,不明白的地方海王子出来,一起共同学习: 字符串匹配 就是KMP,一般思想,用一个for循环找开头 ...
- Theme Section - HDU 4763(KMP)
题目大意:给你一个串,从这个串里面找出一个前缀后缀中间相等的串的最大长度也就是 EAEBE,每个字母都代表一个串,E出现了三次,找出最长的那个E. 分析:我们知道KMP里面保存的就是前缀和后缀的最 ...
- poj2406 Power Strings(kmp)
poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...
- A * B Problem Plus HDU - 1402 (FFT)
A * B Problem Plus HDU - 1402 (FFT) Calculate A * B. InputEach line will contain two integers A and ...
- D - 淡黄的长裙 HDU - 4221(贪心)
D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...
- HDU 4763 (KMP算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意:给定一串字符,从中找出符合“EAEBE”格式的E的最大字符数.AB可以是任意数量的任意 ...
- 剪花布条 HDU - 2087(kmp,求不重叠匹配个数)
Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入 ...
- String Problem - HDU 3374 (kmp+最大最小表示)
题目大意:有一个字符串长度为N的字符串,这个字符串可以扩展出N个字符串,并且按照顺序编号,比如串 ” SKYLONG “ SKYLONG 1 KYLONGS 2 YLONGSK 3 LONGSKY ...
随机推荐
- BZOJ4361 isn 树状数组、DP、容斥
传送门 不考虑成为非降序列后停止的限制,那么答案显然是\(\sum\limits_{i=1}^N cnt_i \times (N-i)!\),其中\(cnt_i\)表示长度为\(i\)的非降序列数量 ...
- git pull 解决 refusing to merge unrelated histories 错误
解决办法: 1.cmd进入项目的根目录. 2.执行下面的命令:git pull origin master --allow-unrelated-histories.可以提交成功. 3.再次push.
- 体验usually.js的管道函数——pipe函数
体验usually.js的管道函数——pipe函数 usually.js 是一个面向现代 Web 开发的 JavaScript 函数库,基于 ES6 开发.最新版本2.4.1,最新版本usually. ...
- 字典 dict
# --------------------------我愿作一叶小舟,驶向远方.----------------------------------------------------------- ...
- Python-collections模块-57
返回顶部 模块的导入和使用 模块的导入应该在程序开始的地方 更多相关内容 http://www.cnblogs.com/Eva-J/articles/7292109.html 常用模块 colle ...
- WinRAR从入门到高级的操作技巧集合
一.基础技巧 1.批量建立文件夹 如果在工作中,经常要建立很多相同文件夹结构(如在备份数据时).那可以把这个繁琐的工作让WinRAR完成:先在“资源管理器”中把多个文件夹结构建好(包括其下的子文件夹) ...
- 【学习总结】之 3Blue1Brown系列
刷知乎看到的,各种力荐. YouTube: https://www.youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw/featured B站: https:// ...
- Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval o
pom.xml报错: Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apach ...
- NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plus
NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plushttps://docs. ...
- Tomcat Cluster
Tomcat群集配置| Tomcat集群| MuleSofthttps://www.mulesoft.com/tcat/tomcat-cluster Tomcat Clustering - A Ste ...