poj2217 后缀数组

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
2
Tady 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 200005 using 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值