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. L - Prime Number(求n内质数的个数)

    Description Write a program which reads an integer n and prints the number of prime numbers which ar ...

  2. jmeter(一)工具介绍(二)

    1.Jmeter 概要描叙 jmeter 是一款专门用于功能测试和压力测试的轻量级测试开发平台.多数情况下是用作压力测试,该测试工具在阿里巴巴有着广泛的使用,估计是不要钱吧,哈哈,功能上来说,整个平台 ...

  3. spark调试环境搭建

    到目前为止,基于RDD的spark streamming实时应用和离线应用(主要解析日志)已经写了一些,但是对spark的了解还是很少,所以决心花点精力,对spark做一些比较深入的了解和学习.参照之 ...

  4. zojDakar Rally(01背包)

    01背包 加上每次更新解题数目最多 总用时最少 因为要保证用时最少,要先把时长由小到大排序. 没排序 WA了几小时..链接 #include <iostream> #include< ...

  5. [书目20140902]实战Windows Azure——微软云计算平台技术详解 --徐子岩

    目录第1章  云计算技术简介    1.1  云计算所要解决的问题    1.2  云计算平台的分类    1.3  微软云计算平台Windows Azure        1.3.1  高可用性   ...

  6. React.js 基本环境安装

    安装 React.js React.js 单独使用基本上是不可能的事情.不要指望着类似于 jQuery 下载放到 <head /> 标签就开始使用.使用 React.js 不管在开发阶段生 ...

  7. eclipse debug java 源码

    当我们需要研究java SE的时候,debug 源码是个不错的选择,可以帮助我们清楚了解java 封装jar包的具体实现. 因为oracle 提供的源码jar包为了节省空间,所以没有将调试信息一起打包 ...

  8. 鸢尾花数据集-iris.data

    iris.data 5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setosa 4.6,3. ...

  9. freenas iscsi initiator 配置

    1.加载Iscsi Initiator 模块 freebsd从7.0开始已经包含了Iscsi Initiator ,不需要安装后再使用,但在使用前,需要加载模块. # kldload -v iscsi ...

  10. ubuntu apt-update NO_PUBKEY 40976EAF437D05B5 NO_PUBKEY 3B4FE6ACC0B21F32

    Fetched 28.1 MB in 11s (2344 kB/s) W: GPG error: http://archive.canonical.com xenial Release: The fo ...