码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3630 / hdu 1671 Phone List 【Trie字典树 动态创建&静态创建】

时间:2015-08-07 00:23:32      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:trie树   poj   hdu   数据结构   字典树   

Phone List
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25160   Accepted: 7641

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

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

Sample Output

NO
YES

这个题目有三种做法:

①动态创建Trie树,这样做的好处是节省空间,没有空间冗余,但是缺点是需要多次new一个节点,对于这个题而言,每组测试样例 new 的次数多达10000*10次,要知道动态创建这么多个节点是必然超了1000ms的,但是maybe,hdu的数据比较水,这种方法依旧能AC,但是这种方法必须要注意delete,防止内存泄露。

②静态创建Trie树,这样做的好处是避免了多次new操作,节省了时间,但是缺点是,不能保证数据有没有空间冗余,而且,这种方法其实还有一些限制的,在有些题目中,字符串的最大长度未知,或者字符串的数目未知的情况下,这种做法是不可取的,但是在这个题目里面,都明确了范围,故这种方法可行。

③最后一种方法,也是我没有想到的,先对输入字符串进行排序,然后比较相邻两个字符串是不是前缀。

方法一:

Problem : 1671 ( Phone List )     Judge Status : Accepted
RunId : 14381387    Language : G++    Author : 245747005
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
/****************************>>>>HEADFILES<<<<****************************/
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define CASE(T)         for(scanf("%d",&T);T--;)
#define rep(i,a,b)      for(int i = a;i <= b;i++)
#define rep1(i,a)       for(int i = 1;i <= a;i++)
#define rep0(i,a)       for(int i = 0;i < a;i++)
#define MP(a,b)         make_pair(a,b)
#define PB(a)           push_back(a)
#define fst             first
#define snd             second
#define lson            l,mid,rt<<1
#define rson            mid+1,r,rt<<1|1
/****************************>>>>>>DEBUG<<<<<<****************************/
#define out(x)          cout<<x<<""
/****************************>>>>SEPARATOR<<<<****************************/
const int maxk = 10;
const int maxl = 200+5;
int N,T;
bool suc;
struct Node
{
    int cnt;
    Node* pNext[maxk];
    Node() : cnt(0)
    {
        rep0(i,maxk) pNext[i] = NULL;
    }
};
struct Trie
{
    Node* pRoot;
    //Trie() : pRoot(new Node()) {}
    void fuck() { pRoot = new Node();pRoot->cnt = 0; }
    void AddWord(const char str[],int len);
    void Release(const Node *p);
};
void Trie::AddWord(const char str[],int len)
{
    Node* ptr = pRoot;
    bool flag = false;
    for(int i = 0;i < len;i++)
    {
        int nPos = str[i] - '0';

        int son_cnt = 0;
        for(int j = 0;j < maxk;j++) if(ptr->pNext[j] != NULL) son_cnt += ptr->pNext[j]->cnt;
        if(ptr->cnt - son_cnt >= 1)  { suc = false; return; }

        if(ptr->pNext[nPos] == NULL) ptr->pNext[nPos] = new Node(),flag = true;
        ptr->cnt++;
        ptr = ptr->pNext[nPos];
    }
    if(!flag) suc = false;
    ptr->cnt++;
}
void Trie::Release(const Node* p)
{
    for(int i = 0;i < maxk;i++) if(p->pNext[i] != NULL) Release(p->pNext[i]);
    delete p;
}
char buf[maxl];
int main()
{
   // FIN;
    CASE(T)
    {
        Trie dic;
        dic.fuck();
        suc = true;
        scanf("%d",&N);
        rep1(i,N)
        {
            scanf("%s",buf);
            if(suc) dic.AddWord(buf,strlen(buf));
        }
        dic.Release(dic.pRoot);
        printf("%s\n",suc?"YES":"NO");
    }
    return 0;
}

方法二:

