地址: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. 表达式树在LINQ动态查询

    动态构建表达式树,最佳实践版,很实用! public class FilterCollection : Collection<IList<Filter>> { public F ...

  2. html转pdf工具:wkhtmltopdf.exe

    百度云下载:http://pan.baidu.com/s/1dEX0h93  

  3. AWS系列-AWS EC2实例类型改配(机器配置升级)

    1.1 EC2实例升级需要先把升级的实例停机才能升级 1.2 如图选择需要升级的EC2,点击操作,实例设置,更改实例类型 1.3 选择你要想的配置,点击应用 1.4 启动EC2实例即可

  4. 提高ASP.NET网站性能的方法

    http://www.360doc.com/content/14/0705/18/7662927_392224856.shtml   Asp.NET有许多秘密,当你了解了这些秘密后,可以使得你的ASP ...

  5. cocos lua 加密方案

    cocos2d使用的是luajit,lua原生编译出来的bytecode和luajit是不兼容的,所以直接用luac法编译出来的bytecode脚本无法在cocos2d中使用. 目前所指的解决方案有2 ...

  6. 【黑金原创教程】【Modelsim】【第二章】Modelsim就是电视机

    声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/al ...

  7. Struts入门(三)深入Struts用法讲解

    访问Servlet API Action搜索顺序 动态方法调用 指定多个配置文件 默认Action Struts 后缀 接收参数 处理结果类型 1.访问Servlet API 首先我们了解什么是Ser ...

  8. FZU 2144 Shooting Game (贪心区域划分)

    Problem 2144 Shooting Game Accept: 370 Submit: 1902 Time Limit: 1000 mSec Memory Limit : 32768 KB Pr ...

  9. Go语言 map的实现

    Go中的map在底层是用哈希表实现的,你可以在 $GOROOT/src/pkg/runtime/hashmap.goc 找到它的实现. 数据结构 哈希表的数据结构中一些关键的域如下所示: struct ...

  10. HDU_5538_House Building

    House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...