Background
Before Albanian people could bear with the freedom of speech (this story is fully described in the problem “Freedom of speech”), another freedom - the freedom of choice - came down on them. In the near future, the inhabitants will have to face the first democratic Presidential election in the history of their country.
Outstanding Albanian politicians liberal Mohammed Tahir-ogly and his old rival conservative Ahmed Kasym-bey declared their intention to compete for the high post.
ProblemAccording to democratic traditions, both candidates entertain with digging dirt upon each other to the cheers of their voters’ approval. When occasion offers, each candidate makes an election speech, which is devoted to blaming his opponent for corruption, disrespect for the elders and terrorism affiliation. As a result the speeches of Mohammed and Ahmed have become nearly the same, and now it does not matter for the voters for whom to vote.
The third candidate, a chairman of Albanian socialist party comrade Ktulhu wants to make use of this situation. He has been lazy to write his own election speech, but noticed, that some fragments of the speeches of Mr. Tahir-ogly and Mr. Kasym-bey are completely identical. Then Mr. Ktulhu decided to take the longest identical fragment and use it as his election speech.
Input
The first line contains the integer number N (1 ≤ N ≤ 100000). The second line contains the speech of Mr. Tahir-ogly. The third line contains the speech of Mr. Kasym-bey. Each speech consists of N capital latin letters.
Output
You should output the speech of Mr. Ktulhu. If the problem has several solutions, you should output any of them.
Example
input
28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE
output
THEGREATALBANIA
两个字符串中最长的相同子字符串。将两个字符串拼接起来,求height最大值即可,注意判断两个子字符串要分属于两边,才能更新答案
#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int MAXN=0x3f3f3f3f;
const int sz=1000005;
int n;
int t1[sz],t2[sz],c[sz],sa[sz],tsa[sz],rk[sz],height[sz],r[sz],RMQ[sz],mm[sz];
int best[20][sz];
char str[sz],str1[sz];
bool cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
//hash,判断两个关键字是否完全一样
}
void da(int str[],int sa[],int rk[],int height[],int n,int m){
n++;
int i,j,p,*x=t1,*y=t2;
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[i]=str[i]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
//统计这个字符或数字包括他排在前面的一共有几个
for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
//这个字符所在的排名
//第一次基数排序,求单独字符的排名
for(j=1;j<=n;j<<=1){//排序的子字符串长度以2^n速度增长,所以只要排序logn次
p=0;
for(i=n-j;i<n;i++) y[p++]=i;
//后面j个数没有第二关键字,可以理解为第二关键字为0,排名放在最前
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
//sa[i]-j的第二关键字是j,记录了第二关键字排名
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[y[i]]]++;
//第二关键字排名由前到后的第一关键字的字符,进行统计
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
//保证了取数时第二关键字靠后的依然排在后面
swap(x,y);
p=1;x[sa[0]]=0;
for(i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
//这里的y是之前的x,记录数组编号对应的具体字符
//统计数字种类,缩小范围
if(p>=n) break;
//每个位置都单独对应了一个排名,可以不用排序了
m=p;
}
int k=0;
n--;
for(i=0;i<=n;i++) rk[sa[i]]=i;
for(i=0;i<n;i++){
if(k)k--;
j=sa[rk[i]-1];
while(str[i+k]==str[j+k])k++;
height[rk[i]]=k;
}
}
void initRMQ(int n){
mm[0]=-1;
int i,j;
for(i=1;i<=n;i++){
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
//当i是2^n时i&(i-1)为0,则此时查询的长度上限要增加一倍
}
for(i=1;i<=n;i++) best[0][i]=i;
for(i=1;i<=mm[n];i++){
for(j=1;j+(1<<i)-1<=n;j++){//j是起点,i是长度
int a=best[i-1][j];
int b=best[i-1][j+(1<<(i-1))];
if(RMQ[a]<RMQ[b]) best[i][j]=a;
else best[i][j]=b;
}
}
}
int askRMQ(int a,int b){
int t;
t=mm[b-a+1];
b-=(1<<t)-1;
a=best[t][a];b=best[t][b];
return RMQ[a]<RMQ[b]?a:b;
}
int lcp(int a,int b){
a=rk[a];b=rk[b];
if(a>b) swap(a,b);
return height[askRMQ(a+1,b)];
//a为什么+1?因为hight记录的是i和i-1之间的前缀长度,求a到b就要从a+1开始
}
int main()
{
int i,T;
//freopen("in.txt","r",stdin);
scanf("%d",&n);
int len=n;
scanf("%s",&str1);
for(i=0;i<len;i++) r[i]=str1[i];
scanf("%s",&str);
for(i=0;i<len;i++) r[len+1+i]=str[i];
r[len]=1;
n=2*len+1;
r[n]=0;
da(r,sa,rk,height,n,128);
//for(i=1;i<=n;i++) RMQ[i]=height[i];
//initRMQ(n);
int ans=0,st,tmp;
for(i=2;i<=n;i++){//注意边界,长度有n个,所以排名也排到n
if(height[i]>ans){
if((sa[i]<len&&sa[i-1]>len)||(sa[i]>len&&sa[i-1]<len)){
ans=height[i];
st=sa[i];
}
}
}
/*for(i=1;i<=len;i++){
cout<<sa[i]<<' ';
}
cout<<endl;
for(i=1;i<=len;i++){
cout<<height[i]<<' ';
}
cout<<endl;
sa从0开始
rk从1开始
height从2开始
*/
//r[st+ans]=0;
for(i=st;i<st+ans;i++)
printf("%c",r[i]);
printf("\n");
return 0;
}