地址: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. 系统管理模块_岗位管理_改进_使用ModelDroven方案_套用美工写好的页面效果_添加功能与修改功能使用同一个页面

    改进_使用ModelDroven方案 @Controller @Scope("prototype") public class RoleAction extends ActionS ...

  2. shell脚本学习总结06--数学计算

    在bash中可利用let,(())和[]执行基本的操作,高级操作将会使用expr和bc 运算符:+,—,*,/,**(幂) (()) [root@Director ~]# ((c=2**3-9%2)) ...

  3. 在 Ubuntu Mate 16.04 上通过 PPA 升级 Mate 1.14

    导读 Mate 桌面环境 1.14 现在可以在 Ubuntu Mate 16.04 ("Xenial Xerus") 上使用了.根据这个版本的描述,为了全面测试 Mate 1.14 ...

  4. 【BZOJ4262】Sum 单调栈+线段树

    [BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...

  5. ionic 移动开发性能调优-去除动画

    <ion-refresher></ion-refresher> ion-refresher指令有以下可选的属性: on-refresh - 当用户向下拉动足够的距离并松开时,执 ...

  6. 运用JS设置cookie、读取cookie、删除cookiev

    JS设置cookie: 假设在A页面中要保存变量username的值("jack")到cookie中,key值为name,则相应的JS代码为: document.cookie=&q ...

  7. springData 整合 Rrdis

    导包(使用maven的话,采用坐标的方式) <!-- redis nosql 内存数据库 --> <dependency> <groupId>redis.clien ...

  8. django高级应用(分页功能)

    django高级应用(分页功能) 1.原生分页应用 前端html代码 <!DOCTYPE html> <html lang="en"> <head&g ...

  9. 用Recover来实现更健壮的go程序

    缘起:线上的go service 挂了,无法启动. 原因:采用的第三方库有个bug, 在go携程里面执行task的时候会产生out of range 的panic, 而我又把任务队列每次加载的时候重做 ...

  10. linux下的KSM内存共享机制分析

    2017-04-26 KSM是内核中的一种内存共享机制,在2.6.36版本的内核中开始引入,简单来说就是其会 合并某些相同的页面以减少页面冗余.在内核中有一个KSM守护进程 ksmd,它定期扫描用户向 ...