URAL 1517. Freedom of choice (后缀数组)

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.

 

Example input


28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE

Example output


THEGREATALBANIA
给你两个等长的串,求他们的最长公共连续子串.

这里要求我们输出这个最长公共串了.(题目中要求有多解就输出所有解,但是其实只有1个解.)

对于输出解,我们只要记录过程中的每一个可以作为解的后缀编号以及长度即可一一输出.

AC代码:
 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int maxn=200000+1000;
int len1,len2;//分别为串1和串2的长度
int ans,pos;//ans是公共字串的长度,pos是起始位置
struct SuffixArray
{
    char s[maxn];
    int sa[maxn],rank[maxn],height[maxn];
    int t1[maxn],t2[maxn],c[maxn],n;
    void build_sa(int m)
    {
        int i,*x=t1,*y=t2;
        for(i=0; i<m; i++)
            c[i]=0;
        for(i=0; i<n; i++)
            c[x[i]=s[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(int k=1; k<=n; k<<=1)
        {
            int p=0;
            for(i=n-k; i<n; i++)
                y[p++]=i;
            for(i=0; i<n; i++)
                if(sa[i]>=k)
                    y[p++]=sa[i]-k;
            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]]= y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++;
            if(p>=n)
                break;
            m=p;
        }
    }
    void build_height()
    {
        int i,j,k=0;
        for(i=0; i<n; i++)
            rank[sa[i]]=i;
        for(i=0; i<n; i++)
        {
            if(k)
                k--;
            j=sa[rank[i]-1];
            while(s[i+k]==s[j+k])
                k++;
            height[rank[i]]=k;
        }
    }
    void solve()
    {
        ans=0;//最长字串的长度
        for(int i=2; i<n; i++)
        {
            int a1=sa[i-1],a2=sa[i];
            if(a1>a2)
                swap(a1,a2);
            if(a1>=0&&a1<=len1-1&&a2>=len1+1&&a2<=len1+len2)
            {
                if(ans<height[i])
                {
                    ans = height[i];
                    pos=a1;
                }
            }

        }
    }
} sa;
int main()
{
    scanf("%d",&len1);
    len2=len1;
    scanf("%s",sa.s);
    sa.s[len1]=1;
    scanf("%s",sa.s+len1+1);
    sa.n=len1+len2+2;
    sa.s[len1+len2+1]=0;
    sa.build_sa(128);
    sa.build_height();
    sa.solve();
    sa.s[pos+ans]=0;
    printf("%s\n",sa.s+pos);
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值