地址:http://acm.hdu.edu.cn/showproblem.php?pid=2594

题目:

Simpsons’ Hidden Talents

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8709    Accepted Submission(s): 3051

Problem Description
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
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.
 
Input
Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.
 
Output
Output consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed by the length of that prefix. If the longest such string is the empty string, then the output should be 0.
The lengths of s1 and s2 will be at most 50000.
 
Sample Input
clinton
homer
riemann
marjorie
 
Sample Output
0
rie 3
 
Source
 
Recommend
lcy
 思路:裸扩展kmp
 #include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
const int MM=;
int nt[MM],extand[MM];
char S[MM],T[MM];
void Getnext(const char *T){
int len=strlen(T),a=;
nt[]=len;
while(a<len- && T[a]==T[a+]) a++;
nt[]=a;
a=;
for(int k=;k<len;k++){
int p=a+nt[a]-,L=nt[k-a];
if( (k-)+L >= p){
int j = (p-k+)> ? (p-k+) : ;
while(k+j<len && T[k+j]==T[j]) j++;
nt[k]=j;
a=k;
}
else
nt[k]=L;
}
}
void GetExtand(const char *S,const char *T){
Getnext(T);
int slen=strlen(S),tlen=strlen(T),a=;
int MinLen = slen < tlen ? slen : tlen;
while(a<MinLen && S[a]==T[a]) a++;
extand[]=a;
a=;
for(int k=;k<slen;k++){
int p=a+extand[a]-, L=nt[k-a];
if( (k-)+L >= p){
int j= (p-k+) > ? (p-k+) : ;
while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
extand[k]=j;
a=k;
}
else
extand[k]=L;
}
}
int main()
{
while(scanf("%s%s",S,T)==)
{
GetExtand(T,S);
int len=strlen(T),mx=,st;
for(int i=;i<len;i++)
if(extand[i]>mx&&extand[i]==len-i) mx=extand[i],st=i;
for(int i=;i<mx;i++)
printf("%c",S[i]);
if(mx)
printf(" ");
printf("%d\n",mx);
}
return ;
}

hdu2597 Simpsons’ Hidden Talents的更多相关文章

  1. hdu 2594 Simpsons’ Hidden Talents KMP

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  2. HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  3. HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)

    HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...

  4. hduoj------2594 Simpsons’ Hidden Talents

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  5. hdu2594 Simpsons’ Hidden Talents kmp

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...

  6. hdu 2594 Simpsons’ Hidden Talents KMP应用

    Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...

  7. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  8. hdu2594 Simpsons' Hidden Talents【next数组应用】

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  9. HDU2594 Simpsons’ Hidden Talents 【KMP】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

随机推荐

  1. Run time setting设置详解

    Pacing转载自belie 1>     Run time setting设置中的Browser:‘Simulate a new user on each iteration’选项例如:录制了 ...

  2. 【python】matplotlib中文乱码问题

    http://www.pythoner.com/200.html 改matplotlibrc文件进入Python安装目录下的Lib\site-packages\matplotlib\mpl-data目 ...

  3. C# 导出Excel "正在中止线程" 错误

    导出Excel相信很多人都用过,但是我却遇到了一个问题 “正在中止线程” 源代码如下: public static void ExportExcel(string fileName, GridView ...

  4. win7win8 64位汇编开发环境合集安装与设置

    win7win8 64位汇编开发环境合集安装与设置 下载 win7 win8  64位汇编开发环境.rar 下载地址(免积分下载) http://download.csdn.net/detail/li ...

  5. 编译OSG_FBX插件

    安装FBX的SDK,例如C:\Program Files\Autodesk\FBX\FBX SDK\2017.1\ 在CMake配置lib库路径的时候,使用 C:\Program Files\Auto ...

  6. MyBatis基础入门

    1.MyBatis概述 MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动.创建connection.创 ...

  7. [Android Tips] 17. 查看 APK 签名信息

    从 APK 文件中获取签名信息 方法一 $ keytool -list -printcert -jarfile <path of APK> 方法二 解压 APK 文件,释放出 META-I ...

  8. 在 Java 应用程序中绑定 Bean 和数据

    本指南介绍了 NetBeans IDE 对 Java 应用程序中 Bean 绑定和数据绑定的支持. 要学完本教程,您需要具备以下软件和资源. 软件或资源 要求的版本 NetBeans IDE 版本 7 ...

  9. 160429、nodejs--Socket.IO即时通讯

    动态web 在html5以前,web的设计上并没有考虑过动态,他一直是围绕着文档设计的,我们看以前比较老的网站,基本上都是某一刻用来显示单一的文档的,用户请求一次web页面,获取一个页面,但是随着时间 ...

  10. C++中,将单精度浮点数转换成2进制数

    在C++里,实数(float)是用四个字节即三十二位二进制位来存储的.其中有1位符号位,8位指数位和23位有效数字位.实际上有效数字位是24位,因为第一位有效数字总是"1",不必存 ...