poj_1671 Phone List

news/2024/7/8 16:29:05 标签: numbers, null, list, each, integer, build

Phone List

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

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1671

Problem Description

Given a list of phone numbers,determine if it is consistent in the sense that no number is the prefix ofanother. 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 directyour call to the emergency line as soon as you had dialled the first threedigits of Bob’s phone number. So this list would not be consistent.

 

Input

The first line of input givesa single integer, 1 <= t <= 40, the number of test cases. Each test casestarts 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 phonenumber 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

 

Source

2008 “Insigma International Cup” Zhejiang CollegiateProgramming Contest - Warm Up3

 

Recommend

lcy

 

题目大意:

         输入n个测试用例,m个字符串,判断是否存在某个字符串是否是另外一个字符串的子串,存在输出YES,不存在输出NO

解题思路:

         这是一个字典树的题目,首先建立字典树,边建立边判断,在一下情况返回false

1、  当前字符是最后一个字符,并且已经存在于字典树中

2、  当前字符不是这个字符串的最后一个字符,但是是另外一个字符传的最后一个字符

否则返回true

这个题目如果直接这样判断不释放上一次建树所用的内存的话要超时,所以每一个测试用例完之后要释放内存。

代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 9
using namespace std;
struct Trie
{
    Trie *next[maxn];
    bool Islast;//判断这个字符是否是某个单词的结尾
};


//把字符串插入字典树root中
bool build(Trie *root,char *str)
{
    Trie *p=root;
    Trie *q;
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        int id=str[i]-'0';
        if(p->next[id]!=NULL)
        {
			if(i==len-1)
			{//到最后一个数依然存在相同的数字
				return false;
			}
			//已经有单词在这里结尾
			if(p->next[id]->Islast==true)
				return false;
            p=p->next[id];			
            continue ;
        }
        //没有该节点
        q=(Trie *)malloc(sizeof(Trie));
        for(int j=0;j<=maxn;j++)
        {
            q->next[j]=NULL;
        }

		
		if(i==len-1)
		{
			q->Islast=true;
		}
		if(q->Islast==true)
		{
			p->next[id]=q;
			p=p->next[id];
			continue;
		}
		else 
		{
			
			q->Islast=false;
			p->next[id]=q;
			p=p->next[id];
		}
		
    }
	return true;
}

//释放内存
int dealTrie(Trie* T)
{
    int i;
    if(T==NULL)
        return 0;
    for(i=0;i<maxn;i++)
{
//释放每一个节点的内存
        if(T->next[i]!=NULL)
            dealTrie(T->next[i]);
    }
    free(T);
    return 0;
}

int main()
{
	int tc;
	int n;
	Trie *root;
	char ch[30];
	bool flag;
	scanf("%d",&tc);
	while(tc--)
	{
		flag=true;
		root=(Trie *)malloc(sizeof(Trie));
		for(int j=0;j<=maxn;j++)
		{
			root->next[j]=NULL;
		}
		scanf("%d",&n);
		getchar();
		while(n--)
		{
			
			gets(ch);
			if(build(root,ch)==false)
			{
				flag=false;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
		dealTrie(root);//释放内存空间,要一个一个节点释放,不能用free(root);
	}
    

    return 0;
}


 


http://www.niftyadmin.cn/n/862678.html

相关文章

hdu_4190 DistributingBallot Boxes

DistributingBallot Boxes Time Limit: 20000/10000 MS (Java/Others) Memory Limit:65536/32768 K (Java/Others) Total Submission(s): 310 Accepted Submission(s): 154 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid4190 Problem Description Tod…

SQL Server DBA 的工作清单

有许多不同类型的数据库管理员。 一些类型的数据库管理员致力于于开发领域&#xff0c;而其他的一部分更重视数据库性能的调整以及仍然有一部分数据库管理员则致力于管理SQL Server的业务。 依据数据库管理员的工作环境不同&#xff0c;他们将执行一定数量的不同的任务。为了区…

hdu_1251 统计难题

统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit:131070/65535 K (Java/Others) Total Submission(s): 9856 Accepted Submission(s): 3989 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1251 Problem Description Ignatius最近遇到一个…

看看DBA应该具备怎样的素质

近年来&#xff0c;我一直在和数据库管理员打交道&#xff0c;并直接面试了很多DBA职位。本文想概括一下IT行业对DBA的要求&#xff0c;以及国内DBA的新资现状。可以肯定地说&#xff0c;做一个高级DBA是很不错的职业。如果你打算成为一名DBA&#xff0c;那么希望本文起到抛砖引…

poj_1129 Channel Allocation

Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9669 Accepted: 4919 题目链接&#xff1a;http://poj.org/problem?id1129 Description When a radio station is broadcasting over a very largearea, repeaters are used to retransm…

2021.04.27【R语言】丨箱线图无法显示解决办法

摘要 箱线图主要用于反映原始数据分布的特征&#xff0c;还可以进行多组数据分布特征的比 较。箱线图的绘制方法是&#xff1a;先找出一组数据的上边缘、下边缘、中位数和两个四分位数&#xff1b;然后&#xff0c; 连接两个四分位数画出箱体&#xff1b;再将上边缘和下边缘与箱…

C++实现Adapter模式

由于CSDN长时间无法显示图片&#xff0c;本文已暂时迁移到&#xff1a; http://patmusing.blog.163.com/blog/static/13583496020100231823927/

poj_2387 Til the Cows Come Home

Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20550 Accepted: 6864 题目链接&#xff1a;http://poj.org/problem?id2387 Description Bessie is out in the field andwants to get back to the barn to get as much sleep as …