文件详细信息
本文件的大小为 2901 字节。
#include<algorithm>
#include<cstdio>
#include<cstring>
namespace IO{
const int ARR_SIZE=1<<20;
#define gc() ((IO::si!=IO::ti||(IO::ti=(IO::si=IO::input)+fread(IO::input,1,IO::ARR_SIZE,stdin))),IO::si!=IO::ti?*(IO::si++):EOF)
#define pc(ch) ((IO::o.so!=IO::o.to||(fwrite(IO::o.output,1,IO::ARR_SIZE,stdout),IO::o.so=IO::o.output)),*(IO::o.so++)=ch)
char input[ARR_SIZE],*si=input,*ti=input;
struct Output_Stream{
char output[ARR_SIZE],*so=output,*to=output+ARR_SIZE;
~Output_Stream(){
if(so==output)return;
fwrite(output,1,so-output,stdout);
so=output;
}
}o;
template<typename T>
void read(T&num){
int ch=gc();
num=0;
while(ch<48||ch>57)ch=gc();
while(ch>=48&&ch<=57)num=(num<<3)+(num<<1)+(ch^48),ch=gc();
}
int getstr(char*str){
char ch=gc(),*tmp=str;
while(ch=='\r'||ch=='\n'||ch==' '||ch=='\t')ch=gc();
while(ch!='\r'&&ch!='\n'&&ch!=' '&&ch!='\t'&&ch!=EOF)*(str++)=ch,ch=gc();
return str-tmp;
}
template<typename T>
void write(T a){
static int ch[50],cnt=0;
if(a==0)pc('0');
while(a)ch[++cnt]=a%10|48,a/=10;
while(cnt)pc(ch[cnt--]);
}
}
using IO::read;
using IO::getstr;
using IO::write;
inline void chkmax(int&a,const int b){
a<b&&(a=b);
}
inline void chkmin(int&a,const int b){
a>b&&(a=b);
}
const int maxn=50,maxlen=1000000+maxn,inf=0x3f3f3f3f;
int n,len;
char input[maxlen+2],s[maxlen+2];
int bel[maxlen+2],sa[maxlen+1],rk[maxlen*2+1],oldrk[maxlen*2+1],id[maxlen+1],cnt[maxlen+1],ht[maxlen+1];
void initSA(const int n){
int m=std::max(n,::n+26);
for(int i=1;i<=n;i++)++cnt[rk[i]=s[i]];
for(int i=1;i<=m;i++)cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--)sa[cnt[rk[i]]--]=i;
for(int w=1;w<n;w<<=1){
memset(cnt,0,sizeof(int)*(m+1));
for(int i=1;i<=n;i++)++cnt[rk[(id[i]=sa[i])+w]];
for(int i=1;i<=m;i++)cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--)sa[cnt[rk[id[i]+w]]--]=id[i];
memset(cnt,0,sizeof(int)*(m+1));
for(int i=1;i<=n;i++)++cnt[rk[id[i]=sa[i]]];
for(int i=1;i<=m;i++)cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--)sa[cnt[rk[id[i]]]--]=id[i];
memcpy(oldrk,rk,sizeof(int)*(n+1));
int p=0;
for(int i=1;i<=n;i++)
if(oldrk[sa[i]]==oldrk[sa[i-1]]&&oldrk[sa[i]+w]==oldrk[sa[i-1]+w])rk[sa[i]]=p;
else rk[sa[i]]=++p;
m=p;
if(p==n)break;
}
for(int i=1,k=0;i<=n;i++){
if(k)k--;
while(s[i+k]==s[sa[rk[i]-1]+k])k++;
ht[rk[i]]=k;
}
}
int ans[maxn+1][maxn+1],minh[maxn+1];
bool vis[maxn+1];
int main(){
read(n);
for(int i=1,l;i<=n;i++){
l=getstr(input+1);
for(int j=1,t=len;j<=l;j++)s[++t]=input[j]-'a';
for(int j=1,t=len;j<=l;j++)bel[++t]=i;
s[len+=l+1]=i+26;
}
initSA(len);
for(int i=1;i<=len;i++){
const int belong=bel[sa[i]];
for(int j=1;j<=n;j++){
chkmin(minh[j],ht[i]);
if(vis[j])chkmax(ans[belong][j],minh[j]);
}
vis[belong]=true,minh[belong]=inf;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j)
write(std::max(ans[i][j],ans[j][i])),pc(j!=n-(i==n)?' ':'\n');
return 0;
}