poj2217 后缀数组

来源:互联网 发布:电视直播点播软件 编辑:程序博客网 时间:2024/06/02 08:48
The basic condition of success of a political party, it is the good Election Programme. PSOS know about it, so they entrust the top secretary Juliet with this task. Because she wanted to make her work easier, she used her charm to talk round her friend Romeo to help her. Romeo is assistent of another political party and he was writing the programme some time ago. While writing the Programme for Juliet, he used some parts of his previous programme. When he gave the finished Programme to Juliet, they recognized that both programmes are too similar and that someone could notice it. They need to determine the longest part of text which is common to both programmes.
Input
At the first line there is a positive integer N stating the number of assignments to follow. Each assignment consists of exactly two lines of text, each of them contains at most 10000 characters. The end-of-line character is not considered to be a part of the text.
Output
Print a single line of text for each assignment. The line should contain the sentence "Nejdelsi spolecny retezec ma delku X." (The longest common part of text has X characters). Replace X with the length of the longest common substring of both texts.
Sample Input
2Tady nejsou zadni mimozemstani.Lide tady take nejsou.Ja do lesa nepojedu.V sobotu pojedeme na vylet.
Sample Output
Nejdelsi spolecny retezec ma delku 7.

Nejdelsi spolecny retezec ma delku 5.

题意:给你两个串,求最大公共子串的长度

思路:将两个串连在一起,用分隔符分割一下,然后求sa数组和height数组,height数组的最大值即为所求

ac代码:

#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#define maxn 200005using namespace std;char str[maxn];int sa[maxn],t1[maxn],t2[maxn],c[maxn],n;void suffix(int m){    int *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;    for(int k=1;k<=n;k<<=1)    {        int p=0;        for(int i=n-k;i<n;i++)y[p++]=i;        for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;        for(int i=0;i<m;i++)c[i]=0;        for(int i=0;i<n;i++)c[x[y[i]]]++;        for(int i=0;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]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;        if(p>=n)break;        m=p;    }}int rank[maxn],height[maxn];void getheight(){    int k=0;    for(int i=0;i<n;i++)rank[sa[i]]=i;    for(int i=0;i<n;i++)    {        if(k)k--;        if(!rank[i])continue;        int j=sa[rank[i]-1];        while(str[i+k]==str[j+k])k++;        height[rank[i]]=k;    }}char tmp[11111];int main(){    int T;    scanf("%d",&T);    getchar();    while(T--)    {        gets(tmp);        int len=strlen(tmp);        int top=0;        for(int i=0;i<=len;i++)        str[top++]=tmp[i];        gets(tmp);        int cnt=0;        while(tmp[cnt]!='\0')str[top++]=tmp[cnt++];        n=top;        suffix(256);        getheight();        int ans=0;        for(int i=1;i<n;i++)        {            if((sa[i-1]<len)!=(sa[i]<len))            ans=max(ans,height[i]);        }        printf("Nejdelsi spolecny retezec ma delku %d.\n",ans);    }    return 0;}

0 0
原创粉丝点击