hdu6208
The Dominator of Strings
Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2680 Accepted Submission(s): 970
Problem Description
Here you have a set of strings. A dominator is a string of the set dominating all strings else. The string S is dominated by T if S is a substring of 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 indicating the size of the set.
Each of the following N lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of 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
题意:
t组样例,每个样例n个串,问在这中间有没有一个串包含所有其他串,有的话就输出这个串
思路:
如果存在满足题意的串那一定是最长的串,找到最长的串,其余每个串都和他做kmp匹配
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define ll long long
using namespace std;
int f[1000010];
int T,n;
char s[2000010];
char *t[120000];
void getfail(char p[]) //字符串p自我匹配
{
int len=strlen(p);
f[0]=f[1]=0;
for(int i=1;i<len;i++)
{
int j=f[i];
while(j&&p[i]!=p[j])
j=f[j];
if(p[i]==p[j])
f[i+1]=j+1;//多匹配到了一个字符
else
f[i+1]=0;//该字符配不上
}
}
int find(char* T, char*P)//p去匹配字符串T
{
int n = strlen(T), m = strlen(P);
getfail(P); //得出部分匹配表
int j = 0; //短串的下标
for(int i = 0; i < n; i++) //长串下标
{
while(j && P[j] != T[i])//突然失配了
{
j = f[j]; //j往回退,直到0或者上一个字符相等的位置
}
if(P[j] == T[i])
{
j++; //匹配了一个字符,j++
}
if(j == m) //短串匹配到头了
{
return 1;//i - m + 1;//返回成功匹配的起点字符位置
}
}
return -1;
}
int main(){
int max_len;
scanf("%d",&T);
while(T--){
scanf("%d",&n);//有n个串
max_len=0;
int tmp;
char *qw;
char *io=s;
for(int i=1 ;i <= n;i++){
scanf("%s",io);
tmp=strlen(io);
if( tmp > max_len ){
max_len=tmp;//找到最长的串
qw=io;
}
t[i]=io;
io+=strlen(io)+2;
}
int flag=1;
for(int j=1;j<=n;j++){//遍历每个串与最长的串做kmp查询
if( find(qw,t[j]) != 1 ){
flag=0;
break ;
}
}
if(flag) printf("%s\n",qw);
else printf("No\n");
}
return 0;
}