题目:

给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S, T ,使得 S 是 T 的前
缀。多组数据,数据组数不超过 40.


题解:

前缀问题一般都用Trie树解决:

所以跑一个Tire树,记录一下每个节点是不是结尾就好

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100010
#define Z 10
using namespace std;
int T,n,tot;
struct node
{
int trans[Z],bo;
void clear()
{
memset(trans,0,sizeof(trans));
bo=0;
}
}tr[N];
char s[20];
int insert(char *s)
{
int len=strlen(s),u=1,flag=0;
for (int i=0;i<len;i++)
{
if (!tr[u].trans[s[i]-'0'])
tr[tr[u].trans[s[i]-'0']=++tot].clear();
else if (i==len-1)
flag=1;
u=tr[u].trans[s[i]-'0'];
if (tr[u].bo) flag=1;
}
tr[u].bo=1;
return flag;
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
tr[tot=1].clear();
int ans=0;
for (int i=1;i<=n;i++)
{
scanf("%s",s);
if (insert(s)) ans=1;
}
if (!ans) puts("YES");
else puts("NO");
}
return 0;
}

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 2513 Colored Sticks (trie 树)

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

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

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

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

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

  5. poj 3630 Phone List(字典树)

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

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

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

  7. POJ 3630 Phone List Trie题解

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

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

    解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include < ...

  9. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

随机推荐

  1. LeetCode426.Convert Binary Search Tree to Sorted Doubly Linked List

    题目 Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right point ...

  2. Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 36

    例: <aop:config> <aop:pointcut expression="execution(* com.zsn.Service.Impl.*.*(..))&qu ...

  3. Maven - pom.xml常用元素

    基本坐标信息:

  4. POJ:3977-Subset(双向搜索)

    Subset Time Limit: 30000MS Memory Limit: 65536K Total Submissions: 5961 Accepted: 1129 Description G ...

  5. [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机+DP)

    Description 贝西正在打格斗游戏.游戏里只有三个按键,分别是“A”.“B”和“C”.游戏中有 N 种连击 模式,第 i 种连击模式以字符串 Si 表示,只要贝西的按键中出现了这个字符串,就算 ...

  6. [CodeForces950C]Zebras

    Description 题目地址: Codeforces 题意:给你一串只含01的字符串,判断能否将字符串分为k个子序列,使得子序列满足以下条件: 开头和结尾都是0 相邻的2个数是01或者10 如0, ...

  7. 30-RoutingMiddleware介绍以及MVC引入

    1-构建路由 public class Startup { // This method gets called by the runtime. Use this method to add serv ...

  8. ansible-1

    ansible与salt对比: 相同: 都是为了同时在多台机器上执行相同的命令 都是python开发 不同: agent(saltstack需要安装.ansible不需要) 配置(salt配置麻烦,a ...

  9. 2 Mongodb基本操作

    1.基本操作 MongoDB将数据存储为一个文档,数据结构由键值(key=>value)对组成 MongoDB文档类似于JSON对象,字段值可以包含其他文档.数组.文档数组 安装管理mongod ...

  10. svn Previous operation has not finished; run 'cleanup' if it was interrupted

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted Usually, a ...