HDU——T 2594 Simpsons’ Hidden Talents
http://acm.hdu.edu.cn/showproblem.php?pid=2594
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9919 Accepted Submission(s): 3418
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.
The lengths of s1 and s2 will be at most 50000.
homer
riemann
marjorie
rie 3
题意:求出第一个串的最长前缀同时是后一个串的最长后缀
而kmp算法中next数组其实就是查找某串中每一位前面的子串的前后缀有多少位匹配
所以可以将两个串合并,求出next[L(L=l1+l2)],最后保证L<=l2&&L<=l1
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
char s1[N],s2[N],s3[N<<];
int p[N<<],l1,l2,l3; inline void Get_next()
{
for(int i=,j=;i<=l1+l2;i++)
{
for(;j>&&s3[j+]!=s3[i];) j=p[j];
if(s3[j+]==s3[i]) j++;
p[i]=j;
}
} int main()
{
for(;cin>>s1+>>s2+;)
{
l1=strlen(s1+); l2=strlen(s2+);
for(int i=;i<=l1;i++) s3[i]=s1[i];
for(int i=;i<=l2;i++) s3[i+l1]=s2[i];
Get_next(); int l=l1+l2;
for(;l>l1||l>l2;) l=p[l];
for(int i=;i<=l;i++) printf("%c",s1[i]);
if(l) printf(" %d\n",l);
else puts("");
}
return ;
}
cin很灵性、、、
HDU——T 2594 Simpsons’ Hidden Talents的更多相关文章
- HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)
HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...
- HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu 2594 Simpsons’ Hidden Talents KMP
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu 2594 Simpsons’ Hidden Talents(KMP入门)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu 2594 Simpsons’ Hidden Talents KMP应用
Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...
- hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 【HDU 2594 Simpsons' Hidden Talents】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu 2594 Simpsons’ Hidden Talents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路:将两个串连起来求一遍Next数组就行长度为两者之和,遍历时注意长度应该小于两个串中的最小值 ...
- hdu 2594 Simpsons’ Hidden Talents(扩展kmp)
Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...
随机推荐
- mesg---设置当前终端的写权限
mesg命令用于设置当前终端的写权限,即是否让其他用户向本终端发信息.将mesg设置y时,其他用户可利用write命令将信息直接显示在您的屏幕上. 语法 mesg(参数) 参数 y/n:y表示运行向当 ...
- UML 绘图关系
1 继承 子类继承父类 2 实现 实现类实现接口 3 依赖 (偶然.临时.比较弱关联) 类 A 使用了类 B,如果类 B 产生变化将会影响类A ...
- Android调试命令总结
转载表明来源:http://blog.csdn.net/yzzst/article/details/47128581 创业要接地气,GOOGLE.亚马逊.微软在中国做的怎么样,全然取决于他们的本地化程 ...
- HDUOJ Let the Balloon Rise 1004
/* Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- 为什么选择Sqoop1
1)大部分企业还在使用sqoop1版本 2)sqoop1能满足公司的基本需求 3)sqoop2 功能还不是很成熟和完善 4)sqoop只是一个工具而已,相对比较简单
- Python正则表达式初识(五)
正则表达式的内容很丰富,今天小编继续给大家分享Python正则表达式的基础知识.今天要给大家的讲的特殊字符是竖线“|”.竖线“|”实质上是一个或的关系. 1.直接上代码演示,比方说我们需要匹配一个字符 ...
- TI Code Composer Studio MSP430系列驱动源代码
一.参考TI官网驱动源代码 安装打开Code Composer Studio,如下图所示,最近在调试MSP430G2533的AD,需要参考官网的底层驱动配置. 从Code Composer Studi ...
- Linux学习总结(8)——VMware v12.1.1 专业版以及永久密钥
VMware v12.1.1 专业版以及永久密钥 热门虚拟机软件VMware Workstation 现已更新至v12.1.1 专业版!12.0属于大型更新,专门为Win10的安装和使用做了优化,支持 ...
- Nginx模型 & 惊群问题
这篇写的不错 http://www.cnblogs.com/linguoguo/p/5511293.html Nginx为啥性能高-多进程异步IO模型 1. 对于每个worker进程来说,独立的进程, ...
- 【J2EE】在项目中理解J2EE规范
J2EE平台由一整套服务(Service),应用程序接口(API)和协议构成,它对开发企业级应用提供了功能支持.13个核心技术各自是JDBC, JNDI, EJB, RMI, JSP ...