POJ 3630 Phone List(trie树的简单应用)

题目链接:http://poj.org/problem?id=3630

题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES

思路:简单的trie树应用,插入的过程中维护到当前节点是不是字符串这个布尔量即可,同时判断是否存在上述情况。

code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int KIND = 10;
 8 const int MAXN = 100005;
 9 struct trie
10 {
11     bool isString;      // 标志到当前节点是否是字符串
12     trie* next[KIND];
13     trie()
14     {
15         isString = false;
16         for (int i = 0; i < KIND; ++i) next[i] = NULL;
17     }
18 };
19
20 trie node[MAXN];
21 trie* root;
22 bool flag;
23 int k;
24
25 void Insert(string ss)
26 {
27     trie* temp = root;
28     int len = ss.size();
29     for (int i = 0; i < len; ++i)
30     {
31         int curr = ss[i] - ‘0‘;
32         if (temp->next[curr] != NULL)   // 判断前缀在前面出现的情况
33         {
34             if (temp->next[curr]->isString)
35             {
36                 flag = false;
37                 return;
38             }
39         }
40         else temp->next[curr] = &node[k++];
41         temp = temp->next[curr];
42     }
43     temp->isString = true;
44     for (int i = 0; i < KIND; ++i)  // 判断前缀在后面出现的情况
45     {
46         if (temp->next[i])
47         {
48             flag = false;
49             return;
50         }
51     }
52 }
53
54 int main()
55 {
56     int nCase;
57     cin >> nCase;
58     while (nCase--)
59     {
60         flag = true;
61         k = 1;
62         memset(node, 0, sizeof(node));
63         root = &node[0];
64
65         int n;
66         cin >> n;
67
68         string str;
69         for (int i = 0; i < n; ++i)
70         {
71             cin >> str;
72             if (flag) Insert(str);
73         }
74         if (flag) cout << "YES" << endl;
75         else cout << "NO" << endl;
76     }
77     return 0;
78 }
时间: 2024-11-17 03:38:14

POJ 3630 Phone List(trie树的简单应用)的相关文章

poj 3630 Phone List trie树

Phone List 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: Emergency 911 Alice 97 625 999 Bob 91 12 54 26 In this case,

poj 3630 Phone List (字典树 +静态字典树)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22235   Accepted: 6885 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

POJ训练计划2418_Hardwood Species(Trie树)

解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; struct node { int v; node *next[256]; }; int cnt=0,q; char ch[100],str1[100]; node *newnode() { node *p=new node; p->

[POJ 1204]Word Puzzles(Trie树暴搜)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

poj 2418 Hardwood Species (trie树)

poj   2418   Hardwood Species http://poj.org/problem?id=2418 trie树+dfs 题意: 给你多个单词,问每个单词出现的频率. 方法:通过字典树,将所有单词放入树中,通过dfs遍历(题目要求按ASSIC码顺序输出单词及其频率),dfs可满足 注意:单词中不一定只出现26个英文字母,ASSIC码表共有256个字符 1 #include <stdio.h> 2 #include <string.h> 3 #include &l

[ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 29736   Accepted: 7843 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st

poj 2945 Find the Clones trie树的简单应用

题意: 给n个长m的字符串,统计他们的出现频率,输出出现1次的有几种,出现2次的有几种...出现n次的有几种.n<=20000,m<=20. 分析: 也可以用排序,map水的,但还是写个trie树也不麻烦,trie树我觉得就是针对字符串的hash表,效率如果数据大点是比暴力解法高很多的,另外写的时候不小心把index定义成char,n<256完全没问题..调了一个小时也是醉了. 代码: //poj 2945 //sep9 #include <iostream> using n

Trie树的简单描述(需后续总结)

http://www.cnblogs.com/pony1993/archive/2012/07/18/2596730.html 字典树(Trie树) 字典树,又称单词查找树,Trie树,是一种树形结构,典型应用是用于统计,排序和保存大量的字符串,所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀来节约存储空间,最大限度的减少无谓的字符串比较,查询效率比哈希表高. 它有三个基本性质,根节点不包含字符,除根节点外每一个节点都只包含一个字符,从根节点到某一节点,路径上经过的字符连

POJ 2513 Colored Sticks (Trie树+并查集+欧拉路)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 31490   Accepted: 8320 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st