题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594
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
题目大意:
求两个串的第一个串的前缀和第二个串的后缀的最长匹配
写法一:在中间添加一个大写字母,将两个串连在一起,上Get_next(),保证后面的后缀不会匹配到前面去
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;
string s1,s2,mo;
int Next[N];
void Get_next()
{
int len = mo.length();
int i,j;
j = Next[0] = -1;
i=0;
while(i<len)
{
while(j!=-1&&mo[i]!=mo[j])
j = Next[j];
Next[++i] = ++j;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
while(cin>>s1>>s2)
{
mo = s1+'A'+s2;
int len = mo.length();
Get_next();
int t = Next[len];
if(t==-1||t==0)
cout<<"0"<<endl;
else
{
for(int i = 0;i<t;i++)
cout<<mo[i];
cout<<" "<<t<<endl;
}
}
return 0;
}
写法二:直接把两个字符串连接起来,上Get_next(),但是有点问题就是,直接用next数组的话会把后缀延伸到第一串当中,或者把前缀延伸到第二串当中,这样就不符合题意了。所以要继续next下去,直到长度小等于短串的长度,就行了。
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;
string s1,s2,mo;
int Next[N];
void Get_next()
{
int len = mo.length();
int i,j;
j = Next[0] = -1;
i=0;
while(i<len)
{
while(j!=-1&&mo[i]!=mo[j])
j = Next[j];
Next[++i] = ++j;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
while(cin>>s1>>s2)
{
mo = s1+s2;
int len1 = s1.length();
int len2 = s2.length();
int len = mo.length();
Get_next();
int t = Next[len];
while(t>len1||t>len2)
t = Next[t];
if(t==-1||t==0)
cout<<"0"<<endl;
else
{
for(int i = 0;i<t;i++)
cout<<mo[i];
cout<<" "<<t<<endl;
}
}
return 0;
}
写法三:将第一个串当成模式串,第二个串去匹配,一直匹配到最后求出来的j就是答案
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;
string str,mo;
int Next[N];
void Get_next()
{
int len = mo.length();
int i,j;
j = Next[0] = -1;
i=0;
while(i<len)
{
while(j!=-1&&mo[i]!=mo[j])
j = Next[j];
Next[++i] = ++j;
}
}
void KMP()
{
int i=0,j=0;
int len = str.length();
int molen = mo.length();
while(i<len)
{
while(j!=-1&&mo[j]!=str[i])
j = Next[j];
++i;
++j;
}
if(j==-1||j==0)
cout<<"0"<<endl;
else
{
for(int k=0;k<j;k++)
cout<<mo[k];
cout<<" "<<j<<endl;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
while(cin>>mo>>str)
{
Get_next();
KMP();
}
return 0;
}
写法四:扩展KMP
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e6+5;
//假设s为文本串(长度为n),T为模式串(长度为m)
char s[N],T[N];
int n,m;
int Next[N]; //Next[i]代表T[i~m-1]与T[0~m-1]最长公共前缀
int ex[N]; //extend数组,代表s[i~n-1]与T[0~m-1]最长公共前缀
//注意到,如果有一个位置extend[i]=m,则表示T在S中出现,而且是在位置i出现,这就是标准的KMP问题
void Get_Next(){
Next[0] = m;
int j=0;
while(j+1<m&&T[j]==T[j+1]) //计算Next[1]
j++;
Next[1] = j;
int po=1; //设po为答案伸得最远的后缀的下标
for(int i=2;i<m;i++){
int p = Next[po] + po - 1; //已匹配最大位置
int L = Next[i-po]; //
if(i+L<p+1)
Next[i] = L;
else{
j = max(0,p-i+1);
while(i+j<m && T[i+j] == T[j])
j++;
Next[i] = j;
po = i;
}
}
}
void EX_KMP(){
Get_Next();
int j=0;
while(j<n&&j<m&&s[j]==T[j])
j++;
ex[0] = j;
int po = 0;
for(int i=1;i<n;i++){
int p = ex[po]+po-1;
int L = ex[i-po];
if(i+L<p+1)
ex[i] = L;
else{
j = max(0,p-i+1);
while(i+j<n&&j<m&&s[i+j]==T[j])
j++;
ex[i] = j;
po = i;
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
while(~scanf(" %s %s",&T,&s)){
n = strlen(s);
m = strlen(T);
EX_KMP();
int k = 0;
int len = 0;
for(int i=0;i<n;i++){
if(i+ex[i] == n){
len = ex[i];
break;
}
}
for(int i=0;i<len;i++){
printf("%c",T[i]);
}
if(len!=0)
printf(" ");
printf("%d\n",len);
}
return 0;
}