Simpsons’ Hidden Talents

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 2594
64-bit integer IO format: %I64d      Java class name: Main

 
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

 
解题:求前缀后缀的最长相同长度,注意长度
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
char sa[<<],sb[];
int fail[<<];
void getFail(){
fail[] = fail[] = ;
for(int i = ; sa[i]; i++){
int j = fail[i];
while(j && sa[i] != sa[j]) j = fail[j];
fail[i+] = sa[i] == sa[j]?j+:;
}
}
int main() {
int i,j;
while(~scanf("%s %s",sa,sb)){
int len = strlen(sa),len2 = strlen(sb),i = len+len2;
for(i = len,j = ; sb[j]; i++,j++)
sa[i] = sb[j];
sa[i] = '\0';
getFail();
for(;fail[i] > len || fail[i] > len2; i--);
len = strlen(sa);
if(fail[i]){
printf("%s %d\n",sa+len-fail[i],fail[i]);
}else puts("");
}
return ;
}

BNUOJ 6719 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. 题解报告:hdu 2546 饭卡(01背包)

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  2. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

  3. linux下的c程序排版工具:indent 分类: linux 2014-06-14 20:05 720人阅读 评论(0) 收藏

    Linux下有一个方便的c语言程序排版工具,只要选择恰当的参数,可以轻易地使自己的程序具有统一的风格. 当然首先要安装indent,执行命令:apt-get install indent indent ...

  4. [转]Azure 表存储和 Windows Azure SQL Database - 比较与对照

    本文转自:https://msdn.microsoft.com/library/azure/jj553018 更新时间: 2014年10月 作者:Valery Mizonov 和 Seth Manhe ...

  5. spring中for循环中事务

    1.需求:批量插入一批数据,不用spring jdbc的批处理,用for循环插入数据. 2.遇到的问题:在for循环中,当一个插入不成功,前面插入成功的数据也将回滚. 3.初始设计:在service中 ...

  6. preg_replace_callback使用方法

    官网解释: 执行一个正则表达式搜索并且使用一个回调进行替换 (PHP 4 >= 4.0.5, PHP 5) preg_replace_callback — 执行一个正则表达式搜索并且使用一个回调 ...

  7. linux 安装 mongo 3.4

    要求:linux 安装 mongo 3.4 大体上,按照官网提供的方法来做. 系统是ubuntu 16.04 安装的是mongo3.4.8 社区版 1.         导入导入包管理系统使用的公钥 ...

  8. ubuntu设置root账号密码

    Ubuntu Linux有一个与众不同的特点,那就是初次使用时,你无法作为root来登录系统,为什么会这样?这就要从系统的安装说起.对于其他Linux系统来 说,一般在安装过程就设定root密码,这样 ...

  9. 这是一条立了Flag的不归路

    时间2017年7月11日 14:48:40 首次激活博客园的博客来进行学习记录,立下了不算远大的小目标,下一步就是要一步一步的往前走. Java是目前最普遍的使用语言之一,作为一名测试,本应该去学习更 ...

  10. idea 常用操作

    1.创建的maven项目,java文件不提示错误:有main方法但右击却找不到run选项的问题 1)首先要配置SDK--就是配置JDK 2)然后要按照提示信息导入某些maven相关的东西,就这个Eve ...