poj2774最长公共子序列(后缀数组)

Long Long Message

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.

题目大意:求两个字符串的最长公共子序列

基本的后缀数组的应用,将两个字符串连起来,中间用一个未出现的字符表示,我这里使用的是’$’,连接起来后利用height数组求出最后结果,找到最大的那个height,有个条件就是s[i-1]和s[i]必须满足一定的条件,是来自两个不同的字符串。需要注意的一点是我们需要在s[len]处赋值一个最小的数,这样比较的时候才让最后一个字符的排名为0,这样求height数组时不要将它计算在内了。

这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

const int N=200010;
const int INF=1000000001;
char s2[N],s[N];
int cc[N],x[N],y[N],sa[N],hei[N];
int rank[N],len,l;

void swapp()
{
    int i,k;
    for (i=0;i<N;i++)
    {
        k=x[i];
        x[i]=y[i];
        y[i]=k;
    }
    return;
}

bool cmp(int a,int b,int k)
{
    int ra1=y[a];
    int rb1=y[b];
    int ra2=a+k>=len ? -1:y[a+k];
    int rb2=b+k>=len ? -1:y[b+k];
    return ra1==rb1&&ra2==rb2;
}

void make_sa()
{
    int i,k,p,m;
    m=128;
    for (i=0;i<m;i++) cc[i]=0;
    for (i=0;i<len;i++)  cc[x[i]=s[i]]++; // x[i]=s[i]-'a'+97,++cc[x[i]];
    for (i=0;i<m;i++) cc[i]+=cc[i-1];  
    for (i=len-1;i>=0;i--) sa[--cc[x[i]]]=i;  //i>=0
    for (k=1;k<=len;k<<=1)
    {
        p=0;
        for (i=len-k;i<len;i++) y[p++]=i;  //p++
        for (i=0;i<len;i++)
            if (sa[i]>=k) y[p++]=sa[i]-k;
        for (i=0;i<m;i++) cc[i]=0;
        for (i=0;i<len;i++) ++cc[x[y[i]]];
        for (i=1;i<m;i++) cc[i]+=cc[i-1]; //i=1 to m
        for (i=len-1;i>=0;i--) sa[--cc[x[y[i]]]]=y[i];  //i>=0
        swapp();
        p=1;  //这里不能直接写m=1,因为这个wa挺了... 
        x[sa[0]]=0;
        for (i=1;i<len;i++)
            x[sa[i]]=cmp(sa[i],sa[i-1],k) ? p-1:p++;
        if (p>=len) break;
        m=p;  //把p的值赋给m 
    }
}

void make_hei()
{
    int i;
    for (i=0;i<len;i++) rank[sa[i]]=i;  
    hei[0]=0;
    int k=0;
    for (i=0;i<len;i++)  
    {
        if (!rank[i]) continue;
        int j=sa[rank[i]-1];
        if (k) k--;
        while (s[i+k]==s[j+k])   //防止死循环 
        k++;
        hei[rank[i]]=k;
    }
    return;
}

void solve()
{
    int i,j;
    int ans=-INF;
    for (i=2;i<=len;i++)  //i=2 to len
    {
        int f1=sa[i];
        int f2=sa[i-1];
        if ((f1<l&&f2>l)||(f1>l&&f2<l))
           ans=max(ans,hei[i]);
    }
    printf("%d",ans);
    return;
}

int main()
{
    scanf("%s",&s);
    scanf("%s",&s2);
    l=strlen(s);
    s[l]='$';
    strcat(s,s2);
    len=strlen(s);
    s[len]=0;  ///
    len++;  ///
    make_sa();
    make_hei();
    solve();
    return 0;
 }
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673643.html