POJ 2217 Secretary (后缀数组)

题意:

求最长公共子串。

思路:

后缀数组

直接将两个串S和T用一个未出现的字符'\0'连接起来。  求后缀数组。

当两个sa[i] 属于两个串时,更新lcp[i]最大值即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;


const int maxn = 10000 + 10;

int n, k;
int Rank[maxn<<1];
int tmp[maxn<<1];

int sa[maxn<<1], lcp[maxn<<1];

bool cmp(int i,int j){
    if (Rank[i] != Rank[j]) return Rank[i] < Rank[j];
    else {
        int ri = i+k <= n ? Rank[i+k] : -1;
        int rj = j + k <= n ? Rank[j+k] : -1;
        return ri < rj;
    }
}


void build_sa(string s, int* sa){
    for (int i = 0; i <= n; ++i){
        sa[i] = i;
        Rank[i] = i < n ? s[i] : -1;
    }


    for (k = 1; k <= n; k <<= 1){
        sort(sa, sa + n + 1,cmp);
        tmp[sa[0] ] = 0;
        for (int i = 1;  i<= n; ++i){
            tmp[sa[i] ] = tmp[sa[i-1] ] + (cmp(sa[i-1], sa[i]) ? 1 : 0);



        }
        for (int i = 0; i <= n; ++i){
            Rank[i] = tmp[i];

        }

    }

}

void get_height(string s, int *sa, int* lcp){
    for (int i = 0; i <= n; ++i) Rank[sa[i] ] = i;
    int h = 0;
    lcp[0] = 0;
    for (int i = 0; i < n; ++i){
        int j = sa[Rank[i]- 1 ];
        if (h > 0) --h;
        for (; j + h < n && i + h < n; ++h){
            if (s[j+h] != s[i+h])break;

        }
        lcp[Rank[i]-1 ] = h;


    }


}

string s,t;
void init(){
    memset(Rank,0,sizeof Rank);
    memset(lcp,0,sizeof lcp);
    memset(tmp,0,sizeof tmp);
    memset(sa,0,sizeof sa);
}
int main(){
    int T;
    scanf("%d%*c",&T);
    while(T--){
        init();
        getline(cin, s);
        getline(cin, t);
        int len = s.length();
        s = s + '\0' + t;

        n = s.length();
        build_sa(s,sa);
        get_height(s, sa, lcp);
        int ans = 0;

        for (int i = 1; i < n; ++i){
            if (sa[i] < len && sa[i+1] > len || sa[i] > len && sa[i+1] < len){
                ans = max(ans, lcp[i]);
            }
        }
        printf("Nejdelsi spolecny retezec ma delku %d.\n",ans);

    }

    return 0;
}


Secretary
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1390 Accepted: 572

Description

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.

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值