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)插入顺序 ...
随机推荐
- IP地址获取到为0:0:0:0:0:0:0:1
引用 13 楼 oXiaoShe 的回复: Quote: 引用 11 楼 ahjsdzm 的回复: [Quote=引用 9 楼 huazaiyou 的回复:]最近在进行web开发时,遇到了reques ...
- 奇怪的问题:android:focusable和android:clickable造成ListView的点击不了
今天花了我很长时间,才解决一个很奇怪的问题,就是在ListView的点击反应不了的问题…… 在ListView中,如果其中一个元素设置为android:focusable="true&quo ...
- poj1833---字典序算法
题意:给定一个序列,求它的下k个排列 #include <stdio.h> #include <stdlib.h> int cmp(const void *a,const vo ...
- jquery绑定事件,解绑事件
unbind([type],[data]) 是 bind()的反向操作,从每一个匹配的元素中删除绑定的事件.如果没有参数,则删除所有绑定的事件.你可以将你用bind()注册的自定义事件取消绑定.如果提 ...
- xcode - 移动手势
#import "ViewController.h" @interface ViewController () /** 创建一个UIView */ @property(nonato ...
- JavaScript之JS实现动画效果
在前面的随笔中介绍了如何用DOM技术修改文档的央样式信息,用JavaScript添加样式信息可以节约我们的时间和精力,但总的来说,CSS仍是完成这类任务的最佳工具.但是有一个应用领域是目前的CSS无能 ...
- sql server 2000 和 sql server 2005 数据库连接字符串区别
//sql server 2000 <add name="Connection" connectionString="Data Source=.;Initial C ...
- VS2013 快捷键 与 RESHARPER 冲突
1.VS设置工具-->选项-->环境-->键盘-->重置 2.RESHARPER -->Options-->Environment → Keyboard & ...
- BZOJ 3277: 串/ BZOJ 3473: 字符串 ( 后缀数组 + RMQ + 二分 )
CF原题(http://codeforces.com/blog/entry/4849, 204E), CF的解法是O(Nlog^2N)的..记某个字符串以第i位开头的字符串对答案的贡献f(i), 那么 ...
- php json_encode url链接出现双转义字符‘\\’和中文被编码的解决方法
在PHP开发中 json_encode方法使用的频率还是蛮高的. 当数据中出现中文是,经过json_encode后,中文被编码了,不是显示的原中文字符. 可以通过urlencode编码后,在使用jso ...