此题数据很水,把每个串和其反向串全部连接起来,标记的时候别忘了反向串和原串串对应同一个串
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <ctime>
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
const int MAXN(101000);
const int SIGMA_SIZE(26);
const int MAXM(110);
const int MAXE(4000010);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 2);
const int MOD(9999991);
const ULL LIM(1000000000000000ull);
struct SA
{
int S[MAXN];
int sa[MAXN], t1[MAXN], t2[MAXN], cnt[MAXN], len, M;
void init(int tl, int tm = 128)
{
len = tl;
M = tm;
int *p1 = t1, *p2 = t2;
for(int i = 0; i < M; ++i) cnt[i] = 0;
for(int i = 0; i <= len; ++i) ++cnt[p1[i] = S[i]];
for(int i = 1; i < M; ++i) cnt[i] += cnt[i-1];
for(int i = len; i >= 0; --i) sa[--cnt[p1[i]]] = i;
int temp = 1;
for(int k = 1; temp <= len; k <<= 1)
{
temp = 0;
for(int i = len-k+1; i <= len; ++i) p2[temp++] = i;
for(int i = 0; i <= len; ++i)
if(sa[i] >= k)
p2[temp++] = sa[i]-k;
for(int i = 0; i < M; ++i) cnt[i] = 0;
for(int i = 0; i <= len; ++i) ++cnt[p1[p2[i]]];
for(int i = 1; i < M; ++i) cnt[i] += cnt[i-1];
for(int i = len; i >= 0; --i) sa[--cnt[p1[p2[i]]]] = p2[i];
swap(p1, p2);
temp = 1;
p1[sa[0]] = 0;
for(int i = 1; i <= len; ++i)
p1[sa[i]] = p2[sa[i-1]] == p2[sa[i]] && p2[sa[i-1]+k] == p2[sa[i]+k]? temp-1: temp++;
M = temp;
}
}
int rank[MAXN], height[MAXN];
void getHeight()
{
int k = 0;
for(int i = 1; i <= len; ++i)
rank[sa[i]] = i;
for(int i = 0; i < len; ++i)
{
if(k) --k;
int j = sa[rank[i]-1];
while(S[i+k] == S[j+k]) ++k;
height[rank[i]] = k;
}
}
};
SA sa;
int N;
int hash[MAXN][2];
int table[210];
int flag[110];
char str[110];
int arr_len[110];
bool check(int value)
{
int j, ind = 0;
memset(flag, 0, sizeof(flag));
for(int i = 0; i <= sa.len; i = j)
{
j = i+1;
++ind;
int cnt = 0;
if(table[hash[sa.sa[i]][0]]-sa.sa[i] >= value)
{
++cnt;
flag[hash[sa.sa[i]][1]] = ind;
}
while(j <= sa.len && sa.height[j] >= value)
{
if(flag[hash[sa.sa[j]][1]] != ind)
{
flag[hash[sa.sa[j]][1]] = ind;
++cnt;
}
++j;
}
if(cnt == N)
return true;
}
return false;
}
int main()
{
int TC;
scanf("%d", &TC);
while(TC--)
{
scanf("%d", &N);
int len = 0, seq = 129;
int l = 1, r = 100;
for(int i = 1; i <= N; ++i)
{
scanf("%s", str);
arr_len[i] = strlen(str);
r = min(r, arr_len[i]);
len += arr_len[i];
int j, k;
for(j = len-arr_len[i], k = 0; j < len; ++j, ++k)
{
sa.S[j] = str[k];
hash[j][0] = i;
hash[j][1] = i;
}
hash[len][0] = i;
hash[len][1] = i;
table[i] = len;
sa.S[len++] = seq++;
}
int ind = 0;
for(int i = 1; i <= N; ++i)
{
len += arr_len[i];
int j, k;
for(j = len-arr_len[i]; j < len; ++j)
{
sa.S[j] = sa.S[ind++];
hash[j][0] = i+N;
hash[j][1] = i;
}
for(j = len-arr_len[i], k = len-1; j < k; ++j, --k)
swap(sa.S[j], sa.S[k]);
hash[len][0] = i+N;
hash[len][1] = i;
table[i+N] = len;
if(i != N)
{
sa.S[len++] = seq++;
++ind;
}
else
sa.S[len] = 0;
}
sa.init(len, 256);
sa.getHeight();
++r;
while(l < r)
{
int m = (l+r) >> 1;
if(check(m))
l = m+1;
else
r = m;
}
--l;
printf("%d\n", l);
}
return 0;
}