码迷,mamicode.com
首页 > 其他好文 > 详细

[URAL-1517][求两个字符串的最长公共子串]

时间:2017-02-09 23:03:20      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:describe   should   error   get   ural   message   return   scanf   val   

Freedom of Choice

 URAL - 1517 

 

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
inputoutput
28
VOTEFORTHEGREATALBANIAFORYOU
CHOOSETHEGREATALBANIANFUTURE
THEGREATALBANIA

题意:

  给出两个字符串,求 最长公共子串 

分析:

  同POJ2774 Long Long Message

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=2e5+10;
int n,len,id,ans,maxx,sa[N],tsa[N],rank[N],trank[N],h[N],c[N];
char s[N];
void DA(){
    int p;
    memset(c,0,sizeof c);maxx=256;
    for(int i=1;i<=len;i++) c[rank[i]=s[i]]++;
    for(int i=2;i<=maxx;i++) c[i]+=c[i-1];
    for(int i=len;i;i--) sa[c[rank[i]]--]=i;
    trank[sa[1]]=p=1;
    for(int i=2;i<=len;i++){
        if(rank[sa[i]]!=rank[sa[i-1]]) p++;
        trank[sa[i]]=p;
    }
    for(int i=1;i<=len;i++) rank[i]=trank[i];
    for(int k=1;p<len;k<<=1,maxx=p){
        p=0;
        for(int i=len-k+1;i<=len;i++) tsa[++p]=i;
        for(int i=1;i<=len;i++) if(sa[i]>k) tsa[++p]=sa[i]-k;
        memset(c,0,sizeof c);
        for(int i=1;i<=len;i++) trank[i]=rank[tsa[i]];
        for(int i=1;i<=len;i++) c[trank[i]]++;
        for(int i=2;i<=maxx;i++) c[i]+=c[i-1];
        for(int i=len;i;i--) sa[c[trank[i]]--]=tsa[i];
        trank[sa[1]]=p=1;
        for(int i=2;i<=len;i++){
            if(rank[sa[i]]!=rank[sa[i-1]]||rank[sa[i]+k]!=rank[sa[i-1]+k]) p++;
            trank[sa[i]]=p;
        }
        for(int i=1;i<=len;i++) rank[i]=trank[i];
    }
    for(int i=1,k=0;i<=len;i++){
        int j=sa[rank[i]-1];
        while(s[i+k]==s[j+k]) k++;
        h[rank[i]]=k;if(k>0) k--;
    }
}
int main(){
    scanf("%*d");
    scanf("%s",s+1);len=strlen(s+1);s[++len]=$;n=len+1;
    scanf("%s",s+n);len=strlen(s+1);//s[++len]=‘#‘;
    DA();
    for(int i=2;i<=len;i++){
        if((sa[i]>=n&&sa[i-1]<n)||(sa[i-1]>=n&&sa[i]<n)){
            if(ans<h[i]){
                ans=h[i];
                id=sa[i];
            }
        }
    }
    s[id+ans]=0;
    puts(s+id);
    return 0;
}

 

[URAL-1517][求两个字符串的最长公共子串]

标签:describe   should   error   get   ural   message   return   scanf   val   

原文地址:http://www.cnblogs.com/shenben/p/6384171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!