题目链接: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树的简单应用)的更多相关文章

  1. poj 3630 Phone List trie树

    Phone List Description Given a list of phone numbers, determine if it is consistent in the sense tha ...

  2. POJ 3630 Phone List | Trie 树

    题目: 给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前缀.多组数据,数据组数不超过 40. 题解: 前缀问题一般都用Trie树解决: 所以跑一个T ...

  3. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

  4. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  5. [POJ 1204]Word Puzzles(Trie树暴搜&amp;AC自己主动机)

    Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...

  6. poj 3630 Phone List(字典树)

    题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...

  7. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  8. hdu 1251 统计难题(trie 树的简单应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给你多个字符串,求以某个字符串为前缀的字符串数量. 思路:简单的trie数应用,在trie ...

  9. POJ 3630 Phone List Trie题解

    Trie的应用题目. 本题有两个难点了: 1 动态建立Trie会超时,须要静态建立数组,然后构造树 2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候.2)插入顺序 ...

随机推荐

  1. uvalive5818 uva12376 As Long as I Learn, I Live

    题意:给出一个又向图每个图有权值和编号(正方形里的是编号),从第0号节点开始每次向当前节点所连的点中权值最大的节点移动(不会存在权值相同的节点),问最后所在的节点编号和经过的节点的权值之和. 解:模拟 ...

  2. c reference

    1,函数:strdup复制字符串函数原型定义:char * strdup(const char *s);函数说明:strdup()会先用maolloc()配置与参数s字符串相同的空间大小,然后将参数s ...

  3. JSON.parse()的异常怎么处理;

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. padding-bottom布局解析;

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. hdu3280Equal Sum Partitions (区间DP)

    Problem Description An equal sum partition of a sequence of numbers is a grouping of the numbers (in ...

  6. CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用

    线段树的区间更新与区间求和...一颗这样的线段树用两次... 先扫描1~k...用线段树统计出每个操作执行的次数... 那么每个操作就变成了 op. l  , op.r , op.c= times* ...

  7. Mac之vim普通命令使用

    Mac之vim普通命令使用 标签: vim命令 高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx 开始记录宏,并将结果存入寄存器x q 退出记录模式 @x ...

  8. BZOJ 1492: [NOI2007]货币兑换Cash( dp + 平衡树 )

    dp(i) = max(dp(i-1), x[j]*a[i]+y[j]*b[i]), 0<j<i. x, y表示某天拥有的最多钱去买金券, 金券a和金券b的数量. 然后就很明显了...平衡 ...

  9. easyui 验证控件 tooltip message显示位置

    找了半天才发现是这个属性在控制,tipPosition:'left',官网那个demo,误人子弟.

  10. ListView 条目加载上滑下滑首尾缩放动画实现

    要实现这个效果,只需要再适配器getView之前,给每个条目的view设置相应的动画即可. 首先需要2个动画的xml文件. 在res下新建anim文件夹:(res/anim) 第一个动画xml文件: ...