Simpsons’ Hidden Talents

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

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
 

简单的kmp。。。

代码:

 #include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=;
char str[maxn],st[maxn];
int next[maxn];
int main()
{
while(gets(str)!=NULL)
{
gets(st);
int lena=strlen(str);
int lenb=strlen(st);
int i=,j=-;
next[]=-;
while(i<lena)
{
if(j==-||str[i]==str[j])
next[++i]=++j;
else j=next[j];
}
i=j=;
while(i<lenb)
{
if(j==-||str[j]==st[i])
{
i++;
j++;
}
else j=next[j];
}
if(j==) printf("0\n");
else
printf("%s %d\n",st+lenb-j,j);;
} }

hduoj------2594 Simpsons’ Hidden Talents的更多相关文章

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

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

  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 KMP

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

  4. hdu 2594 Simpsons’ Hidden Talents KMP应用

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

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

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

  6. hdu 2594 Simpsons’ Hidden Talents(KMP入门)

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

  7. HDU——T 2594 Simpsons’ Hidden Talents

    http://acm.hdu.edu.cn/showproblem.php?pid=2594 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  8. hdu 2594 Simpsons’ Hidden Talents(扩展kmp)

    Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren’ ...

  9. 【HDU 2594 Simpsons' Hidden Talents】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  10. hdu 2594 Simpsons’ Hidden Talents

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路:将两个串连起来求一遍Next数组就行长度为两者之和,遍历时注意长度应该小于两个串中的最小值 ...

随机推荐

  1. Cheatsheet: 2015 04.01 ~ 04.30

    Other CentOS 7.1 Released: Installation Guide with Screenshots A Git Style Guide Recommender System ...

  2. jquery之 off()方法

    off()函数用于移除元素上绑定的一个或多个事件的事件处理函数. off()函数主要用于解除由on()函数绑定的事件处理函数. 该函数属于jQuery对象(实例). 语法 jQuery 1.7 新增该 ...

  3. [HDOJ5763]Another Meaning(KMP, DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...

  4. 头文件为什么要加#ifndef #define #endif

    #ifndef 在头文件中的作用 在一个大的软件工程里面,可能会有多个文件同时包含一个头文件,当这些文件编译链接成一个可执行文件时 ,就会出现大量“重定义”的错误.在头文件中实用#ifndef #de ...

  5. [SAP ABAP开发技术总结]字符编码与解码、Unicode

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. So easy Webservice 4.Java方式访问WebService(使用jdk1.6以上 wsimport命令)

    1.选中要调用的服务单击”服务说明” 2.获取wsdl文件.使用JDK1.6以上的版本的wsimport命令 a) 例如选中:http://webservice.webxml.com.cn/WebSe ...

  7. hdu 5150 Sum Sum Sum 水

    Sum Sum Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  8. Codeforces Round #376 (Div. 2) F. Video Cards 数学,前缀和

    F. Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. HTML笔记(四) 框架

    通过框架,可以在一个窗口显示多个页面.而所谓的框架,就是指每一份HTML文档. 框架结构标签<frameset> 定义如何将窗口分割为框架. frameset定义了一系列的行列. rows ...

  10. Spring管理bean的生命周期

    1: bean的创建:   如果我们默认的scope配置为Singleton的话, bean的创建实在Spring容器创建的时候创建: 如果scope的配置为Prototype的话,bena的创建是在 ...