URAL 1517 Freedom of Choice 后缀数组 入门

1517. Freedom of Choice

Time limit: 2.0 second
Memory limit: 64 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

input output
28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE
THEGREATALBANIA





输入两串n长度的字符串,输出最长共同子串、




#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cctype>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <numeric>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
typedef long double LD;
const double pi=acos(-1.0);
const double eps=1e-6;
#define INF 0x3f3f3f3f
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI > PP;

const int MAXN=200005;
int t1[MAXN], t2[MAXN], c[MAXN];
bool cmp(int *r, int a, int b, int l)
{
    return r[a]==r[b] && r[a+l]==r[b+l];
}
void da(int str[], int sa[], int Rank[], int height[], int n, int m)
{
    int p, *x=t1, *y=t2;
    for(int i=0;i<m;i++)
        c[i]=0;
    for(int i=0;i<n;i++)
        c[x[i]=str[i]]++;
    for(int i=1;i<m;i++)
        c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--)
        sa[--c[x[i]]]=i;
    int j;
    for(j=1;j<=n;j<<=1)
    {
        p=0;
        for(int i=n-j;i<n;i++)
            y[p++]=i;
        for(int i=0;i<n;i++)
            if(sa[i]>=j)
                y[p++]=sa[i]-j;
        for(int i=0;i<m;i++)
            c[i]=0;
        for(int i=0;i<n;i++)
            c[x[y[i]]]++;
        for(int i=1;i<m;i++)
            c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--)
            sa[--c[x[y[i]]]]=y[i];
        swap(x, y);
        p=1;
        x[sa[0]]=0;
        for(int i=1;i<n;i++)
            x[sa[i]]=cmp(y, sa[i-1], sa[i], j)? p-1:p++;
        if(p>=n)
            break;
        m=p;
    }
    int k=0;
    n--;
    for(int i=1;i<=n;i++)
        Rank[sa[i]]=i;
    for(int i=0;i<n;i++)
    {
        if(k)
            k--;
        int j=sa[Rank[i]-1];
        while(str[i+k]==str[j+k])
            k++;
        height[Rank[i]]=k;
    }
}
int Rank[MAXN], height[MAXN];
int RMQ[MAXN];
int mm[MAXN];
int best[20][MAXN];
void initRMQ(int n)
{
    mm[0]=-1;
    for(int i=1;i<=n;i++)
        mm[i]=((i&(i-1))==0)? mm[i-1]+1:mm[i-1];
    for(int i=1;i<=n;i++)
        best[0][i]=i;
    for(int i=1;i<=mm[n];i++)
        for(int j=1;j+(1<<i)-1<=n;j++)
        {
            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=Rank[a];
    b=Rank[b];
    if(a>b)
        swap(a, b);
    return height[askRMQ(a+1, b)];
}
char str[MAXN];
int r[MAXN];
int sa[MAXN];


int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		scanf("%s",str); 
		str[n]='#';
		scanf("%s",str+n+1);
		int len2=strlen(str);
		str[2*n+1]=0;

		for(int i=0;i<=2*n+1;i++)
			r[i]=str[i];

		da(r,sa,Rank,height,2*n+1,128);
		
		int ii,maxx=-1;
		for(int i=2;i<=2*n+1;i++)
		{
			if(height[i]>maxx)
			{
				if((sa[i]>n&&sa[i-1]<n)||(sa[i-1]>n&&sa[i]<n))
				{
					ii=i;
					maxx=height[i];
				}
			}
		}
		for(int i=sa[ii];i<sa[ii]+maxx;i++)
		{
			printf("%c",str[i]);
		} 
		puts("");
	}
	return 0;
}







你可能感兴趣的:(后缀数组)