1. 程式人生 > >nyoj 163 Phone List && poj 3630 Phone List

nyoj 163 Phone List && poj 3630 Phone List

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
上傳者

思路:

從大往小將字串加入字典樹,,如果將這個字串都加進去沒有增加新的字典樹分支就說明這個字串是以前字串的字首。。就可以輸出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;
}