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
题意:找字符串a和字符串b的最长公共前后缀
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int nextt[];
int extend[];
void getnext(string s){
int po; int len=s.length();
nextt[]=len; //初始化next[0]
int pos=;
while(s[pos]==s[pos+]&&pos<len-) pos++;
nextt[]=pos; //计算next[1]
po=; //初始化po的位置
for(int i=;i<len;i++){
if(nextt[i-po]+i<po+nextt[po]) //第一种情况,可以直接得到next[i]的值
nextt[i]=nextt[i-po];
else{ //第二种情况,要继续匹配才能得到next[i]的值
int j=po+nextt[po]-i;
if(j<) j=;
while(i+j<len&&s[j]==s[i+j]) j++;
nextt[i]=j;
po=i;
}
//cout<<nextt[i]<<endl;
}
}
void exkmp(string a,string b){
int len1=a.length(); int len2=b.length();
getnext(a);
int po; int pos=;
while(a[pos]==b[pos]&&pos<len1&&pos<len2) pos++;
extend[]=pos;
po=;
for(int i=;i<len2;i++){
if(nextt[i-po]+i<extend[po]+po)
extend[i]=nextt[i-po];
else{
int j=po+extend[po]-i;
if(j<) j=;
while(i+j<len2&&a[j]==b[i+j]&&j<len1) j++;
extend[i]=j;
po=i;
}
}
}
int main(){
ios::sync_with_stdio(false);
string s1,s2;
while(cin>>s1>>s2){
exkmp(s1,s2);
int len1=s1.length(); int len2=s2.length();
int maxn=-inf;
int maxn_i;
for(int i=;i<len2;i++){
//cout<<extend[i]<<endl;
if(extend[i]+i==len2&&maxn<extend[i]){
maxn=extend[i];
maxn_i=i;
}
}
if(maxn==-inf) cout<<""<<endl;
else{ for(int i=maxn_i;i<maxn_i+maxn;i++)
cout<<s2[i];
cout<<" "<<maxn;
cout<<endl;
}
}
return ;
}

hdu 2594 Simpsons’ Hidden Talents(扩展kmp)的更多相关文章

  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)

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

  3. hdu 2594 Simpsons’ Hidden Talents 【KMP】

    题目链接:http://acm.acmcoder.com/showproblem.php?pid=2594 题意:求最长的串 同一时候是s1的前缀又是s2的后缀.输出子串和长度. 思路:kmp 代码: ...

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

    HDU 2594 Simpsons’ Hidden Talents(辛普森一家的潜在天赋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 3 ...

  5. hdu 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 Problem Description Write a program that, when given strings s1 and s2, fin ...

  7. hdu 2594 Simpsons’ Hidden Talents(KMP入门)

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

  8. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

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

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

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

随机推荐

  1. Web系统大规模并发——秒杀与抢购 秒杀系统优化与预防措施

    电商的秒杀和抢购,对我们来说,都不是一个陌生的东西.然而,从技术的角度来说,这对于Web系统是一个巨大的考验.当一个Web系统,在一秒钟内收到数以万计甚至更多请求时,系统的优化和稳定至关重要.这次我们 ...

  2. Spring是如何校验XML的

    首先来看下xml的一些概念: xml的schema里有namespace,可以给它起个别名.比如常见的spring的namespace: xmlns:mvc="http://www.spri ...

  3. vue 中的slot属性(插槽)的使用

    总结如下: VUE中关于插槽的文档说明很短,语言又写的很凝练,再加上其和方法,数据,计算机等常用选项在使用频率,使用先后上的差别,这就有可能造成初次接触插槽的开发者容易产生“算了吧,回头再学,反正已经 ...

  4. java.util.concurrent.TimeoutException: Idle timeout expired: 300000/300000 ms

    Request idle timed out at 123000 ms. That means there was no activity (read or write) for 123000 ms ...

  5. APIDOC的使用

    工具名称:APIDOCGit地址:https://github.com/apidoc/apidoc 项目地址:http://apidocjs.com/ 样例项目:http://apidocjs.com ...

  6. SpringBoot之通过yaml绑定注入数据

    依赖包: <!--配置文件注解提示包--> <dependency> <groupId>org.springframework.boot</groupId&g ...

  7. easyui datagrid动态修改editor时动态绑定combobox的数据

    需求在 datagrid 编辑框中开启一个combobox  ,但是里面的数据需要开启的时候才会知道,数据会根据其他因数变更 参考原文 :http://blog.csdn.net/donggua369 ...

  8. Lodop打印控件设置表格次页偏移

    Lodop打印控件有很好的自动分页功能,超文本table表格一页装不下,自动分到第二页,第三页……通常表格之前还会有一些内容,比如标题,制表人名称日期什么的杂七杂八的东西,这种东西会占用一定的空间,这 ...

  9. react事件绑定,事件传参,input单向数据绑定

    import React, { Component } from 'react'; class New extends Component { constructor(props){ super(pr ...

  10. Nginx stream如何获取ssl信息并反向代理至上游服务器

    L:116