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. MYSQL主键自动增加的配置及auto_increment注意事项

    文章一 原文地址: http://ej38.com/showinfo/mysql-202971.html 文章二:   点击转入第二篇文章 在数据库应用,我们经常要用到唯一编号.在MySQL中可通过字 ...

  2. Beaglebone Black–GPIO 开关 LED(三极管与继电器实验)

    上一篇,用 GPIO 直接供电给 LED,用高低电平作开关,不靠谱.GPIO 是信号用的,不是当电源用的.而且,一个 GPIO 只能给可怜的 5mA 左右,取多了会烧(我没烧过不知道是不是真的会烧,但 ...

  3. Servlet上下文

    Servlet上下文 运行在Java虚拟机的每一个Web应用程序都有一个与之相关的Servlet上下文. Java Servlet API提供了一个ServletContext接口来表示上下文.在这个 ...

  4. nancy的诊断2

    接上文. 一 再看看第二个面板内容 点击带文号 的  Interactive Diagnostics 这个面板 如下 上面的是诊断程序,默认是系统提供的测试用的诊断程序.  你可以 通过实现IDiag ...

  5. this call和apply

    this指针总是指向一个对象,大致可以分为以下四种: 1,作为对象的方法调用(this指向该对象) 2,作为普通函数调用 当函数不作为对象的属性被调用时,也就是普通函数方式,此时的this总是指向全局 ...

  6. E2 2014.5.8 更新日志

    增加功能 增加报价单功能,可以针对指定客户生成报价单,可以直接生成一个在线地址,直接把地址发给客户在线打开 传统的报价,先生成一个EXCEL,再传给客户,使用E2,这一切都变得简单,你可生成一个在线地 ...

  7. iOS - Swift NSPoint 位置

    前言 结构体,这个结构体用来表示事物的一个坐标点. public typealias NSPoint = CGPoint public struct CGPoint { public var x: C ...

  8. LVS--NAT模型配置

    环境准备 管理IP地址 角色 备注 192.168.11.131 调度器(Director) 对外提供VIP服务的地址为192.168.1.114 192.168.11.132 RS1  网关为192 ...

  9. (一)S5PV210开发板常用易忘操作记录

    一.调试串口 2.SD卡槽 (三)启动方式选择 蜂鸣器下面的白色2针插座(图中红色线圈出来的那个)为选择USB/SD卡启动的开关.默认情况下为SD卡启动,如果需要USB启动则使用短路帽(若没有短路帽, ...

  10. 图形处理的api

      [1]旋转      public class MainActivity extends Activity { private float degrees;// 图片旋转的角度 @Override ...