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

 

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