题意:长度最长串为母串 问其余n-1个串是否都能在母串中匹配 ,总字符长度<=1e5.
AC自动机匹配数为n则有解.
query时不重复统计以标记结点.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+20,M=1e5+5;
struct AHO
{
int chd[N][26],v[N],f[N],last[N],sz,ans;
void init()
{
sz=1;ans=0;
memset(v,0,sizeof(v));
memset(f,0,sizeof(f));
memset(chd[0],0,sizeof(chd[0]));
}
void insert(char *s)
{
int u=0,i=0;
while(s[i])
{
int id=s[i]-'a';
if(!chd[u][id])
{
memset(chd[sz],0,sizeof(chd[sz]));
chd[u][id]=sz++;
}
u=chd[u][id];
i++;
}
v[u]++;
}
void getfails()
{
queue<int> q;
f[0]=0;
for(int c=0;c<26;c++)
{
int u=chd[0][c];
if(u)
{
f[u]=0;
q.push(u);last[u]=0;
}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<26;c++)
{
int u=chd[r][c];
if(!u)
{
chd[r][c]=chd[f[r]][c];
}
else
{
q.push(u);
int x=f[r];
while(x&&!chd[x][c])
x=f[x];
f[u]=chd[x][c];
last[u]=v[f[u]]? f[u]:last[f[u]];
}
}
}
}
int find(char *s)
{
int u=0,cnt=0;
int n=strlen(s);
for(int i=0;i<n;i++)
{
int c=s[i]-'a';
u=chd[u][c];
int temp=0;//mark
if(v[u])
temp=u;
else if(last[u])
temp=last[u];
while(temp)
{
cnt+=v[temp];
v[temp]=0;
temp=last[temp];
}
}
return cnt;
}
}aho;
char s[M],dic[M];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
aho.init();
int n,mx=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",dic);
int tl=strlen(dic);
if(mx<tl)
{
mx=tl;
strcpy(s,dic);
}
aho.insert(dic);
}
aho.getfails();//
int ans=aho.find(s);
//printf("%d\n",ans);
if(ans==n)
printf("%s\n",s);
else
printf("No\n");
}
return 0;
}