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

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

【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.

霍默: 玛姬, 我突然想到有个办法可以发掘我们的潜力。

玛姬: 厄, 那是啥?

霍默: 比如我想知道自己是否有潜力蹚官场...

玛姬: 恩。

霍默: 因此我列了写政客的名字,比如Clinton,然后找到他名字的前缀与我名字后缀相匹配的最大长度。这表示我有多大的希望成为和Clinton一样的政客。

玛姬: 那么究竟为什么比对最长前缀后缀???

霍默: 毕竟大家都是真人不露相,玛姬。

玛姬: 那你的结果呢?

霍默: 0!

玛姬: 意料之中。

Homer: 但我知道,你肯定有潜在的数学天赋。

玛姬: 哪来的?

霍默: Riemann 和 Marjorie 匹配了 3!!!

玛姬: 黎曼是谁?

霍默: 不懂。

敲一个程序,对于给定的字符串s1与 s2,找出s1前缀与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.

输入有两行。第一行是s1,第二行是s2。你可以认为只有小写字母。

【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.

输出一行,包括最长s1前缀与s2后缀匹配的字符串,并输出前缀的长度。如果字符串不存在,输出0即可。

s1与s2的长度小于50000。

【Sample Input - 输入样例】

【Sample Output - 输出样例】

clinton

homer

riemann

marjorie

0

rie 3

【题解】

KMP比较部分的变形,利用next数组前移s1,匹配s2的时候为保存最后的匹配长度即可。

【代码 C++】

 #include<cstdio>
#include<cstring>
#define mx 50005
char s1[mx], s2[mx];
int nextS1[mx], optS2[mx], s1ED, s2ED;
void rdy(){
int i = , j;
nextS1[] = j = -;
while (i < s1ED){
if (j == - || s1[i] == s1[j]) nextS1[++i] = ++j;
else j = nextS1[j];
}
}
int count(){
int i = , j = ;
while (i < s2ED){
if (j == - || s2[i] == s1[j]) optS2[++i] = ++j;
else j = nextS1[j];
}
return optS2[s2ED];
}
int main(){
int len, i;
while (~scanf("%s%s", s1, s2)){
s1ED = strlen(s1); s2ED = strlen(s2);
rdy();
if (len = count()){
for (i = ; i < len; putchar(s1[i++]));
printf(" %d\n", len);
}
else puts("");
}
return ;
}

HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋)的更多相关文章

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

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

  2. hdu 2594 Simpsons’ Hidden Talents KMP

    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. 【HDU 2594 Simpsons' Hidden Talents】

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

  6. hdu 2594 Simpsons’ Hidden Talents

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

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

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

  8. HDU 2594 Simpsons’ Hidden Talents (KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就能够做出来,只是我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. ...

  9. HDU 2594 Simpsons’ Hidden Talents(KMP求s1前缀和s2后缀相同部分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 题目大意:给两串字符串s1,s2,,找到最长子串满足既是s1的前缀又是s2的后缀,输出子串,及相 ...

随机推荐

  1. ==,equal,hasCode(),identifyHasCode()浅析

    在java中如果我们要比较两个对象之间的关系的话有可能会用到下面的几种方法:==,equal,hasCode(),identifyHasCode(). ==用来比较对象本身是不是相同的. public ...

  2. 前端不为人知的一面–前端冷知识集锦 原文地址(http://web.jobbole.com/83473/);

    前端已经被玩儿坏了!像console.log()可以向控制台输出图片等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Quora上一个帖子,瞬间又GET了好多前 ...

  3. python DB.fetchall()--获取数据库所有记录列表

    查询到的数据格式为列表: 多个元素的列表:

  4. iOS发布条款检查表

    序号 分类 条款编号 条款 案例 1 功能 2.1 崩溃的程序将会被拒绝 2 2.2 有错误的程序将会被拒绝 点击版本升级无反应/点击版本升级,在线版本和当前版本都是2.0.3 3 2.3 跟开发者宣 ...

  5. Erlang-特性

    一.模式匹配: 模式匹配作为Erlang的基础,用来完成很多不同的任务:可以用它从数据结构中提取字段值,在函数中进行流程控制,或者当你向一个进程发送消息时,从并行程序刷选那些需要处理的消息: 二.函数 ...

  6. Web API 2 authentication with JWT

    Web API 2 authentication with JWT JSON Web Token (JWT) 使用 AngularJS & NodeJS 实现基于 token 的认证应用

  7. CSS选择器无法找到td

    .table >  tr > td  <----这样无法找到td 因为table在浏览器下会自动生成tbody,这样即可 .table > tbody > tr > ...

  8. 2.1:你的第一个AngularJS App

    本章,带你体验一个简单的开发流程,将一个静态的使用模拟数据的应用,变成具有AngularJS特性的动态web应用.在6-8章,作者将展示如何创建一个更复杂,更真实的AngularJS应用. 1.准备项 ...

  9. oracle初次使用连接不上

    问题描述: win10下,cmd运行 输入sqlplus报一下错误 SP2-1503: 无法初始化 Oracle 调用界面 SP2-0152: ORACLE 不能正常工作 解决办法 cmd右键--以管 ...

  10. 4、BOM编程/正则表达式

    1.    BOM编程 1.1. BOM编程基础 全称 Browser Object Model,浏览器对象模型. JavaScript是由浏览器中内置的javascript脚本解释器程序来执行jav ...