Time Limit: 3000/3000 MS (Java/Others)
Memory Limit: 65535/32768 K (Java/Others)
Problem Description
Here you have a set of strings. A A A dominator is a string of the set dominating all strings else. The string S S S is dominated by T T T if S S S is a substring of T T T.
Input
The input contains several test cases and the first line provides the total number of cases.
For each test case, the first line contains an integer
N
N
N indicating the size of the set.
Each of the following
N
N
N lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of
100000
100000
100000.
The limit is 30MB for the input file.
Output
For each test case, output a dominator if exist, or No if not.
Sample Input
3
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
5
abc
cde
abcde
abcde
bcde
3
aaaaa
aaaab
aaaac
Sample Output
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
abcde
No
Source
2017 ACM/ICPC Asia Regional Qingdao Online
题意:
给n个字符串,问其中是否存在一个字符串使得其他所有字符串是它的子串。
题解:
AC自动机裸题
#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
int n,maxLen,Len[100004];
char s[100004],revs[100004];
int sz,ch[1000004][34],cnt[1000004],p[1000004];
bool mark[1000004];
queue<int>q;
int add(char *s){
int l=strlen(s+1);
int now=1;
for(int i=1;i<=l;i++){
int to=s[i]-'a';
if(!ch[now][to]){
ch[now][to]=++sz;
}
now=ch[now][to];
}
cnt[now]++;
return 0;
}
int build(){
while(!q.empty())q.pop();
p[1]=0;
q.push(1);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=0;i<26;i++){
if(!ch[x][i])continue;
int now=p[x];
while(!ch[now][i])now=p[now];
p[ch[x][i]]=ch[now][i];
q.push(ch[x][i]);
}
}
return 0;
}
int solve(char *pro){
int ret=0,now=1,l=strlen(pro+1);
for(int i=1;i<=l;i++){
mark[now]=1;
int to=pro[i]-'a';
while(!ch[now][to])now=p[now];
now=ch[now][to];
if(!mark[now]){
for(int j=now;j;j=p[j]){
ret+=cnt[j];
cnt[j]=0;
}
}
}
return ret;
}
int w33ha(){
for(int i=0;i<=sz;i++){
p[i]=mark[i]=cnt[i]=0;
for(int j=0;j<26;j++)ch[i][j]=0;
}
sz=1;maxLen=0;
for(int i=0;i<26;i++)ch[0][i]=1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s+1);
int l=strlen(s+1);
if(maxLen<l){
maxLen=l;
for(int i=1;i<=l;i++)revs[i]=s[i];
revs[l+1]='\0';
}
add(s);
}
build();
int ans=solve(revs);
if(ans==n){
for(int i=1;i<=maxLen;i++)printf("%c",revs[i]);
puts("");
}
else puts("No");
return 0;
}
int LiangJiaJun(){
int T;scanf("%d",&T);
while(T--)w33ha();
return 0;
}