hdu-4468-Spy-KMP+贪心
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4468
题目意思:
给你一个串r,求一个串s,使得s的前缀1+s的前缀2+s的前缀3+...+s的前缀n+s=r .
解题思路:
KMP+贪心。
初始时把r[1]赋给s[1],从r中每个字符从前至后依次匹配s,当匹配失败时,说明该字符在模式串中没有出现,由贪心思想,把它放到最后(前面满足要求的话,最短的也要从上个完全匹配开始),所以把从上一次的完全匹配的位置到该字符之间的所有字符作为新的模式串,继续匹配。
当完全匹配时,更新上次完全匹配的位置值。
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF 0x1f1f1f1f
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
using namespace std; #define M 100005 /*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
*/
char rr[M],pp[M];
int rn,pn,next[M]; void getnext(int s)
{
int j=next[s-1];//只需从上一个开始计算
//int j=s-1;
for(int i=s;i<=pn;i++)
{
while(j>0&&pp[j+1]-pp[i])
j=next[j];
if(pp[j+1]==pp[i])
j++;
next[i]=j;
}
return ;
} int main()
{
int ca=0; while(scanf("%s",rr+1)!=EOF)
{
rn=strlen(rr+1);
pp[1]=rr[1];
pn=1;
next[1]=0;
int last=1;//记录上一个完全匹配的位置
int j=0;
for(int i=1;i<=rn;i++)
{
while(j>0&&pp[j+1]-rr[i])
j=next[j];
if(pp[j+1]==rr[i])
j++;
if(j==pn) //找到了新的完全匹配
{
last=i-pn+1;
j=next[j];//往回跳一个
}
else if(j==0) //有新的字母,只能作为最后一个
{
int tmp=pn;
for(int k=last+pn;k<=i;k++)
pp[++pn]=rr[k];
getnext(tmp+1); //不是getnext(last+tmp) last是随i增加的,wa了一上午
//j=pn;
}
}
printf("Case %d: %d\n",++ca,rn-last+1);
}
return 0;
}
hdu-4468-Spy-KMP+贪心的更多相关文章
- HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)
Description “Be subtle! Be subtle! And use your spies for every kind of business. ”― Sun Tzu“A spy w ...
- hdu 4468 spy 极其精彩的一道kmp灵活运用题
出的超级好的一道题.至于好在哪里,请思考题目: 题意抽象出来为给定一个字符串r,找出它的一个最短后缀s,使得这个r可以被 s的某前缀+s的某前缀+......+s的某前缀+s本身构造出来. 具体题目描 ...
- HDU 4442 Physical Examination(贪心)
HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...
- HDU 4749 Parade Show(贪心+kmp)
题目链接 题目都看不懂,做毛线...看懂了之后就是kmp出,所有的匹配区间,然后DP可以写,贪心也可以做把,DP应该需要优化一下,直接贪,也应该对的,经典贪心问题. #include<iostr ...
- HDU 2087 KMP模板题
1.HDU 2087 2.题意:一个主串,一个子串,求子串在主串里出现了几次. 3.总结:看了题解,还是不太懂.. //#include<iostream>#include<cmat ...
- Number Sequence HDU 1711(KMP)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 首次接触KMP,自己都不是特别理解.在网上百度看了好几个帖子之后,对KMP也有了初步的理解. #inclu ...
- hdu 1686 Oulipo KMP匹配次数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...
- HDU 5835 Danganronpa (贪心)
Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...
- HDU 5821 Ball (贪心)
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- HDU 4763 (KMP算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意:给定一串字符,从中找出符合“EAEBE”格式的E的最大字符数.AB可以是任意数量的任意 ...
随机推荐
- 重新关联bat文件的打开方式为系统默认方式
为什么“BAT”扩展名的默认打开方式:显示出来的居然是“%1”这么一个怪异的东东,具体在什么位置的? c:\windowssystem32\command.com修复bat关联,打开command.c ...
- Linux软件大全
https://www.linux-apps.com/browse/cat/239/ord/latest/http://www.cnblogs.com/riskyer/p/3262745.htmlht ...
- java应用程序远程登录linux并执行其命令(ssh jar包)
http://www.ganymed.ethz.ch/ssh2/在这个网址下载一个调用ssh和scp命令的jar包. 然后,就可以写程序了.将上面的jar包导入MyEclipse,下面是一个类的实例代 ...
- Recover a file even if it was not committed but it has to have been added when you use git reset head by mistake.
git init echo hello >> test.txt git add test.txt Now the blob is created but it is referenced ...
- xshell使用命令总结
这个工具主要是链接linux 并且可以从linux上面下载文件到本地 还有上传本地文件到linux上面 下载首先需要压缩打包命令为: tar -cf am_mailer.tar * sz am_mai ...
- poj3267--The Cow Lexicon(dp:字符串组合)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3864 D ...
- html系列教程--描述
什么是 HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记语言 (ma ...
- SQL中去除某字段中的某个字符语法
update S_ENTERPRISE set DAY_PROCESS=replace(DAY_PROCESS,'吨','') where DAY_PROCESS like '%吨%'
- win32 消息说明
WM_NULL = $0000; WM_CREATE = $0001; 应用程序创建一个窗口 WM_DESTROY = $0002; 一个窗口被销毁 WM_MOVE = $0003; 移动一个窗口 W ...
- Object的增。删。查。改。遍历
1.增: 1.向对象添加属性和方法 (私有) ---> obj.属性 =""; 2.向对象原型添加方法 (公共) ---> obj.prototype. ...