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.
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.
The lengths of s1 and s2 will be at most 50000.
Sample Input
clinton
homer
riemann
marjorie
Sample Output
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 ;
}