# spojPHRASES Relevant Phrases of Annihilation

## 代碼：

/*************************************************************
Problem: spoj PHRASES Relevant Phrases of Annihilation
User: fengyuan
Language: C++
Result: Accepted
Time: 30 ms
Memory: 25.6 MB
Submit_Time: 2018-01-23 21:44:11
*************************************************************/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cctype>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;

const int N = 300010;
int n, m, all;
int tong[N], sa[N], h[N], rk[N], tp[N], mx[15], mi[15], pos[N], t[N], wv[N];
char str[N], s[N];

inline void ssort() {
for(int i = 0; i <= all; i ++) tong[i] = 0;
for(int i = 1; i <= n; i ++) tong[rk[tp[i]]] ++;
for(int i = 1; i <= all; i ++) tong[i] += tong[i-1];
for(int i = n; i >= 1; i --) sa[tong[rk[tp[i]]] --] = tp[i];
}
inline void get_sa() {
for(int i = 1; i <= n; i ++) rk[i] = s[i], tp[i] = i;
all = 127; ssort(); int w = 1; all = 1;
while(all < n) {
int t = 0;
for(int i = n-w+1; i <= n; i ++) tp[++ t] = i;
for(int i = 1; i <= n; i ++) if(sa[i]>w) tp[++ t] = sa[i]-w;
ssort(); for(int i = 1; i <= n; i ++) tp[i] = rk[i];
rk[sa[1]] = all = 1;
for(int i = 2; i <= n; i ++)
rk[sa[i]] = (tp[sa[i]] == tp[sa[i-1]] && tp[sa[i]+w] == tp[sa[i-1]+w])?all:++ all;
w <<= 1;
} int k = 0;
for(int i = 1; i <= n; i ++) {
if(k) k --; int j = sa[rk[i]-1];
for(; i+k<=n && j+k<=n && s[i+k] == s[j+k]; k ++);
h[rk[i]] = k;
}
}
inline bool check(int x) {
memset(mx, 0, sizeof mx); memset(mi, 0x3f, sizeof mi);
for(int i = 1; i <= n; i ++) {
if(h[i] >= x) {
mx[pos[sa[i]]] = max(mx[pos[sa[i]]], sa[i]);
mi[pos[sa[i]]] = min(mi[pos[sa[i]]], sa[i]);
mx[pos[sa[i-1]]] = max(mx[pos[sa[i-1]]], sa[i-1]);
mi[pos[sa[i-1]]] = min(mi[pos[sa[i-1]]], sa[i-1]);
bool flag = 1;
for(int j = 1; j <= m; j ++)
if(mx[j]-mi[j] < x) { flag = 0; break; }
if(flag) return 1;
} else {
memset(mx, 0, sizeof mx); memset(mi, 0x3f, sizeof mi);
mx[pos[sa[i]]] = mi[pos[sa[i]]] = sa[i];
}
}
return 0;
}
int main() {
int test; scanf("%d", &test);
while(test --) {
scanf("%d", &m); n = 0; int l = 0, r = 10000, mid;
for(int i = 1; i <= m; i ++) {
scanf("%s", str); int len = strlen(str);
for(int j = 0; j < len; j ++) { s[++ n] = str[j]; pos[n] = i; }
if(i < m) s[++ n] = '#'+i, pos[n] = i;
}
get_sa(); int ans = 0;
while(l <= r) {
mid = l+r>>1;
if(check(mid)) ans = mid, l = mid+1; else r = mid-1;
} printf("%d\n", ans);
}
return 0;
}