2017ICPC青岛网络赛-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

裸trie模板

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
#define INF 0x2fffffff
#define LL long long
#define MAX(a,b) ((a)>(b))?(a):(b)
#define MIN(a,b) ((a)<(b))?(a):(b)
#define id(a) ((a)-'a')
const int maxnode = 10000*50+10;
int ch[maxnode][26];
int val[maxnode];
int sz = 0;
int n;

char s[100010]; 
char a[100010];

int mmax;
int insert(char *s){
    int len =strlen(s);
    int u = 0;
    for(int i = 0;i < len;i++){
        int c = id(s[i]);
        if(!ch[u][c]){
            memset(ch[sz],0,sizeof(ch[sz]));
            val[sz] = 0;
            ch[u][c] = sz++;
        }
        u = ch[u][c];           
    }
    val[u] ++;
}

int query(char *s){
    int ans = 0;
    for(int i = 0;s[i];i++){
        int c = id(s[i]);
        int u = 0;
        int j = 0;
        while(ch[u][c]){
            if(val[ch[u][c]]){
                ans += val[ch[u][c]];
                val[ch[u][c]] = 0;
            }
            u = ch[u][c];
            j ++;
            c = id(s[i+j]);
        }
    }
    if(ans==n)
    {
        cout<else
    printf("No\n");
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        mmax=-1;
        scanf("%d",&n);

        memset(ch[0],0,sizeof(ch[0]));
        val[0]= 0;
        sz = 1;
        for(int i = 0;i < n;i++){
            scanf("%s",a);
            insert(a);
            int l=strlen(a);
            if(l>mmax)
            {
                mmax=l;
                int i;
                for(i=0;i'\0';
            }
        }
        query(s);
    }
    return 0;
}

你可能感兴趣的:(icpc)