hdu 2594 Simpsons’ Hidden Talents KMP
Simpsons’ Hidden Talents
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4683 Accepted Submission(s): 1702
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
#include <cstdio>
#include <string>
#include <iostream> using namespace std; int maxl[],p[]; int main()
{string a,b;
while(cin>>b>>a)
{ b=" "+b;
a=" "+a;
int m=b.length();
int n=a.length();
n--;
m--;
memset(p,,sizeof(p));
memset(maxl,,sizeof(maxl));
p[]=p[]=;
int j=;
for(int i=;i<=m;i++)
{
while(j>&&b[j+]!=b[i])j=p[j];
if(b[j+]==b[i])j++;
p[i]=j;
}
j=;
for(int i=;i<=n;i++)
{
while(j>&&b[j+]!=a[i])j=p[j];
if(b[j+]==a[i])j++;
maxl[i]=j;
}
if(maxl[n]==)cout<<<<endl;
else {
for(int i=;i<=maxl[n];i++)
cout<<b[i];
cout<<" "<<maxl[n]<<endl;
} getchar();
}
return ;
}
代码
hdu 2594 Simpsons’ Hidden Talents KMP的更多相关文章
- hdu 2594 Simpsons’ Hidden Talents KMP应用
Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...
- 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求s1前缀和s2后缀相同部分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 题目大意:给两串字符串s1,s2,,找到最长子串满足既是s1的前缀又是s2的后缀,输出子串,及相 ...
- 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)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就能够做出来,只是我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. ...
- hdu 2594 Simpsons’ Hidden Talents 【KMP】
题目链接:http://acm.acmcoder.com/showproblem.php?pid=2594 题意:求最长的串 同一时候是s1的前缀又是s2的后缀.输出子串和长度. 思路:kmp 代码: ...
- hdu 2594 Simpsons’ Hidden Talents(扩展kmp)
Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...
- 【HDU 2594 Simpsons' Hidden Talents】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
随机推荐
- 使用Adivisor配置增强处理
使用Adivisor配置增强处理 实现步骤: 1.通过MethodBeforeAdivice接口实现前置增强处理 public class ServiceBeforeAdvisor implement ...
- MyBatis 用户表记录数查询
搭建MyBatis开发环境,实现用户表记录数查询 1.在MyEclipse中创建工程,导入MyBatis的jar包
- 高效使用git的一些命令
1,添加文件到版本库 添加单个文件: git add filename 添加所有txt文件: git add *.txt 添加所有修改文件: git add ...
- vmware, failed to lock the file
电脑死机,进不了桌面,实在不行就重启,但是在运行着虚拟机,重启后打开VMware虚拟机,提示failed to lock the file,进不去了,急阿,里面不少资料呢...问万能的Google,终 ...
- 淘宝(阿里百川)手机客户端开发日记第四篇 自定义ListView详解
我们知道,如果采用官方的ListView,实现的功能在很多时候,并不能满足自己的业务需求,比如在设计到复杂的列表的时候,这一节,我们就开始动手自己实现自定义的ListView. 在上一节中,我们采用了 ...
- search in 2d matrix and serach minimum in rotated array
import java.io.*; import java.lang.reflect.Array; import java.util.Arrays; import java.util.Collecti ...
- 《linux备份与恢复之二》3.19 dump(文件系统备份)
<Linux指令从初学到精通>第3章文件管理,本章介绍了许多常用命令,如cp.ln.chmod.chown.diff.tar.mv等,因为这些都与文件管理相关,在日常的使用中经常用到,因此 ...
- linux ldconfig
http://blog.csdn.net/dante_k7/article/details/7211868 ldconfig的主要用途: 默认搜寻/lilb和/usr/lib,以及配置文件/etc/l ...
- ubuntu tar 命令详细讲解
Ubuntu--tar命令 tar zxvf ut6410-android2.1.tgz tar zcvf ut6410-android2.1.tgz ut6410-android2.1/ tar - ...
- maven的一些依赖
maven的一些依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...