题目:

给定 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. 打包时ElementUI使vendor.js文件体量过大优化方法

    <h1> 1.在index.html中以CDN的方式引入 </h1> <p> 引入的时候注意:要先在引入之前引入VUE否则会报undedined prototype ...

  2. Python 统计不同url svn代码变更数

    #!/bin/bash/python # -*-coding:utf-8-*- #svn统计不同url代码行数变更脚本,过滤空行,不过滤注释. import subprocess,os,sys,tim ...

  3. ionic 命令cordova

    安装android platform : ionic platform add android 安装一维码cordova插件 :cordova plugin add https://github.co ...

  4. BZOJ1965: [Ahoi2005]SHUFFLE 洗牌(exgcd 找规律)

    Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 989  Solved: 660[Submit][Status][Discuss] Description ...

  5. Eclipse编写JavaFX环境配置

    配置eclipse用于写JavaFX:1.确定JRE中有jfxrt.jar---jdk82.选中项目-->属性-->Java Build Path3.Libraries-->jre包 ...

  6. Linux中用户与用户组管理

    1.基础知识 Linux作为一种多用户的操作系统(服务器系统),允许多个用户同时登陆到系统上,并响应每个用户的请求. 任何需要使用操作系统的用户,都需要一个系统账号,账号分为:管理员账号与普通用户账号 ...

  7. 深入理解PHP7之zval

    PHP7已经发布, 如承诺, 我也要开始这个系列的文章的编写, 今天我想先和大家聊聊zval的变化. 在讲zval变化的之前我们先来看看zval在PHP5下面是什么样子 PHP5zval回顾在PHP5 ...

  8. 超简单开发自己的php框架一点都不难

    (转)https://blog.csdn.net/qq_33862644/article/details/79344331 写框架的极简思路: 接收,打印参数想怎么弄.如 获取配置文件的方法,根据传过 ...

  9. C语言实例解析精粹学习笔记——26

    实例26:阿拉伯数字转换为罗马数字,将一个整数n(1~9999)转换为罗马数字,其中数字和罗马数字的对应关系如下: 原书中的开发环境很老,我也没有花心思去研究.自己在codeblocks中进行开发的, ...

  10. C语言进阶——const 和 volatile 分析09

    const只读变量: const修饰的变量是只读的,本质还是一个变量 const修饰的局部变量在栈上分配空间 const修饰的全局变量在全局函数区分配资源空间 const只在编译器有用,在运行期无用 ...