Trie树(字典树)poj3630+hdu1671

来源:互联网 发布:h5免费制作网站 知乎 编辑:程序博客网 时间:2024/06/05 16:09

Language:
Phone List
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 22737 Accepted: 7030

Description

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:

  • Emergency 911
  • Alice 97 625 999
  • Bob 91 12 54 26

In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.

Input

The first line of input gives a single integer, 1 ≤ t ≤ 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

Output

For each test case, output "YES" if the list is consistent, or "NO" otherwise.

Sample Input

2391197625999911254265113123401234401234598346

Sample Output

NOYES
题意:判读是不是存在有的串是其他串的前缀。

思路:字典树的应用。

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=10010;int n,flag;char s[maxn];struct TRIE{    int sz;    int ch[maxn*100*10][10];    int val[maxn*1000];    void clear(){memset(ch[0],0,sizeof(ch[0]));memset(val,-1,sizeof(val));sz=1;}    int idx(char x){return x-'0';}    void insert(char *s,int id)    {        int n=strlen(s);        int u=0;        for(int i=0;i<n;i++)        {            int c=idx(s[i]);            bool is_new=false;            if(!ch[u][c])            {                memset(ch[sz],0,sizeof(ch[sz]));                val[sz]=0;                ch[u][c]=sz++;                is_new=true;            }            u=ch[u][c];            if(val[u]>0){flag=false;return ;}            if(i==n-1&&!is_new){flag=false;return ;}        }        val[u]=id;    }}tree;int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        flag=1;        tree.clear();        for(int i=1;i<=n;i++)        {            scanf("%s",s);            if(flag)tree.insert(s,i);        }        if(flag)printf("YES\n");        else printf("NO\n");    }    return 0;}


hdu上这样会超内存,要用指针,还得释放内存

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=10010;int n,flag;char s[maxn];struct node{    node *next[26];    int val;    node(){memset(next,0,sizeof(next));val=0;}};struct TRIE{    node *root;    void build(){root=new node();}    void clear(node *p)    {        for(int i=0;i<26;i++)        {            if(p->next[i]!=NULL)                clear(p->next[i]);        }        delete p;    }    int idx(char x){return x-'0';}    void insert(char *s,int id)    {        int n=strlen(s);        node *p=root;        for(int i=0;i<n;i++)        {            int c=idx(s[i]);            bool is_new=false;            if(!p->next[c])            {                p->next[c]=new node();                is_new=true;            }            p=p->next[c];            if(p->val>0){flag=false;return ;}            if(i==n-1&&!is_new){flag=false;return ;}        }        p->val=id;    }}tree;int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        flag=1;        tree.build();        for(int i=1;i<=n;i++)        {            scanf("%s",s);            if(flag)tree.insert(s,i);        }        if(flag)printf("YES\n");        else printf("NO\n");        tree.clear(tree.root);    }    return 0;}



0 0
原创粉丝点击