poj 3630 Phone List
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 100005
using namespace std;
int Trie[N][];
int nodeN;
int main()
{
int t, n, i, j, isPrefix, flag;
char ph[];
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
isPrefix=;
nodeN=;
memset(Trie[], , sizeof(int)*);
for(i=; i<n; ++i)
{
scanf("%s", ph);
flag=;
int cur=;
if(!isPrefix)
for(j=; ph[j]; ++j)
{
int k=ph[j]-'';
if(!Trie[cur][k])
{
if(i!= && !flag)//利用第一个字符串建立一个初始的trie树
{
flag=;//建立了新节点(如果当前 cur 节点还有孩子说明没有公共前缀, 否则说明当前路已经走到了尽头,产生了公共前缀)
int c;
for(c=; c<; ++c)//检查时候当前节点cur是否有孩子节点
if(Trie[cur][c])
break;
if(c>=)
38 {
isPrefix=; //没有孩子节点,则说明产生了公共前缀
break;
}
}
Trie[cur][k]=++nodeN;
memset(Trie[nodeN], , sizeof(int)*);
}
cur=Trie[cur][k];
}
if(flag== && i!=)//如果不是第一个字符串,而且在遍历整个树的时候没有发现建立新的节点,则说明当前字符串必然是之前某个字符串的公共前缀
isPrefix=;
}
if(isPrefix)
printf("NO\n");
else printf("YES\n");
}
return ;
}
/*
好不容易想用一下链表来写一下,还超时啊。。。。
*/
1 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
class Trie
{
public:
Trie *ch[];
Trie()
{
memset(ch, , sizeof(ch));
}
}; int main()
{
int t, n, i, j, isPrefix, flag;
char ph[];
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
Trie *root=new Trie();
isPrefix=;
for(i=; i<n; ++i)
{
scanf("%s", ph);
flag=;
Trie * cur=root;
if(!isPrefix)
for(j=; ph[j]; ++j)
{
int k=ph[j]-'';
if(!cur->ch[k])
{
if(i!= && !flag)
{
flag=;
int c;
for(c=; c<; ++c)
if(cur->ch[c])
break;
if(c>=)
{
isPrefix=;
break;
}
}
cur->ch[k]=new Trie();
}
cur=cur->ch[k];
}
if(flag== && i!=)
isPrefix=;
}
if(isPrefix)
printf("NO\n");
else printf("YES\n");
}
return ;
}
poj 3630 Phone List的更多相关文章
- POJ 3630 Phone List(trie树的简单应用)
题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...
- poj 3630 Phone List(字典树)
题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...
- 【POJ 3630】 Phone List
[题目链接] http://poj.org/problem?id=3630 [算法] 字典树 [代码] #include <algorithm> #include <bitset&g ...
- POJ 3630 , HDU 1671 Phone List - from lanshui_Yang
这道题也是一道找前缀的问题,很自然地要用到Trie树,但是如果用动态Trie树(即用指针开辟内存)的话,虽然在HDU上可以过(可能是HDU的数据比较水),但在POJ上会TLE , 所以这道题只能用静态 ...
- HDU 1671 Phone List(POJ 3630)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 3630 Phone List Trie题解
Trie的应用题目. 本题有两个难点了: 1 动态建立Trie会超时,须要静态建立数组,然后构造树 2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候.2)插入顺序 ...
- POJ 3630 Phone List(字符串前缀重复)题解
Description Given a list of phone numbers, determine if it is consistent in the sense that no number ...
- poj 3630 Phone List 贪心
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23722 Accepted: 7289 Descr ...
- poj 3630 Phone List trie树
Phone List Description Given a list of phone numbers, determine if it is consistent in the sense tha ...
随机推荐
- Curator leader 选举(一)
要想使用Leader选举功能,需要添加recipes包,可以在maven中添加如下依赖: <dependency> <groupId>org.apache.curator< ...
- c和oc小知识
1.const const 修饰了*p1 / *p2 const int * p1=&age; int const * p2=&age;//和上面的意义一样 ,换句话说就是 在 “ * ...
- swiper插件 纵向内容超出一屏解决办法
1.css .swiper-slide { overflow: auto; } 2.js var swiper = new Swiper('.swiper-container', { directio ...
- [转]Android ListView 与 RecyclerView 对比浅析—缓存机制
从源码角度剖析ListView 与 RecyclerView 缓存机制的不同 https://zhuanlan.zhihu.com/p/23339185 原文地址:http://dev.qq.com/ ...
- python 函数之装饰器,迭代器,生成器
装饰器 了解一点:写代码要遵循开发封闭原则,虽然这个原则是面向对象开发,但也适用于函数式编程,简单的来说,就是已经实现的功能代码不允许被修改但 可以被扩展即: 封闭:已实现功能的代码块 开发:对扩张开 ...
- 八大排序算法Java
目录(?)[-] 概述 插入排序直接插入排序Straight Insertion Sort 插入排序希尔排序Shells Sort 选择排序简单选择排序Simple Selection Sort 选择 ...
- Centos7 Docker 多主机 容器互连--基于OVS
来一张自己画的图,mark:2016年6月27日17:09:14 自己理解,如有错误 多谢指教. centos7, 部署OVS和docker.以及基于centos6.8的ssh images 命令. ...
- PHP如何使用GeoIP数据库
1.首先下载GeoIP的IP库.参考<利用GeoIP数据库及API进行地理定位查询>.下载后解压,得到一个GeoIP.dat文件 2.新建一个文件geoip.inc.内容为 <?ph ...
- 安卓刷机--fastboot线刷
首先需要下载fastboot.exe,copy到system32文件夹下. 对于安卓系统的智能手机,同时按住开机键和音量减键,或手机连上电脑,输入adb reboot bootloader进入fast ...
- HDFS NameNode 设计实现解析
接前文 分布式存储-HDFS 架构解析,我们总体分析了 HDFS 架构的主要构成组件包括:NameNode.DataNode 和 Client.本文首先进一步解析 HDFS NameNode 的设计和 ...