1517. Freedom of Choice
Time Limit: 2.0 second
Memory Limit: 32 MB
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.
Problem
According 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.
Sample
inputoutput 28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE
THEGREATALBANIA
Problem Author: Ilya Grebnov, Nikita Rybak, Dmitry Kovalioff
Problem Source: Timus Top Coders: Third Challenge
Tags:
string algorithms
(
)
Printable version
Statistics
Discuss
Submit solution
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=200010;
char str[maxn];
int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];
int cmp(int* r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
//n为字符串长度,m为字符的取值范围,r为字符串。后面的j为每次排序时子串的长度
void DA(int* r,int* sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
///对R中长度为1的子串进行基数排序
for(i=0; i<m; i++)wn[i]=0;
for(i=0; i<n; i++)wn[x[i]=r[i]]++;
for(i=1; i<m; i++)wn[i]+=wn[i-1];
for(i=n-1; i>=0; i--)sa[--wn[x[i]]]=i;
for(j=1,p=1; p<n; j*=2,m=p)
{
//利用了上一次基数排序的结果,对待排序的子串的第二关键字进行了一次高效地基数排序
for(p=0,i=n-j; i<n; i++)y[p++]=i;
for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
///基数排序
for(i=0; i<n; i++)wv[i]=x[y[i]];
for(i=0; i<m; i++)wn[i]=0;
for(i=0; i<n; i++)wn[wv[i]]++;
for(i=1; i<m; i++)wn[i]+=wn[i-1];
for(i=n-1; i>=0; i--)sa[--wn[wv[i]]]=y[i];
///当p=n的时候,说明所有串都已经排好序了
///在第一次排序以后,_rank数组中的最大值小于p,所以让m=p
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return;
}
int _rank[maxn],height[maxn];
void calheight(int* r,int* sa,int n)
{
int i,j,k=0;
for(i=1; i<=n; i++)_rank[sa[i]]=i;
for(i=0; i<n; height[_rank[i++]]=k)
for(k?k--:0,j=sa[_rank[i]-1]; r[i+k]==r[j+k]; k++);
return;
}
char str1[maxn],str2[maxn];
int main()
{
int N;
while(scanf("%d",&N)!=EOF)
{
scanf("%s%s",str1,str2);
for(int i=0; i<N; i++) a[i]=(int)str1[i];
a[N]=1;//notice 1
for(int i=0; i<N; i++) a[N+1+i]=(int)str2[i];
a[N+N+1]=24;
int n=N+N;
DA(a,sa,n+1,256);
calheight(a,sa,n);
int rmax=1;
for(int i=1; i<=n; i++)
{
if(((sa[i]<N&&sa[i-1]>N)||(sa[i]>N&&sa[i-1]<N))&&height[i]>height[rmax])
rmax=i;
}
for(int p=sa[rmax-1],i=0;i<height[rmax];i++,p++) printf("%c",a[p]);
printf("/n");
}
return 0;
}