SPOJ 220 PHRASES Relevant Phrases of Annihilation 后缀数组

题目大意:

就是现在给出T组测试数据(T <= 10) 每一组有n(n <= 10)个字符串, 每个字符串长度L满足(2 <= L <= 10000)

对于每一组, 求在所给的所有串都出现了至少两次的子串的最大长度, 每个串中出现的两次不能有重叠部分


大致思路:

很明显的后缀数组利用height数组进行分组的题

刚开始的时候没有考虑出现的两次不重合(没仔细读题以为可以...) WA了2发= =

具体细节见代码:


代码如下:


Result  :  Accepted     Memory  :  6758 KB     Time  :  50 ms

/*
 * Author: Gatevin
 * Created Time:  2015/2/9 14:14:28
 * File Name: Iris_Freyja.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 102333

/*
 * Doubling Algorithm 求后缀数组
 */
int wa[maxn], wb[maxn], wv[maxn], Ws[maxn];

int cmp(int *r, int a, int b, int l)
{
    return r[a] == r[b] && r[a + l] == r[b + l];
}

void da(int *r, int *sa, int n, int m)
{
    int *x = wa, *y = wb, *t, i, j, p;
    for(i = 0; i < m; i++) Ws[i] = 0;
    for(i = 0; i < n; i++) Ws[x[i] = r[i]]++;
    for(i = 1; i < m; i++) Ws[i] += Ws[i - 1];
    for(i = n - 1; i >= 0; i--) sa[--Ws[x[i]]] = i;
    for(j = 1, p = 1; p < n; j *= 2, m = p)
    {
        for(p = 0, i = n - j; i < n; i++) y[p++] = i;
        for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
        for(i = 0; i < n; i++) wv[i] = x[y[i]];
        for(i = 0; i < m; i++) Ws[i] = 0;
        for(i = 0; i < n; i++) Ws[wv[i]]++;
        for(i = 1; i < m; i++) Ws[i] += Ws[i - 1];
        for(i = n - 1; i >= 0; i--) sa[--Ws[wv[i]]] = y[i];
        for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
            x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
    }
    return;
}

int rank[maxn], height[maxn];
void calheight(int *r, int *sa, int n)
{
    int i, j, k = 0;
    for(i = 1; i <= n; i++) rank[sa[i]] = i;
    for(i = 0; i < n; height[rank[i++]] = k)
        for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);
    return;
}

char in[12333];
int s[maxn], sa[maxn], belong[maxn];
int n;

bool check(int mid, int N)//测试长度为mid的满足条件的子串是否存在
{
    int appear[12];
    memset(appear, 0, sizeof(appear));
    int cnt = 0;
    int Min[12], Max[12];
    bool con[12];
    for(int i = 0; i <= n; i++) Min[i] = 999999;
    memset(Max, 0, sizeof(Max));
    memset(con, 0, sizeof(con));
    for(int i = 1; i <= N; i++)
        if(height[i] >= mid)
        {
            appear[belong[sa[i]]] += 1;//记录每一组中各个子串出现的位置
            Min[belong[sa[i]]] = min(Min[belong[sa[i]]], sa[i]);//记录出自各个输入串的后缀的起始位置的最前和最后位置
            Max[belong[sa[i]]] = max(Max[belong[sa[i]]], sa[i]);
            if(appear[belong[sa[i]]] >= 2 && Max[belong[sa[i]]] - Min[belong[sa[i]]] >= mid && !con[belong[sa[i]]])
                cnt++, con[belong[sa[i]]] = 1;//con数组表示输入的n个串分别是否已经计入
            if(cnt == n) return true;//出现两次不重合的位置的串的数量达到n,满足条件
        }
        else
        {
            memset(appear, 0, sizeof(appear));
            memset(con, 0, sizeof(con));
            for(int j = 0; j <= n; j++) Min[j] = 999999;
            memset(Max, 0, sizeof(Max));
            cnt = 0;
            appear[belong[sa[i]]] = 1;
            Min[belong[sa[i]]] = min(Min[belong[sa[i]]], sa[i]);
            Max[belong[sa[i]]] = max(Max[belong[sa[i]]], sa[i]);
        }
    return false;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t-->0)
    {
        scanf("%d", &n);
        int N = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%s", in);
            int len = strlen(in);
            for(int j = 0; j < len; j++)
            {
                belong[N] = i;
                s[N++] = in[j] - 'a' + 1;
            }   
            s[N++] = 26 + i;
        }
        N--;
        s[N] = 0;
        da(s, sa, N + 1, 40);
        calheight(s, sa, N);
        int L = 1, R = 10000, ans = 0, mid;
        /*
         * 注意到如果存在长度为len的满足条件的子串,那么对于长度小于len的也一定满足条件
         * 所以长度len是否可行具有单调性可以用二分查找到长度的最大值
         */
        while(L <= R)
        {
            mid = (L + R) >> 1;
            if(check(mid, N))
            {
                ans = mid;
                L = mid + 1;
            }
            else
                R = mid - 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(后缀数组,spoj,220,PHRASES)