nyoj 163 Phone List && poj 3630 Phone List <字典树 动态建树&&静态建树>

33 篇文章 0 订阅
10 篇文章 0 订阅

Phone List

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述

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.

输入
The first line of input gives a single integer, 1 ≤ t ≤ 10, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 100000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
输出
For each test case, output "YES" if the list is consistent, or "NO" otherwise.
样例输入
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
样例输出
NO
YES
来源
POJ
上传者
iphxer


 

思路:

从大往小将字符串加入字典树,,如果将这个字符串都加进去没有增加新的字典树分支就说明这个字符串是以前字符串的前缀。。就可以输出NO了

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n;
struct node{
	char hh[10];
	int ll;
}ch[100100];
struct trie{
	struct trie *hai[10];
};
struct trie root;
bool cmp(node xx,node yy)
{
	if (xx.ll!=yy.ll)
	return xx.ll>yy.ll;
	for (int i=0;i<xx.ll;i++)
	{
		if (i==xx.ll-1)
			return xx.hh[i]>yy.hh[i];
		else
		{
			if (xx.hh[i]!=yy.hh[i])
			return xx.hh[i]>yy.hh[i];
		}
	}
}
int jia(char xx[])
{
	struct trie *kk;
	kk=&root;
	int ll=strlen(xx);
	for (int i=0;i<ll;i++)
	{
		if (kk->hai[xx[i]-'0'])
		{
			if (i==ll-1)
			return true;
		}
		else
		{
			kk->hai[xx[i]-'0']=new trie;
			memset(kk->hai[xx[i]-'0'],0,sizeof(trie));
		}
		kk=kk->hai[xx[i]-'0'];
	}
	return false;
}
void Freedom(trie* p) //释放new的内存。。。。看别人博客说这是一种态度.-。-  
{   
    int i;   
    for(i=0;i<10;i++)
	{   
        if(p->hai[i]!=NULL)   
        Freedom(p->hai[i]);   
    }   
    delete p;   
}
int main()
{
	int t;scanf("%d",&t);
	while (t--)
	{
		scanf("%d",&n);
		for (int i=0;i<n;i++)
		{
			scanf("%s",ch[i].hh);
			ch[i].ll=strlen(ch[i].hh);
		}
		sort(ch,ch+n,cmp);
		for (int i=0;i<10;i++)
		root.hai[i]=0;
		bool fafe=true;
		for (int i=0;i<n;i++)
		{
			if (jia(ch[i].hh))
			{
				fafe=false;
				break;
			}
		}
		if (fafe)
		printf("YES\n");
		else
		printf("NO\n");
	/*	printf("guo\n");
		for (int i=0;i<10;i++)
		Freedom(root.hai[i]);//这一点好像有问题。。。。
		printf("guo\n");*/
	}
	return 0;
}


 动态建树:需释放内存

带释放内存:内存突然少了几倍-.-好开心。。。。

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n;
struct node{
    char hh[10];
    int ll;
}ch[101000];
struct trie{
    struct trie *hai[10];
};
struct trie *root;
bool cmp(node xx,node yy)
{
    if (xx.ll!=yy.ll)
    return xx.ll>yy.ll;
    for (int i=0;i<xx.ll;i++)
    {
        if (i==xx.ll-1)
            return xx.hh[i]>yy.hh[i];
        else
        {
            if (xx.hh[i]!=yy.hh[i])
            return xx.hh[i]>yy.hh[i];
        }
    }
}
int jia(char xx[])
{
    struct trie *kk;
    kk=root;
    int ll=strlen(xx);
    for (int i=0;i<ll;i++)
    {
        if (kk->hai[xx[i]-'0'])
        {
            if (i==ll-1)
            return true;
        }
        else
        {
            kk->hai[xx[i]-'0']=new trie;
            memset(kk->hai[xx[i]-'0'],0,sizeof(trie));
        }
        kk=kk->hai[xx[i]-'0'];
    }
    return false;
}
void Freedom(trie* p) //释放new的内存。。。。看别人博客说这是一种态度.-。-  
{   
    int i;   
    for(i=0;i<10;i++)
    {   
        if(p->hai[i]!=NULL)   
        Freedom(p->hai[i]);   
    }   
    delete p;   
}
int main()
{
    int t;scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n);
        for (int i=0;i<n;i++)
        {
            scanf("%s",ch[i].hh);
            ch[i].ll=strlen(ch[i].hh);
        }
        sort(ch,ch+n,cmp);
        root=new trie();
        memset(root,0,sizeof(trie));
        bool fafe=true;
        for (int i=0;i<n;i++)
        {
            if (jia(ch[i].hh))
            {
                fafe=false;
                break;
            }
        }
        if (fafe)
        printf("YES\n");
        else
        printf("NO\n");
        Freedom(root);
    }
    return 0;
}


静态建树:

开始已分配内存单元-.-时间上快-.-

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,wei;
struct node{
    char hh[10];
    int ll;
}ch[101000];
struct trie{
    struct trie *hai[10];
}di[1001000];
struct trie *root;
bool cmp(node xx,node yy)
{
    if (xx.ll!=yy.ll)
    return xx.ll>yy.ll;
    for (int i=0;i<xx.ll;i++)
    {
        if (i==xx.ll-1)
            return xx.hh[i]>yy.hh[i];
        else
        {
            if (xx.hh[i]!=yy.hh[i])
            return xx.hh[i]>yy.hh[i];
        }
    }
}
int jia(char xx[])
{
    struct trie *kk;
    kk=root;
    int ll=strlen(xx);
    for (int i=0;i<ll;i++)
    {
        if (kk->hai[xx[i]-'0'])
        {
            if (i==ll-1)
            return true;
        }
        else
        {
            kk->hai[xx[i]-'0']=&di[wei++];
            memset(kk->hai[xx[i]-'0'],0,sizeof(trie));
        }
        kk=kk->hai[xx[i]-'0'];
    }
    return false;
}
int main()
{
    int t;scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n);
        for (int i=0;i<n;i++)
        {
            scanf("%s",ch[i].hh);
            ch[i].ll=strlen(ch[i].hh);
        }
        sort(ch,ch+n,cmp);
        wei=0;
		root=&di[wei++];
        memset(root,0,sizeof(trie));
        bool fafe=true;
        for (int i=0;i<n;i++)
        {
            if (jia(ch[i].hh))
            {
                fafe=false;
                break;
            }
        }
        if (fafe)
        printf("YES\n");
        else
        printf("NO\n");
    }
    return 0;
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值