现在的位置: 首页 > 综合 > 正文

hdu1671字典树入门题

2019年02月09日 ⁄ 综合 ⁄ 共 2781字 ⁄ 字号 评论关闭

Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7110    Accepted Submission(s): 2441

Problem 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:
1. Emergency 911
2. Alice 97 625 999
3. 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
题意:给你一些人的电话号码,问你是否存在一个人的号码是另一个人的前缀。
小结:第一次写字典树,接触了下字典树的模版。
字典树主要有几个模块:
1.build建树
2.search查找
3.delete删除空间
代码:
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<cstdio>
#include<cstring>
#define maxn 2005
#define INF 0xfffffff
using namespace std;
char s[10005][20];
struct trie
{
    trie* next[11];
    int count;
    trie(){
        memset(next,0,sizeof(next));
        count=0;
    }
};
trie* root=0;
void build(char *a)
{
    int l=strlen(a);
    trie* p=root;
    for(int i=0;i<l;i++)
    {
        int t=a[i]-'0';
        if(p->next[t]==0)
        {
            p->next[t]=new trie();
        }
        p=p->next[t];
        p->count++;
    }
}
bool search(char *a)
{
    int l=strlen(a);
    trie* p=root;
    for(int i=0;i<l;i++)
    {
        int t=a[i]-'0';
        p=p->next[t];
    }
    return p->count>1;
}
void deletetrie(trie* p)
{
    for(int i=0;i<10;i++)
    {
        if(p->next[i]!=0)
        {
            deletetrie(p->next[i]);
        }
    }
    delete p;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        root=new trie();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s[i]);
            build(s[i]);
        }
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            if(search(s[i]))
            {
                flag=true;
                break;
            }
        }
        if(flag)
        printf("NO\n");
        else
        printf("YES\n");
        deletetrie(root);
    }
	return 0;
}

静态数组的写法:主要是把节点都定义在一个数组里面,0表示根节点,每次都得把所有的节点都初始化为0,因为以前的结点的状态会影响现在的状态。
代码:

#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<cstdio>
#include<cstring>
#define maxn 2005
#define INF 0xfffffff
using namespace std;
char s[10005][20];
struct trie
{
    int next[11];
    int count;
    trie(){
        memset(next,0,sizeof(next));
        count=0;
    }
}T[100005];
int num=0;
void build(char *a)
{
    int l=strlen(a);
    int p=0;
    for(int i=0;i<l;i++)
    {
        int t=a[i]-'0';
        if(T[p].next[t]==0)
        {
           T[p].next[t]=++num;
        }
        p=T[p].next[t];
        T[p].count++;
    }
}
bool search(char *a)
{
    int l=strlen(a);
    int p=0;
    for(int i=0;i<l;i++)
    {
        int t=a[i]-'0';
        p=T[p].next[t];
    }
    return T[p].count>1;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<=100005;i++)
        {
            memset(T[i].next,0,sizeof(T[i].next));
            T[i].count=0;
        }
        num=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s[i]);
            build(s[i]);
        }
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            if(search(s[i]))
            {
                flag=true;
                break;
            }
        }
        if(flag)
        printf("NO\n");
        else
        printf("YES\n");
    }
	return 0;
}




【上篇】
【下篇】

抱歉!评论已关闭.