HDU 6208 The Dominator of Strings

The Dominator of Strings

Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 549    Accepted Submission(s): 158


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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6216  6215  6214  6213  6212 
 


就是KMP,KMP了,手贱,把next(即f每次付一遍1),汗,想了一下午的ac自动机

ac代码:


#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);
		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++){
			
			if( find(qw,t[j]) != 1 ){
				flag=0;
				break ;
			}
		}
		
		if(flag){
			printf("%s\n",qw);
		}
		else{
			printf("No\n");
		}
		
	}
		
	
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值