Monday, October 31, 2016

Solution of UVa 11362-Phone List

See the problem  UVa-11362


#include<bits/stdc++.h>
using namespace std;
long long n, t, i, last=1, cur, k, x, fl;
string s;

struct node{
    bool endmark;
    int next[11];
}r[100009];

void new_trie(int cur){
    for(int zz=0; zz<10; zz++){
        r[cur].next[zz]=-1;
    }
    r[cur].endmark=false;
}

void insrt(){
    cur=0, k=s.size();
    for(int z=0; z<k; z++){
        x=(int)s[z]-48;
        if(r[cur].next[x]==-1){
            r[cur].next[x]=last;
            new_trie(last++);
            cur=r[cur].next[x];
        }
        else{
            cur=r[cur].next[x];
            if(r[cur].endmark==true)fl=1;
            if(z==k-1) fl=1;
        }
        if(fl==1) break;
    }
    r[cur].endmark=true;
}

int main(){
    cin>>t;
    while(t--){
        cin>>n;
        fl=0, last=1;
        new_trie(0);
        for(i=0; i<n; i++){
            cin>>s;
            insrt();
        }
        if(fl==1) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
    return 0;
}

No comments:

Post a Comment