Problem : 1671 ( Phone List )     Judge Status : Accepted
RunId : 14383224    Language : G++    Author : 245747005
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
/****************************>>>>HEADFILES<<<<****************************/
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define CASE(T)         for(scanf("%d",&T);T--;)
#define rep(i,a,b)      for(int i = a;i <= b;i++)
#define rep1(i,a)       for(int i = 1;i <= a;i++)
#define rep0(i,a)       for(int i = 0;i < a;i++)
#define MP(a,b)         make_pair(a,b)
#define PB(a)           push_back(a)
#define fst             first
#define snd             second
#define lson            l,mid,rt<<1
#define rson            mid+1,r,rt<<1|1
/****************************>>>>>>DEBUG<<<<<<****************************/
#define out(x)          cout<<x<<""
/****************************>>>>SEPARATOR<<<<****************************/
const int maxk = 10;
const int maxl = 20+5;
int N,T;
bool suc;
struct Node
{
    int cnt;
    Node* pNext[maxk];
    Node() : cnt(0)
    {
        rep0(i,maxk) pNext[i] = NULL;
    }
}Memory[100006];
int G_Cnt;
struct Trie
{
    Node* pRoot;
    //Trie() : pRoot(new Node()) {}
    void AddWord(const char str[],int len);
    //void Release(const Node *p);
}trie;
void Trie::AddWord(const char str[],int len)
{
    Node* ptr = pRoot;
    bool flag = false;
    for(int i = 0;i < len;i++)
    {
        int nPos = str[i] - '0';

        int son_cnt = 0;
        for(int j = 0;j < maxk;j++) if(ptr->pNext[j] != NULL) son_cnt += ptr->pNext[j]->cnt;
        if(ptr->cnt - son_cnt >= 1)  { suc = false; return; }

        if(ptr->pNext[nPos] == NULL) ptr->pNext[nPos] = &Memory[G_Cnt++],flag = true;
        ptr->cnt++;
        ptr = ptr->pNext[nPos];
    }
    if(!flag) suc = false;
    ptr->cnt++;
}
char buf[maxl];
int main()
{
    //FIN;
    CASE(T)
    {
        memset(Memory,0,sizeof(Memory));
        G_Cnt = 0;
        trie.pRoot = &Memory[G_Cnt++];
        suc = true;
        scanf("%d",&N);
        rep1(i,N)
        {
            scanf("%s",buf);
            if(suc) trie.AddWord(buf,strlen(buf));
        }
        printf("%s\n",suc?"YES":"NO");
    }
    return 0;
}

方法三:

/****************************>>>>HEADFILES<<<<****************************/
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define CASE(T)         for(scanf("%d",&T);T--;)
#define rep(i,a,b)      for(int i = a;i <= b;i++)
#define rep1(i,a)       for(int i = 1;i <= a;i++)
#define rep0(i,a)       for(int i = 0;i < a;i++)
#define MP(a,b)         make_pair(a,b)
#define PB(a)           push_back(a)
#define fst             first
#define snd             second
#define lson            l,mid,rt<<1
#define rson            mid+1,r,rt<<1|1
/****************************>>>>>>DEBUG<<<<<<****************************/
#define out(x)          cout<<x<<""
/****************************>>>>SEPARATOR<<<<****************************/
string str[10010];
int T,n;
bool judge(int n)
{
    int i, j;
    int len;
    for(i = 0; i < n - 1; i++)
    {
        len = str[i].size();
        for(j = 0; j < len; j++)
            if(str[i][j] != str[i + 1][j]) break;
        if(j >= len) return false;
    }
    return true;
}
int main()
{
    FIN;
    CASE(T)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++) cin >> str[i];
        sort(str, str + n);
        if(judge(n)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 3630 / hdu 1671 Phone List 【Trie字典树 动态创建&静态创建】

标签:trie树   poj   hdu   数据结构   字典树   

原文地址:http://blog.csdn.net/acmore_xiong/article/details/47325569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!