题目:

给定 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. ant Design表单验证笔记

    1.pattern正则验证 <Col md={12} sm={24}> <FormItem {...formItemLayout} label="班数"> ...

  2. 转 Ubuntu 下 vim 搭建python 环境 配置

    1. 安装完整的vim# apt-get install vim-gnome 2. 安装ctags,ctags用于支持taglist,必需!# apt-get install ctags 3. 安装t ...

  3. PHPExcel 中文使用手册详解

    /** * * execl数据导出 * 应用场景:订单导出 * @param string $title 模型名(如Member),用于导出生成文件名的前缀 * @param array $cellN ...

  4. 图解HTTP总结(7)——确保Web安全的HTTPS

    HTTP 主要有这些不足, 例举如下.       通信使用明文( 不加密) , 内容可能会被窃听.       不验证通信方的身份, 因此有可能遭遇伪装. 无法证明报文的完整性, 所以有可能已遭篡改 ...

  5. python scrapy实战糗事百科保存到json文件里

    编写qsbk_spider.py爬虫文件 # -*- coding: utf-8 -*- import scrapy from qsbk.items import QsbkItem from scra ...

  6. 007---Django的视图层

    视图函数 一个视图函数,简称视图,是一个简单的python函数.它接收web请求并且返回web响应. 1.一张网页的HTML内容 2.一个重定向 3.一个404错误 4.一个xml文档 5.一个字符串 ...

  7. 使用dataframe解决spark TopN问题:分组、排序、取TopN和join相关问题

    package com.profile.mainimport org.apache.spark.sql.expressions.Windowimport org.apache.spark.sql.fu ...

  8. 5-1 练习css 总结

    1. 边框 border:3px dotted; border: 2px solid yellow; 背景颜色 background-color: red; 外攘 margin:20px 0 20px ...

  9. sprintf()函数使用异常

    调试STM32F103,比如如下代码:使用springf函数,这个函数是把最后两个参数先格式化成字符串 ,输出到ERROR_STRING,如果他们合并的长度大于30会出现深情况? ] sprintf( ...

  10. linux下vi的复制,黏贴,删除,撤销,跳转等命令-费元星

    前言    在嵌入式linux开发中,进行需要修改一下配置文件之类的,必须使用vi,因此,熟悉 vi 的一些基本操作,有助于提高工作效率. 一,模式vi编辑器有3种模式:命令模式.输入模式.末行模式. ...