POJ 2774

http://www.elijahqi.win/2017/07/12/poj-2774/
Long Long Message
Time Limit: 4000MS Memory Limit: 131072K
Total Submissions: 30613 Accepted: 12387
Case Time Limit: 1000MS
Description

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother.

The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out:

  1. All characters in messages are lowercase Latin letters, without punctuations and spaces.
  2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long.
  3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer.
    E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc.
  4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different.

You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat.

Background:
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be.

Why ask you to write a program? There are four resions:
1. The little cat is so busy these days with physics lessons;
2. The little cat wants to keep what he said to his mother seceret;
3. POJ is such a great Online Judge;
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :(

Input

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.
Output

A single line with a single integer number – what is the maximum length of the original text written by the little cat.
Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
Sample Output

27
Source

POJ Monthly–2006.03.26,Zeyuan Zhu,”Dedicate to my great beloved mother.”

这题比较水,用来当作后缀数组的复习题 但是我查错查了一天…原因是 Int数组定义成char了..
题目中似乎是多组数据 但是不写多组也是可以得吧,也是可以过的
首先复习几个概念
后缀数组是把一个字符串所有的后缀按照字典顺序进行排序
这里我们的rank[]针对第一关键字,rank1[]针对第二关键字 再通过两个关键字的排序更新新的rank 倍增求最终的rank
rank[第几个后缀]=第几名
count[关键字]=个数 tmp表示根据第二关键字排序 tmp[排行]=第一关键字序号
倍增算法是基于双关键字基数排序的算法,所以我们先来了解基数排序。
一个简单的例子:排序1 5 3 6 4 3 5 2 3 1
统计可得共有2个1,1个2,3个3,1个4,2个5,1个6
按顺序输出,得1 1 2 3 3 3 4 5 5 6
复杂度O(n+数值范围)。
这是单关键字的基数排序(又名记数排序),双关键字则应先排第二关键字再排第一关键字,并且要注意的是排第一关键字的时候应当保持第二关键字顺序不变。
例如:排序(1,1)(1,2)(3,1)(4,2)(4,3)(2,3)(2,1)(5,3)(1,5)(3,5)
先按照第二关键字排序,得
(1,1)(3,1)(2,1)(1,2)(4,2)(4,3)(2,3)(5,3)(1,5)(3,5)
然后按照第一关键字排序,得
(1,1)(1,2)(1,5)(2,1)(2,3)(3,1)(3,5)(4,2)(4,3)(5,3)
(1)height 数组:定义height[i]=suffix(SA[i-1])和suffix(SA[i])的最长公共前缀,也就是排名相邻的两个后缀的最长公共前缀的长度 。
(2)h[i]=height[rank[i]],也就是suffix(i)和排序后在它前一名的后缀的最长公共前缀的长度。
(3)函数lcp(u,v)=max{i|u=v},也就是从头开始顺次比较u和v的对应字符,对应字符持续相等的最大位置,称为这两个字符串u,v的最长公共前缀的长度。
(4)LCP(i,j):对正整数i,j 定义LCP(i,j)=lcp(Suffix(SA[i]),Suffix(SA[j]),其中i,j 均为1至n的整数。LCP(i,j)也就是后缀数组中第i个和第j个后缀的最长公共前缀的长度。

#include<cstdio>
#include<cstring>
#define N 110000
char str1[N],str2[N],a[N<<1];
int rank[N<<2],rank1[N<<2],sa[N<<1],count[N<<1],st[256],k,tmp[N<<1],h[N<<1],height[N<<1];
inline int min(int x,int y){
    return x<y?x:y;
}
int main(){
    //freopen("poj2774.in","r",stdin);
    //freopen("poj2774.out","w",stdout);
    while (~scanf("%s%s",str1,str2)){
        int n1=strlen(str1),n2=strlen(str2);str1[n1]='&';
        for (int i=n1+1;i<n1+n2+1;++i) str1[i]=str2[i-n1-1];
        for (int i=n1+n2+1;i>=1;--i) a[i]=str1[i-1];
        int n=n1+n2+1;//a[0]='0';
        //printf("%s",a);
        memset(st,0,sizeof(st));
        memset(rank,0,sizeof(rank));
        memset(rank1,0,sizeof(rank1)); 
        for (int i=1;i<=n;++i) st[a[i]]=1;
        for (int i=1;i<=255;++i) st[i]+=st[i-1];
        for (int i=n;i>=1;--i) rank[i]=st[a[i]];
        int bl[N<<1];
        for (int i=1;i<=n1;++i) bl[i]=1;
        for (int i=n1+1;i<=n;++i) bl[i]=2; 
        k=0;
        for (int p=1;k!=n;p<<=1){
            memset(count,0,sizeof(count));
            for (int i=1;i<=n;++i) count[rank[i+p]]++;
            for (int i=1;i<=n;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) tmp[count[rank[i+p]]--]=i;
            memset(count,0,sizeof(count));
            for (int i=1;i<=n;++i) count[rank[tmp[i]]]++;
            for (int i=1;i<=n;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) sa[count[rank[tmp[i]]]--]=tmp[i];
            memcpy(rank1,rank,sizeof(rank)/2);
            rank[sa[1]]=k=1;
            for (int i=2;i<=n;++i){
                if (rank1[sa[i]]!=rank1[sa[i-1]]||rank1[sa[i]+p]!=rank1[sa[i-1]+p]) ++k;
                rank[sa[i]]=k;
            }
        }
        //for (int i=1;i<=n;++i) printf("%d ",rank[i]); 
    /*  for (int i=1;i<=n;++i) {
            for (int j=sa[i];j<=n;++j) printf("%c",a[j]);
            printf("\n");
        }*/
        k=0;
        for (int i=1;i<=n;++i){
            if (rank[i]==1) {k=0;continue;}
            k=k==0?0:k-1;
            while (a[i+k]==a[sa[rank[i]-1]+k]) k++;
            height[rank[i]]=k;  
        }
        //for (int i=1;i<=n;++i) printf("%d ",height[i]);
        int max;
        for (int i=1;i<=n;++i){
            if (height[i]>max&&bl[sa[i]]!=bl[sa[i-1]]) max=height[i];
        }
        printf("%d",max);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值