首先第一题

戳我穿越;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入门三弹连发)的更多相关文章

  1. 题解报告:hdu 2087 剪花布条(KMP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...

  2. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  3. POJ 2406 - Power Strings - [KMP求最小循环节]

    题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...

  4. POJ 2406 Power Strings KMP求周期

    传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...

  5. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  6. POJ 2406 Power Strings KMP算法之next数组的应用

    题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...

  7. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  8. POJ 2406 Power Strings KMP运用题解

    本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...

  9. poj 2406 Power Srings (kmp循环节) (经典)

    <题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...

随机推荐

  1. java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    hibernet 报错 java.lang.StringIndexOutOfBoundsException: String index out of range: 0 处理方法  数据表字段为char ...

  2. mysql读写分离 主从同步

    MySQL主从复制与读写分离的实现 转载 2013年01月17日 18:20:12   MySQL主从复制与读写分离 MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy) ...

  3. 如何解决Android Studio解决DDMS真机/模拟器无法查看data目录问题

    android app开发中,文件.SharedPreference或数据库默认保存在/data文件夹下,有时需要查看该文件夹下数据文件是否创建成功时,发现竟然打不开data目录: 具体解决方式如下: ...

  4. 1.3.4、CDH 搭建Hadoop在安装之前(端口---Impala使用的端口)

    Impala使用的端口 Impala使用下表中列出的TCP端口.在部署Impala之前,请确保在每个系统上打开这些端口. Component Service Port Access Requireme ...

  5. msf客户端渗透(五):注册表

    先获取到一个session 上传nc到被攻击主机上 建立一个键值 创建一个策略 kali上查看是否成功创建键值 后台开启cmd 查看防火墙的策略 打开防火墙的端口 添加一条防火墙策略 在win7上查看 ...

  6. Java 并发AQS

    转载出处:http://www.cnblogs.com/waterystone/ 一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQu ...

  7. 【scrapy】其他问题

    今天看<python爬虫开发与项目实践>的17章写代码的时候发现,一个方法的结尾带了红色波浪线: def _process_booklist_item(self,item): ''' 处理 ...

  8. 笔记-Python中逗号的作用

    1.用,去掉额外的换行符

  9. Leetcode:LRU Cache,LFU Cache

    在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...

  10. .net 报错access to the path c:\tempimagefiles\msc_cntr_0.txt is denied

    报错信息: 解决方法: 在 Web.Config 的 <System.Web> 里加 <identity impersonate="true"/> 节点即可 ...