poj3630(简单tire)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=100000+10;
int trie[maxn][20];
char c[maxn][20];
int tot;
bool endi[maxn*20];

void myinsert(char* st){
     int len=strlen(st);
     int ch,p=1;
     for (int i=0;i<len;i++){
         ch=st[i]-'0'+1;
         if(!trie[p][ch]) trie[p][ch]=++tot;
         p=trie[p][ch];
     }
     endi[p]=true;
}

bool findi(char* st){
     int len=strlen(st);
     int p=1;
     for (int i=0;i<len;i++){
        p=trie[p][st[i]-'0'+1];
        if(p==0) return true;
        if(endi[p]&&i!=len-1) return false;
     }
     return true;
}

int main(){
    int t,n;
    bool ans;
    scanf("%d",&t);
    while(t--){
        tot=1;
        memset(endi,0,sizeof(endi));
        memset(trie,0,sizeof(trie));
        memset(c,'0',sizeof(c));
        ans=true;
        scanf("%d",&n);
        for (int i=1;i<=n;i++){
           scanf("%s",c[i]);
        myinsert(c[i]);
        }
        for (int i=1;i<=n;i++){
            if(!findi(c[i])) {
                    printf("NO\n");
                    ans=false;
                    break;
            }
        }
        if(ans) printf("YES\n");
    }
return 0;
}

 

posted @ 2018-05-15 19:28  lmjer  阅读(253)  评论(0编辑  收藏  举报