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

URAL 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.

Example input

28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE

Example output

THEGREATALBANIA

题意

  输出两个已知长度的字符串最长公共子串。

解题思路

  与POJ2774一样,只需要在更新最大值的时候记录下对应的sa[i]即可。

代码

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn= 2e5+50;

int wa[maxn],wb[maxn],wv[maxn],wt[maxn];

int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(char r[],int sa[],int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) wt[i]=0;
    for(i=0; i<n; i++) wt[x[i]=r[i]]++;
    for(i=1; i<m; i++) wt[i]+=wt[i-1];
    for(i=n-1; i>=0; i--) sa[--wt[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++) wt[i]=0;
        for(i=0; i<n; i++) wt[wv[i]]++;
        for(i=1; i<m; i++) wt[i]+=wt[i-1];
        for(i=n-1; i>=0; i--) sa[--wt[wv[i]]]=y[i];
        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 sa[maxn],rankk[maxn],height[maxn],n;

void getheight(char *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rankk[sa[i]]=i;
    for(i=0; i<n; height[rankk[i++]]=k)
        for(k?k--:0,j=sa[rankk[i]-1]; r[i+k]==r[j+k]; k++);
    for(int i=n; i>=1; --i) ++sa[i],rankk[i]=rankk[i-1];
}

char arr[maxn];
int main()
{
#ifdef DEBUG
    freopen("in.txt","r",stdin);
#endif // DEBUG
    int n;
    scanf("%d",&n);
    getchar();
    for(int i=0; i<n; i++) arr[i]=getchar();
    arr[n]='!';
    getchar();
    for(int i=n+1; i<2*n+1; i++) arr[i]=getchar();
    arr[2*n+1]='\0';
    int len=2*n+1;
    da(arr,sa,len+1,200);
    getheight(arr,sa,len);
    int ans=0,pos=0;
    for(int i=2; i<=len; i++)
    {
        if(ans<height[i])
        {
            if(sa[i]<n&&sa[i-1]>n) ans=height[i],pos=sa[i];
            if(sa[i-1]<n&&sa[i]>n) ans=height[i],pos=sa[i-1];
        }
    }
    for(int i=0;i<ans;i++) printf("%c",arr[i+pos-1]);
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值