K - 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')

题目大意:求在每个字符串中出现至少两次的最长的子串。

解题思路: 注意到这么几个关键点:最长,至少两次,每个字符串。首先对于最长这个条件,我们可以想到二分答案,然后利用后缀数组所求得的三个数组判断是否满足条件。其次是出现两次,每次出现这个条件的时候,我们就应该要想到这是最大值最小值可以处理的,将出现在同一个字符串中的每个相同字符串的起始位置保存下来,如果最小值和最大值的差距超过二分长度L,则表明在这个字符串中这个条件是可行的。将所有的字符串通过拼接符连接在一起,做一遍后缀数组,现在我们根据height数组将大于二分长度的前后后缀分为一组,每当存在分组中的后缀数量大于2*n,就说明这个字符串有可能是我们需要的答案,那么对它进行检验,检验所有可能合法的字符串就可以完成对一个长度的判断了。

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 1e6 + 10 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const int seed = 133 ;

int sa[Maxn] ;
int t1[Maxn], t2[Maxn], c[Maxn] ;
int Rank[Maxn], height[Maxn] ;

void getSa(int s[], int n, int m){
    int i, j, p, *x = t1, *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(j = 1; j <= n; j <<= 1){
        p = 0 ;
        for(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 < 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] + j] == y[sa[i] + j] ? p-1 : p++ ;
        if(p >= n) break ;
        m = p ;
    }
}

//void getHeight(int s[],int n){
//    int i, j, k = 0 ;
//    for(i = 0; i <= n; i++) Rank[sa[i]] = i ;
//    for(i = 0; i < n; i++){
//        if(k) k-- ;
//        else k = 0 ;
//        j = sa[Rank[i] - 1] ;
//        while(s[i + k] == s[j + k]) k++ ;
//        height[Rank[i]] = k ;
//    }
//}

void getHeight(int s[],int n){
    int k = 0, j ;
    for (int i = 1 ;i <= n; i++) Rank[sa[i]] = i;
    for (int i = 0; i < n; height[Rank[i++]] = k){
        for (k ? k-- : 0, j = sa[Rank[i] - 1]; s[i + k] == s[j + k]; k++);
    }
}


char str[Maxn] ;
int s[Maxn], n, lens ;
int pre[Maxn], len[Maxn] ;
int Max[Maxn], Min[Maxn] ;
vector < int > ve[Maxn] ;

bool check(int mid){
    int index = -1 ;
    for (int i = 1; i <= lens; i++){
        if (height[i] < mid) ve[++index].clear() ;
        ve[index].push_back(i) ;
    }

    for (int i = 0; i <= index; i++){
        if (ve[i].size() >= 2 * n){
            memset(Max, -1, sizeof Max) ;
            memset(Min, -1, sizeof Min) ;
            for (int j = 0; j < ve[i].size(); j++){
                int k = ve[i][j] ;
                int id = upper_bound(pre, pre + n + 1, sa[k]) - pre - 1 ;
                Min[id] = (Min[id] == -1 ? sa[k] : min(Min[id], sa[k])) ;
                Max[id] = (Max[id] == -1 ? sa[k] : max(Max[id], sa[k])) ;
            }
            int flag = 0 ;
            for (int i = 0; i < n; i++){
                if (Min[i] == -1 || (Max[i] - Min[i] < mid)){
                    flag = 1 ;
                    break ;
                }
            }
            if (flag == 0) return 1 ;
        }
    }
    return 0 ;
}

int main(){
    int T ;
    cin >> T ;
    while (T--){
        cin >> n ;
        lens = 0 ;
        int asc = 200 ;
        for (int i = 0; i < n; i++){
            cin >> str ;
            len[i] = strlen(str) ;
            for (int j = 0; j < len[i]; j++) s[lens++] = str[j] ;
            s[lens++] = asc++ ;
        }
        asc = 0;
        s[lens] = 0 ;
        for (int i = 0; i <= n; i++){
            pre[i] = asc ;
            if (i < n) asc += (i == 0 ? len[i] : len[i] + 1) ;
        }
        getSa(s, lens + 1, 310) ;
        getHeight(s, lens) ;
//        for (int i = 1; i <= lens; i++) {
//            cout << sa[i] << " " ;
//        }
//        cout << endl ;
//        for (int i = 1; i <= lens; i++){
//            cout << height[i] << " " ;
//        }
//        cout << endl ;
//        for (int i = 1; i <= lens; i++){
//            cout << Rank[i] << " " ;
//        }
//        cout << endl ;
        int l = 1, r = 10000, ans = 0 ;
        while (l <= r){
            int mid = (l + r) >> 1 ;
            if (check(mid)) {
                ans = mid ;
                l = mid + 1 ;
            }
            else r = mid - 1 ;
        }
        cout << ans << endl ;
    }
    return 0;
}

//14 16 23 28 0 3 9 21 6 18 25 30 15 27 2 20 5 17 24 29 1 4 10 22 8 11 12 13 7 19 26 31
//0 3 3 3 2 4 2 1 1 1 1 1 0 4 3 2 2 2 2 2 1 3 1 0 0 1 1 0 0 0 0 0
//21 15 6 22 17 9 29 25 7 23 26 27 28 1 13 2 18 10 30 16 8 24 3 19 11 31 14 4 20 12 32 0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章