spoj 220 Relevant Phrases of Annihilation(每个字符串至少出现两次且不重叠的最长子串)

来源:互联网 发布:javascript格式化xml 编辑:程序博客网 时间:2024/04/29 23:53

Relevant Phrases of Annihilation
Time Limit: 9000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

Description

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: 14abbabbadabddkabababacabababa Output: 2

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


题意:给定n 个字符串,求在每个字符串中至少出现两次且不重叠的最长子串。
思路:也是先将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组。然后二分答案,再将后缀分组。判断的时候,要看是否有一组后缀在每个原来的字符串中至少出现两次,并且在每个原来的字符串中,后缀的起始位置的最大值与最小值之差是否不小于当前答案(判断能否做到不重叠,如果题目中没有不重叠的要求,那么不用做此判断)。这个做法的时间复杂度为O(nlogn)。


AC代码:

#include <cstdio>#include <cstring>#include <iostream>#include <cmath>#include <algorithm>#include <queue>#define ll long longusing namespace std;const int maxn = 200005;const int INF = 1e9;int s[maxn];int sa[maxn], t[maxn], t2[maxn], c[maxn], n, k;int rank[maxn], height[maxn];void build_sa(int n, int m){    int i, *x = t, *y = t2;    for(i = 0; i < m; i++) c[i] = 0;    for(i = 0; i < n; i++) c[x[i] = s[i]]++;    for(i = 1; i < m; i++) c[i] += c[i - 1];    for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;    for(int k = 1; k <= n; k <<= 1)    {        int p = 0;        for(i = n - k; i < n; i++) y[p++] = i;        for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;        for(i = 0; i < m; i++) c[i] = 0;        for(i = 0; i < n; i++) c[x[y[i]]]++;        for(i = 1; i < m; i++) c[i] += c[i - 1];        for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];        swap(x, y);        p = 1; x[sa[0]] = 0;        for(i = 1; i < n; i++)        x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k] ? p - 1 : p++;        if(p >= n) break;        m = p;    }}void getHeight(){    int i, j, k = 0;    for(i = 1; i <= n; i++) rank[sa[i]] = i;    for(i = 0; i < n; i++)    {        if(k) k--;        j = sa[rank[i] - 1];        while(s[i + k] == s[j + k]) k++;        height[rank[i]] = k;    }}int rmq[maxn][20];void initRMQ(){    for(int i = 1; i <= n; i++) rmq[i][0] = rmq[i][0] = height[i];    for(int k = 1; (1 << k) <= n; k++)    for(int i = 1; i + (1 << k) - 1 <= n; i++)    {        rmq[i][k]=min(rmq[i][k-1],rmq[i+(1<<(k-1))][k-1]);    }}int lcp(int a,int b){    a = rank[a], b = rank[b];    if(a > b) swap(a, b);    a++;    int k = log(b - a + 1.0) / log(2.0);    return min(rmq[a][k], rmq[b - (1 << k) + 1][k]);}char str[15][10005];int id[maxn], mx[15], mi[15];bool ok(int d){    for(int i = 0; i < k; i++)    {        mx[i] = 0;        mi[i] = INF;    }    for(int i = 1; i <= n; i++)    {        if(height[i] < d)        {            for(int j = 0; j < k; j++)            {                mx[j] = 0;                mi[j] = INF;            }            mx[id[sa[i]]] = mi[id[sa[i]]] = sa[i];        }        else        {            mx[id[sa[i]]] = max(mx[id[sa[i]]], sa[i]);            mi[id[sa[i]]] = min(mi[id[sa[i]]], sa[i]);            mx[id[sa[i - 1]]] = max(mx[id[sa[i - 1]]], sa[i - 1]);            mi[id[sa[i - 1]]] = min(mi[id[sa[i - 1]]], sa[i - 1]);            int j;            for(j = 0; j < k; j++)            if(mx[j] - mi[j] < d) break;            if(j == k) return true;        }    }    return false;}int main(){    int t;    scanf("%d", &t);    while(t--)    {        scanf("%d", &k);        for(int i = 0; i < k; i++)        scanf("%s", str[i]);        n = 0;        for(int i = 0 ; i < k; i++)        {            for(int j = 0; str[i][j]; j++)            {                int ch = str[i][j] - 'a' + 1;                s[n] = ch;                id[n++] = i;            }            s[n] = 27 + i;            id[n++] = i;        }        n--;        s[n] = 0;        build_sa(n + 1, 256);        getHeight();        int low = 0, high = 10000, ans = 0;        while(low <= high)        {            int mid = (low + high) >> 1;            if(ok(mid))            {                ans = mid;                low = mid + 1;            }            else high = mid - 1;        }        printf("%d\n", ans);    }    return 0;}

0 0
原创粉丝点击