HDU 1671 Phone List(字典树Trie)

解题思路:

判断是否有一个字符串是另一个字符串的前缀,直接用字典树搞。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <map>
#define LL long long
using namespace std;
typedef struct Trie_Node
{
	bool isword;
	struct Trie_Node *next[10];
}Trie;
void insert(Trie *root, char *word)
{
	Trie *p = root;
	int i = 0;
	while(word[i] != '\0')
	{
		if(p->next[word[i]-'0'] == NULL)
		{
			Trie *temp = new Trie;
			temp->isword = false;
			for(int j=0;j<10;j++)
				temp->next[j] = NULL;
			p->next[word[i]-'0'] = temp;
		}
		p = p->next[word[i]-'0'];
		i++;
	}
	p->isword = true;
}
int Find(Trie *root, char *word)
{
	Trie *p = root;
	int i = 0;
	int c = 0;
	while(word[i] != '\0')
	{
		if(p->next[word[i] - '0'] == NULL)
			return 0;
		p = p->next[word[i]-'0'];
		if(p->isword) c++;
		i++;
	}
	return c;
}
void del(Trie *root)
{
    for(int i=0;i<10;i++)
    {
        if(root->next[i] != NULL)
            del(root->next[i]);
    }
    free(root);
}
char s[10010][15];
int main()
{
	int T, N;
	scanf("%d", &T);
	while(T--)
	{
		Trie *root = new Trie;
		root->isword = false;
		for(int i=0;i<10;i++)
			root->next[i] = NULL;
		scanf("%d", &N);
		for(int i=1;i<=N;i++)
		{
			scanf("%s", s[i]);
			insert(root, s[i]);
		}
		bool ok = true;
		for(int i=1;i<=N;i++)
		{
			if(Find(root,s[i]) > 1)
			{
				ok = false;
				break;
			}
		}
		if(ok) printf("YES\n");
		else printf("NO\n");
		del(root);
	}
	return 0;
}
时间: 2024-10-24 21:23:45

HDU 1671 Phone List(字典树Trie)的相关文章

HDU 1671 Phone List (字典树)

Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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 catalogu

字典树 Trie (HDU 1671)

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

HDU 1247 Hat&#39;s words(字典树Trie)

解题思路: 判断给出的单词是否恰好由另外两个单词组成,用栈保存每个子字符串的节点,从这个节点出发判断剩下的字符串是否在字典树中即可. #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <map> #include <sta

hdu 1251 统计难题 字典树

// hdu 1251 统计难题 字典树 // // 题目大意: // // 有一系列的单词表,以空行结尾,之后会有一些字母串,找出以这些字符串 // 作为前缀的单词的个数 // // // 解题思路: // // 字典树 Trie,在插入字符串的时候每遇到一个节点,该节点的值++.查找的时候 // 字符串时,如果找到了,那么返回当前的val,否则返回0,因为没有以这个字符串 // 为前缀的单词. // // // 感悟: // // 这段时间想学学数据结构,就看了看刘老的大白书,感觉用数组挺巧

[POJ] #1003# 487-3279 : 桶排序/字典树(Trie树)/快速排序

一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or

字典树Trie

字典树Trie Trie,又称字典树,前缀树(prefix tree),是一种树形结构,用于保存大量的字符串. 它的优点是:利用字符串的公共前缀来节约存储空间.查找.插入复杂度为O(n),n为字符串长度. 它有3个基本性质: 1. 根节点不包含字符,除根节点外每一个节点都只包含一个字符. 2. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串. 3. 每个节点的所有子节点包含的字符都不相同. 假设有abc,abcd,abd, b, bcd,efg,hii这7个单词,可构建字典树

HDU 1075 map or 字典树

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4069 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

HDU 4825 Xor Sum 字典树+位运算

点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 151 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus

字典树Trie树

一.字典树 字典树--Trie树,又称为前缀树(Prefix Tree).单词查找树或键树,是一种多叉树结构. 上图是一棵Trie树,表示了关键字集合{"a", "to", "tea", "ted", "ten", "i", "in", "inn"} .从上图可以归纳出Trie树的基本性质: 1. 根节点不包含字符,除根节点外的每一个子节点都包含一