Substrings

Description

You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.

Input

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.

Output

There should be one line per test case containing the length of the largest string found.

Sample Input

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

Sample Output

2
2 
题目大意:求出一个最长的串,这个串或者其反向的串是原来所有字符串的子串……
思路:把每一个串和他的反向串连起来(中间用不同的字符隔开),在把所有的串连起来(也用不相同的字符隔开),然后求出其height数组,二分答案,将后缀分组,判断是否有一组中包含了每一个串或者其反转的串~~
// yy实际上是bool数组,判断当前的那个串有没有被用过,通过每次z的值都在变,就省去了的对bool的清零……
//(从论文附件中的标程里学到的orz)
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#define maxn 20201
long t, n, len, z = 0;
long yy[101];
long r[maxn], x[maxn], sa[maxn];
long wa[maxn], wb[maxn], ws[maxn], wv[maxn], rank[maxn], height[maxn];
bool cmp (long *r, long a, long b, long l)
{
    return ((r[a] == r[b]) && (r[a + l] == r[b + l]));
}


void calcsa(long *r, long *sa, long n, long m)
{
    long i, j, p, *x = wa, *y = wb, *t;

    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 >= 0) 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];

        t = x; x = y; y = t;
        p = 1; x[sa[0]] = 0;
        for (i = 1; i < n ; i++)
        {
            x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
        }
    }
 }
void calcheight(long *r, long *sa, long n)
{
    long i, j, p;
    for (i = 1; i <= n; i++)
        rank[sa[i]] = i;
    p = 0;
    for (i = 0; i < n; i++)
    {
        j = sa[rank[i] - 1];
        while (r[i + p] == r[j + p])
            p++;
        height[rank[i]] = p;
        if (p > 0) p--;
    }
    return;
}
long check (long mid)
{
    long i, j, k, t, s;
    for (i = 2; i <= len; i = j + 1)
    {
        for( ; height[i] < mid && i <= len; i++);
        for(j = i; height[j] >= mid; j++);
        if (j - i + 1 < n) continue;
        s = 0; z++;
        for (k = i - 1; k < j; k++)
        {

            if (((t = x[sa[k]]) != 0) &&(yy[t] != z))
            {
                yy[t] = z;
                s++;
            }
        }
        if (s >= n) return 1;
    }
    return 0;
}
void deal()
{
    long l = 0, r = 100;
    while (l <= r)
    {
        long mid = (l + r) / 2;
        if (check(mid)) l = mid + 1;
        else r = mid - 1;
    }
    printf("%d\n", r);
}
int main()
{
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        char c[120];
        len = 0;
        for (long i = 1; i <= n; i++)
        {
            scanf("%s", c);
            long k = strlen(c);
            for (long j = 0; j < k; j++)
            {
                r[j + len] = c[j] + 200;
                x[j + len] = i;
            }
            r[len + k] = 2 * i - 1;
            x[len + k] = 0;
            len += k + 1;
            for(long j = 0; j < k; j++)
            {
                r[j + len] = c[k - 1 - j] + 200;
                x[j + len] = i;
            }
            r[len + k] = 2 * i;
            x[len + k] = 0;
            len += k + 1;
        }
        len--;
        r[len] = 0;
        if (n == 1)
        {
            printf("%d\n", len/ 2);
            continue;

        }
        calcsa(r, sa, len + 1, 400);
        calcheight(r, sa, len);
        height[len + 1] = -1;
        deal();

    }


    return 0 ;
}