POJ 3630 Phone List(trie树的简单应用)
题目链接:http://poj.org/problem?id=3630
题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES
思路:简单的trie树应用,插入的过程中维护到当前节点是不是字符串这个布尔量即可,同时判断是否存在上述情况。
code:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std; const int KIND = ;
const int MAXN = ;
struct trie
{
bool isString; // 标志到当前节点是否是字符串
trie* next[KIND];
trie()
{
isString = false;
for (int i = ; i < KIND; ++i) next[i] = NULL;
}
}; trie node[MAXN];
trie* root;
bool flag;
int k; void Insert(string ss)
{
trie* temp = root;
int len = ss.size();
for (int i = ; i < len; ++i)
{
int curr = ss[i] - '';
if (temp->next[curr] != NULL) // 判断前缀在前面出现的情况
{
if (temp->next[curr]->isString)
{
flag = false;
return;
}
}
else temp->next[curr] = &node[k++];
temp = temp->next[curr];
}
temp->isString = true;
for (int i = ; i < KIND; ++i) // 判断前缀在后面出现的情况
{
if (temp->next[i])
{
flag = false;
return;
}
}
} int main()
{
int nCase;
cin >> nCase;
while (nCase--)
{
flag = true;
k = ;
memset(node, , sizeof(node));
root = &node[]; int n;
cin >> n; string str;
for (int i = ; i < n; ++i)
{
cin >> str;
if (flag) Insert(str);
}
if (flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
return ;
}
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 tha ...
- POJ 3630 Phone List | Trie 树
题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- poj 3630 Phone List(字典树)
题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- hdu 1251 统计难题(trie 树的简单应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给你多个字符串,求以某个字符串为前缀的字符串数量. 思路:简单的trie数应用,在trie ...
- POJ 3630 Phone List Trie题解
Trie的应用题目. 本题有两个难点了: 1 动态建立Trie会超时,须要静态建立数组,然后构造树 2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候.2)插入顺序 ...
随机推荐
- 九度OJ 题目1534:数组中第K小的数字(二分解)
题目链接:点击打开链接 题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6 ...
- 二道shell面试题
1.按照给出的运行结果,编写一个名为xunhuan 的shell过程(用循环语句). 0 10 210 3210 43210 543210 6543210 76543210 876543210 2.编 ...
- Spring AOP AspectJ Pointcut 表达式例子
主要来源:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...
- xcode UIImage图片拉伸
图片拉伸 +(UIImage*)wlisWithImage:(NSString *)name{ //获取图片 UIImage * img=[UIImage imageNamed:name]; //获取 ...
- CentOS安装常用软件
下载第三方库rpmforge,找到合适自己版本的rpmforge下载,用以支持NTFS格式硬盘和MP3格式音频或其他 http://pkgs.repoforge.org/rpmforge-releas ...
- java项目打jar包
首先 在工程中,右键项目,有个export,选择JAR File,就能导出jar包. 一.java项目没有导入第三方jar包 1. 首先在Eclipse中打开项目, 右键点击项目,选择“Export” ...
- Enum基础
enum ColorE { RED, GREEN, BLUE; } public class GetEnumContent { public static void main(Stri ...
- justAP1.3.0版发布了
justAP是一个简单的.易于使用的php运行环境,适合php开发人员使用.与wamp.xampp等不同,它仅仅包含Apache httpd和Php,这也是它名字的来由(justAP=just Apa ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 1 2 5 10 20 --> 800
用1元 2元 5元 10元 20元的钞票凑成800元的方法种数计算,使用了动态规划. 结果没打出来,只是保留在函数里各个vector中,调试可看所有结果. 优点:快 缺点:占空间占内存 耗时时间测试: ...