HDU 6208:The Dominator of Strings(字符串匹配)

来源:互联网 发布:淘宝店家怎么代销 编辑:程序博客网 时间:2024/06/06 06:39

2017 ACM/ICPC Asia Regional Qingdao Online 1003

The Dominator of Strings

Time limit:3000 ms Memory limit:65768 kB


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


题意:

输入n个字符串,问其中是否有字符串能包含其余所有的字串。

解题思路:

首先找到最长的那个串,然后与剩余的所有串匹配,看现场交的情况匹配算法用KMP必然是超时的,百度一下比KMP算法更快的,很自然的就找到了Sunday算法。。。(一下找不到出处就不注明来源了)

Code:

#include<iostream>#include<cstdio>#include<cmath>#include<string>#include<vector>using namespace std;typedef long long int LL;const double EPS=1e-8;int SundaySearch(string text, string pattern){    int i = 0, j = 0, k;    int m = pattern.size();    if(pattern.size() <= 0 || text.size() <= 0)        return -1;    for(; i<text.size();) {        if(text[i] != pattern[j]) {            for(k=pattern.size() - 1; k>=0; k--) {                if(pattern[k] == text[m])                    break;            }            i = m-k;            j = 0;            m = i+pattern.size();        }        else {            if(j == pattern.size()-1)                return i-j;            i++;            j++;        }    }    return -1;}vector<string> v;int main(){    ios::sync_with_stdio(false);    int T;    cin>>T;    while(T--)    {        int n;        cin>>n;        v.clear();        string t,text;        for(int i=0;i<n;i++)        {            cin>>t;            if(text.length()<t.length())                text=t;            v.push_back(t);        }        int flag=1;        for(int i=0;i<n;i++)        {            if(SundaySearch(text,v[i])==-1)            {                flag=0;                break;            }        }        if(flag)            cout<<text<<endl;        else            cout<<"No"<<endl;    }    return 0;}
原创粉丝点击