hdu 1686 & poj 2406 & poj 2752 (KMP入门三弹连发)
首先第一题
戳我穿越;http://acm.hdu.edu.cn/showproblem.php?pid=1686
题目大意好理解,每组输入一个子串和一个母串,问在母串中有多少个子串?
文明人不要暴力,因为宽度会超时,除去暴力后这就是赤果果的KMP
KMP的重点在于在子串中建立一个匹配表,记录 到每一位的 前缀后缀 中的相同的子子串的最大长度
然后在比较子母串的时候当遇到不同时 后移的位数就是前面相同的个数减去对应的匹配表例的数
额 讲的不清不楚 那推荐戳这里:http://kb.cnblogs.com/page/176818/
那这题就是个裸的KMP模板
code 还是很简单的
#include<cstdio>
#include<cstring>
using namespace std;
char cword[10001];
int num[10001];
char mword[1000001];
int main()
{
int t,sum,i,x,j,y;
char temp;
while (~scanf("%d",&t))
{
while (t--)
{
sum=0;
j=-1;i=0;
num[0]=-1;
scanf("%s",cword);
scanf("%s",mword);
x=strlen(cword);
y=strlen(mword);
while (i<x) //num数组就是用来储存匹配表
{
if (j==-1||cword[i]==cword[j])
{
j++;
i++;
if (cword[i]!=cword[j])
num[i]=j;
else
num[i]=num[j];
}
else
j=num[j];
}
i=0;j=0;
while (i<y)
{
if (j==-1||mword[i]==cword[j])
{
i++;
j++;
}
else
j=num[j];
if (j==x)
{
sum++;
j=num[j];
}
}
printf("%d\n",sum);
}
}
return 0;
}
第二题 :pid=124http://acm.hdu.edu.cn/showproblem.php?7
输出一个单词里最多是由多少个子串重复连接而成 注意 是重复 连接 而成,如abefab因为ab没有连接,所以应该输出1
也是kmp的应用,考虑有特殊情况
code更短
#include<cstdio>
#include<cstring>
using namespace std;
char yj[1000001];
int jjc[1000001];
int main()
{
int i,j,x,n;
while (~scanf("%s",yj))
{
if (yj[0]=='.')
break;
j=-1;
jjc[0]=-1;
x=strlen(yj);
for (i=0;i<x;)
{
if (j==-1||yj[i]==yj[j]) jjc[++i]=++j;
else j=jjc[j];
}
if (x%(x-jjc[x])==0)
printf("%d\n",x/(x-jjc[x]));
else
printf("1\n");
}
return 0;
}
第三题:http://poj.org/problem?id=2752
就是找出一行名字中在前缀后缀中都存在的字串的长度,升序输出
对num数组匹配表的运用,既然在前缀后缀中都存在,那么其字串的最后一个字母必定和整个串的最后一位相同
然后再利用num数组一直向下滚一边
code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char yj[400001];
int num[400001];
int jjc[400001];
int main()
{
int i,j,x,t,k;
while (~scanf("%s",yj))
{
j=-1;i=0;
num[0]=-1;
x=strlen(yj);
while (i<x)
{
if (j==-1||yj[i]==yj[j])
num[++i]=++j;
else
j=num[j];
}
t=num[x-1];
k=0;
while (t!=-1)
{
if (yj[t]==yj[x-1])
jjc[k++]=t+1;
t=num[t];
}
for (i=k-1;i>=0;i--)
printf("%d ",jjc[i]);
printf("%d\n",x);
}
return 0;
}
hdu 1686 & poj 2406 & poj 2752 (KMP入门三弹连发)的更多相关文章
- 题解报告:hdu 2087 剪花布条(KMP入门)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...
- poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)
http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submiss ...
- POJ 2406 - Power Strings - [KMP求最小循环节]
题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...
- POJ 2406 Power Strings KMP求周期
传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...
- POJ 2406 Power Strings (KMP)
Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...
- POJ 2406 Power Strings KMP算法之next数组的应用
题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...
- poj 2406 Power Strings kmp算法
点击打开链接 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27368 Accepted: ...
- POJ 2406 Power Strings KMP运用题解
本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...
- poj 2406 Power Srings (kmp循环节) (经典)
<题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...
随机推荐
- C++字符串和向量
陷阱:C字符串使用=和== char a_string[10]; a_string="Hello" 非法 strcpy(a_string,"Hello"); ...
- apache安装配置
因为个人是在docker上面做实验的,所以可以多少会有些出入. 1.先启动一个docker,配置好基本的工具,网络啊,ssh啊是,tar啊,wget啊,vim等等. 其次去官网获取自己想要的压缩文件的 ...
- intellij idea 搜索功能快捷键
intellij idea是一款超智能的编译器,因此在信息资源的搜索功能中给我们的用户提供了很大的帮助.同样作为java编译器的eclipse和myeclipse在搜索方面就比intellij ide ...
- 2.1、CDH 搭建Hadoop在安装(为Cloudera Manager配置存储库)
步骤1:为Cloudera Manager配置存储库 使用包管理工具安装Cloudera Manager yum 对于RHEL兼容系统, zypper对于SLES,和 apt-get对于Ubuntu. ...
- Mono vs IL2CPP
[Mono vs IL2CPP] 参考:http://blog.csdn.net/gz_huangzl/article/details/52486255
- MySQL driver for Node
[MySQL driver for Node] 1.安装 2.一个示例 From this example, you can learn the following: Every method you ...
- 判断元素16种方法expected_conditions
前言 标签(空格分隔): 判断元素 经常有小伙伴问,如何判断一个元素是否存在,如何判断alert弹窗出来了,如何判断动态的元素等等一系列的判断,在selenium的expected_condition ...
- centos 7 下 Ceph 配置安装
一.环境介绍 系统: CentOS Linux release 7.3.1611 (Core) 硬盘: 系统盘:300GB*2-raid 1 OSD:600GB*4-raid 5 ceph ...
- CentOS 下搭建Jenkins
1.下载安装包 A 可以连接外网: 导入仓库 wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat-stable/jen ...
- django的优缺点(非原创)
Django 大包大揽,用它来快速开发一些 Web 应用是不错的.如果你顺着 Django 的设计哲学来,你会觉得 Django 很好用,越用越顺手:相反,你如果不能融入或接受 Django 的设计哲 ...