给n个字符串,求一个最长的子串,使得这个子串或子串的‘逆’在所有的字符串中至少出现一次。直接把每一个串和它翻转后的结果串起来,并且中间用分隔符分开就行,注意同一个串和它的‘逆’编成相同的号,然后二分答案+遍历height分组判断就可以了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=110100;
int s[maxn];
int sa[maxn],t[maxn],t2[maxn],c[maxn];
int sa2[maxn];
char s1[maxn],s2[maxn];
int rank[maxn],height[maxn];
int l1,l2;
int len,len1;
int n,m,ans;
int belong[maxn];
inline int idx(char s)
{
// if (s>='a' && s<='z') return s-'a'+21;
// if (s>='A' && s<='Z') return s-'A'+21+26;
return s-min('a','A')+101;
}
void getheight(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--;
int j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
void build_ss(int m,int n)
{
n++;
int i,*x=t,*y=t2;
for (int i=0; i<m; i++) c[i]=0;
for (int i=0; i<n; i++) c[x[i]=s[i]]++;
for (int i=1; i<m; i++) c[i]+=c[i-1];
for (int i=n-1; i>=0; i--)
sa[--c[x[i]]]=i;
for (int k=1; k<=n; k<<=1)
{
int p=0;
for (i=n-k; i<n; i++) y[p++]=i;
for (i=0; i<n; i++) if (sa[i]>=k) y[p++]=sa[i]-k;
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]+k]==y[sa[i]+k])? p-1 : p++;
if (p>=n) break;
m=p;
}
}
int d[maxn][22];
void RMQ_init()
{
for (int i=1; i<=n; i++) d[i][0]=height[i];
for (int j=1; (1<<j)<=n; j++)
for (int i=1; (i+(1<<j)-1)<=n; i++)
d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
int RMQ(int L,int R)
{
if (L>R) swap(L,R);
L++;
int k=0;
while((1<<(k+1))<=R-L+1) k++;
return min(d[L][k],d[R-(1<<k)+1][k]);
}
void init()
{
n=0;
int ct=1;
for (int i=0; i<m; i++)
{
scanf("%s",s1);
l1=strlen(s1);
for (int j=0; j<l1; j++)
belong[n]=i,s[n++]=idx(s1[j]);
belong[n]=-1;
s[n++]=ct++;
for (int j=l1-1; j>=0; j--)
belong[n]=i,s[n++]=idx(s1[j]);
belong[n]=-1;
s[n++]=ct++;
}
s[n]=0;
build_ss(400,n);
getheight(n);
}
bool ct[120];
bool check(int k)
{
memset(ct,0,sizeof ct);
int cnt=0;
for (int i=1; i<=n; i++)
{
if (height[i]>=k)
{
if (ct[belong[sa[i]]]==false)
{
ct[belong[sa[i]]]=true;
cnt++;
}
if (cnt==m) return true;
}
else
{
memset(ct,0,sizeof ct);
cnt=0;
if (belong[sa[i]]>=0) ct[belong[sa[i]]]=true,cnt=1;
}
}
return false;
}
void slove()
{
int l=1,r=100;
int ans=0;
while(l<r)
{
int mid=(l+r)>>1;
if (check(mid)) ans=mid,l=mid+1;
else r=mid;
}
printf("%d\n",ans);
}
int tt;
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&m);
if (m==1)
{
scanf("%s",s1);
printf("%d\n",strlen(s1));
continue;
}
init();
slove();
}
}