SPOJ220---Relevant Phrases of Annihilation(后缀数组+二分,对后缀分组)

You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.
Input

The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters ‘a’-‘z’, possibly with some additional trailing white space which should be ignored.
Output

For each test case output the length of longest string which appears disjointly at least twice in all of the messages.
Example

Input:
1
4
abbabba
dabddkababa
bacaba
baba

Output:
2

(in the example above, the longest substring which fulfills the requirements is ‘ba’)

简单的后缀数组应用,首先还是老套路,把串都连在一起,中间用没出现过的并且不同的字符隔开,求出后缀数组,然后二分答案,对后缀分组,用3个数组,第一个记录每一组的后缀中,每个字符串中出现的后缀的个数,剩下两个分别存每个字符串里最靠前的和最靠后的后缀的起始位置

/*************************************************************************
    > File Name: SPOJ-220.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年04月07日 星期二 21时21分41秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int pos[122000];
class SuffixArray
{
    public:
        static const int N = 122000;
        int init[N];
        int X[N];
        int Y[N];
        int Rank[N];
        int sa[N];
        int height[N];
        int buc[N];
        int cnt[100];
        int MAXS[100];
        int MINS[100];
        int LOG[N];
        int dp[N][20];
        int size;

        void clear()
        {
            size = 0;
        }

        void insert(int n)
        {
            init[size++] = n;
        }

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

        void getsa(int m = 256) //m一般为最大值+1
        {
            init[size] = 0;
            int l, p, *x = X, *y = Y, n = size + 1;
            for (int i = 0; i < m; ++i)
            {
                buc[i] = 0;
            }
            for (int i = 0; i < n; ++i)
            {
                ++buc[x[i] = init[i]];
            }
            for (int i = 1; i < m; ++i)
            {
                buc[i] += buc[i - 1];
            }
            for (int i = n - 1; i >= 0; --i)
            {
                sa[--buc[x[i]]] = i;
            }
            for (l = 1, p = 1; l <= n && p < n; m = p, l *= 2)
            {
                p = 0;
                for (int i = n - l; i < n; ++i)
                {
                    y[p++] = i;
                }
                for (int i = 0; i < n; ++i)
                {
                    if (sa[i] >= l)
                    {
                        y[p++] = sa[i] - l;
                    }
                }
                for (int i = 0; i < m; ++i)
                {
                    buc[i] = 0;
                }
                for (int i = 0; i < n; ++i)
                {
                    ++buc[x[y[i]]];
                }
                for (int i = 1; i < m; ++i)
                {
                    buc[i] += buc[i - 1];
                }
                for (int i = n - 1; i >= 0; --i)
                {
                    sa[--buc[x[y[i]]]] = y[i];
                }
                int i;

                for (swap(x, y), x[sa[0]] = 0, p = 1, i = 1; i < n; ++i)
                { 
                    x[sa[i]] = cmp(y, sa[i - 1], sa[i], l) ? p - 1 : p++; 
                }
            }
        }

        void getheight()
        {
            int h = 0, n = size;
            for (int i = 0; i <= n; ++i)
            {
                Rank[sa[i]] = i;
            }
            height[0] = 0;
            for (int i = 0; i < n; ++i)
            {
                if (h > 0)
                {
                    --h;
                }
                int j =sa[Rank[i] - 1];
                for (; i + h < n && j + h < n && init[i + h] == init[j + h]; ++h);
                height[Rank[i] - 1] = h;
            }
        }   

        //预处理每一个数字的对数,用于rmq,常数优化
        void initLOG()
        {
            LOG[0] = -1;
            for (int i = 1; i < N; ++i)
            {
                LOG[i] = (i & (i - 1)) ? LOG[i - 1] : LOG[i - 1] + 1;
            }
        }

        void initRMQ()
        {
            initLOG();
            int n = size;
            int limit;
            for (int i = 0; i < n; ++i)
            {
                dp[i][0] = height[i];
            }
            for (int j = 1; j <= LOG[n]; ++j)
            {
                limit = (n - (1 << j));
                for (int i = 0; i <= limit; ++i)
                {
                    dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
                }
            }
        }

        int LCP(int a, int b)
        {
            int t;
            a = Rank[a];
            b = Rank[b];
            if (a > b)
            {
                swap(a, b);
            }
--b;
            t = LOG[b - a + 1];
            return min(dp[a][t], dp[b - (1 << t) + 1][t]);
        }

        bool check(int k, int n)
        {
            memset(cnt, 0, sizeof(cnt));
            memset(MAXS, -1, sizeof(MAXS));
            memset(MINS, inf, sizeof(MINS));
            cnt[pos[sa[1]]] = 1;
            MAXS[pos[sa[1]]] = MINS[pos[sa[1]]] = sa[1];
            for (int i = 1; i < size; ++i)
            {
                if (height[i] >= k)
                {
                    ++cnt[pos[sa[i + 1]]];
                    MAXS[pos[sa[i + 1]]] = max(MAXS[pos[sa[i + 1]]], sa[i + 1]);
                    MINS[pos[sa[i + 1]]] = min(MINS[pos[sa[i + 1]]], sa[i + 1]);
                }
                else
                {
                    bool flag = 1;
                    for (int j = 1; j <= n; ++j)
                    {
                        if (cnt[j] < 2)
                        {
                            flag = 0;
                            break;
                        }
                        else if (MAXS[j] - MINS[j] < k)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if (flag)
                    {
                        return 1;
                    }
                    memset(cnt, 0, sizeof(cnt));
                    memset(MAXS, -1, sizeof(MAXS));
                    memset(MINS, inf, sizeof(MINS));
                    cnt[pos[sa[i + 1]]] = 1;
                    MAXS[pos[sa[i + 1]]] = MINS[pos[sa[i + 1]]] = sa[i + 1];
                }
            }
            return 0;
        }

        void solve(int n)
        {
            int l = 1, r = size, mid, ans = 0;
            while (l <= r)
            {
                mid = (l + r) >> 1;
                if (check(mid, n))
                {
                    ans = mid;
                    l = mid + 1;
                }
                else
                {
                    r = mid - 1;
                }
            }
            printf("%d\n", ans);
        }
}SA;

char buf[10010];

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        SA.clear();
        int len, maxs = 0, cnt = 0;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%s", buf);
            len = strlen(buf);
            for (int j = 0; j < len; ++j)
            {
                maxs = max(maxs, (int)buf[j]);
                SA.insert((int)buf[j]);
                pos[cnt++] = i;
            }
            SA.insert((int)'z' + i);
            pos[cnt++] = 0;
            maxs = max(maxs, (int)'z' + i);
        }
        SA.getsa(maxs + 1);
        SA.getheight();
        SA.solve(n);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值