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. DB buffer bussy wait 分析一例

    ####sample 1: DB层分析OI DB层分析OI的信息如下: 1. 异常时间段,  Logical reads:/ Physical reads/ Physical write  指标都低于 ...

  2. shell 调试 `<<' is not matched

    我的这段脚本,验证数据库连接是否正常: #! /bin/sh...while ..do....sqlplus $user/ $passwd@$sid  <<!quit;! ... 单独执行 ...

  3. .net 字符串和JSON格式的互换

    近期又做了个问卷调查,问卷调查一次性要保存一二十个题目和答案!所以嘞,博主为了偷懒,就直接把答卷内容保存成了Json格式! 好处当然是很多啦! 只需一个字段就能保存整个答卷的内容! 想想都刺激!哈哈~ ...

  4. leetcode764 Largest Plus Sign

    思路: 首先使用dp计算出在每个位置(i, j)上下左右最多有多少个连续的1,得到up[i][j], down[i][j], left[i][j], right[i][j].然后计算这四个值中的最小值 ...

  5. js插件定义事件中,this引用的实现模拟

    在web项目中,经常会使用jquery和mui等js框架,之前只是按照API说明去按规则使用,比如在jq和mui中,事件处理函数中可以直接用this访问事件源,如下面的代码: <!DOCTYPE ...

  6. vue项目开发前的es6的知识储备

    let命令 学习笔记 1.let所声明的变量,只在let命令所在的代码块内有效. 2.不存在变量提升:所声明的变量一定要在声明后使用,否则报错. 一定要先声明,再去使用.let x=x;这样就是错误的 ...

  7. Python学习 Day 7 面向对象 类和实例 访问限制

    面向对象编程 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程 ...

  8. vba,设置,excel,wps ,页面设置

    全面认识页面设置之 PageSetup 对象我们在写 VBA 代码时,特别是做小型程序开发时,经常会用 VBA 来设置“页面设置”中的选项,还可用要用 VBA 来实现一些特殊的效果,这就需要使用 Pa ...

  9. H5 canvas-小球抛物线

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. CREATE FUNCTION - 定义一个新函数

    SYNOPSIS CREATE [ OR REPLACE ] FUNCTION name ( [ argtype [, ...] ] ) RETURNS rettype { LANGUAGE lang ...