HDU–6208 The Dominator of Strings[KMP]


The Dominator of Strings

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

题意:所给的所有字符串中是否含有一个主串包含其他的字符串。

思路:直接找到最长的,检查。

#include "bits/stdc++.h"
using namespace std;
const int maxn = 2000000 + 10;
int f[maxn];
int T,N;
char s[maxn];
char *t[maxn];
void getfail(char p[]) {
    int len = strlen(p);  
    f[0] = f[1] = 0;  
    for(int i = 1; i < len; i++) {
        int j = f[i];  
        while(j and 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[]) {
    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];    
        if(P[j] == T[i]) j++; 
        if(j == m) return 1;
    }    
    return -1;    
} 
int main(int argc, char const *argv[])
{
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        int maxx = 0;
        int tlen = 0;
        char *ms, *ts=s;
        for(int i=1 ;i <= N;i++){
            scanf("%s",ts);
            tlen = strlen(ts);
            if(tlen > maxx){
                maxx = tlen; ms = ts;
            }
            t[i] = ts; ts += strlen(ts) + 2;
        }
        bool flag = true;
        for(int j = 1;j <= N; j++){
            if( find(ms,t[j]) != 1 ){
                flag = false; break;
            }
        }
        if(flag) printf("%s\n",ms);
        else printf("No\n");
    }
    return 0;
}
优质内容筛选与推荐>>
1、The connection to adb is down
2、nyoj73 比大小
3、(十)div和span
4、用户需求报告
5、项目成本管理


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号