POJ 3630 Phone List(字典树)
题意
题意:t个case(1<=t<=40),给你n个电话号码(电话号码长度<10)(1 ≤ n ≤ 10000),如果有电话号码是另一个电话号码的前缀,则称这个通讯录是不相容的,判断通讯录是否相容。
题解
把电话的结尾做标记,check的过程中,如果是电话号码的结尾并且trie树后面还有枝子,输出NO。(对树DFS一下)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=;
int t,n,tot;
char s[N];
struct tree{
int nxt[],e,h;
}tr[N];
void insert(char s[]){
int len=strlen(s);
int now=;
for(int i=;i<len;i++){
if(!tr[now].nxt[s[i]-'']){
tr[now].h=;
tr[now].nxt[s[i]-'']=++tot;
}
now=tr[now].nxt[s[i]-''];
}
tr[now].e++;
}
bool dfs(int u){
if((tr[u].e&&tr[u].h)||(tr[u].e>))return true;
for(int i=;i<=;i++){
if(tr[u].nxt[i]){
if(dfs(tr[u].nxt[i]))return true;
}
}
if(u==)return false;
}
void clear(int u){
for(int i=;i<=;i++){
if(tr[u].nxt[i])clear(tr[u].nxt[i]);
}
memset(tr[u].nxt,,sizeof(tr[u].nxt));
tr[u].e=tr[u].h=;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
tot=;
for(int i=;i<=n;i++){
scanf("%s",s);
insert(s);
}
if(dfs())printf("NO\n");
else printf("YES\n");
clear();
}
return ;
}
POJ 3630 Phone List(字典树)的更多相关文章
- poj 3630 Phone List(字典树)
题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先 ...
- Phone List POJ 3630 Trie Tree 字典树
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29416 Accepted: 8774 Descr ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)
Phone List 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Given a list of phone numbers, determine if it i ...
- poj 1204 Word Puzzles(字典树)
题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
- POJ 2408 - Anagram Groups - [字典树]
题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...
- POJ 1816 - Wild Words - [字典树+DFS]
题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
随机推荐
- MYSQL5.6/5.7 数据库密码丢失问题处理(需重启)
文章结构图: 一.MYSQL5.6密码丢失 1. 强行停止MYSQL 丢失超级管理用户ROOT的密码是致命的,可以通过--skip-grant-tables参数来跳过权限表. 停止MYSQL,强行杀 ...
- 最长公共子序列(稀疏序列)nlogn解法
首先这种做法只能针对稀疏序列, 比如这种情况: abc abacabc. 会输出5 ,,,,就比较尴尬, #include<iostream> #include<cstdio> ...
- ASP.NET 让ajax请求webform后台方法
$.ajax({ type: "POST", url: ".aspx/getSubjectDirection", data: JSON.stringify({ ...
- Swift 闭包中 self? 的由来
class UIViewSpringAnimator: SwipeAnimator { // 动画完成的闭包 var completion:((Bool) ->Void)? func addCo ...
- 洛谷1345 [Usaco5.4]奶牛的电信
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- 3D ShapeNets: A Deep Representation for Volumetric Shapes 代码遇到的问题
遇到 Error using polygon2voxel_double Requested 515396075640x140711718551672x140719189273184 (17179869 ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- 微软100题第51题:和为n连续正数序列
题目:输入一个正数n,输出全部和为n连续正数序列.比如输入15,因为1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. 方法一:记录序列长度.推断首项是否满足条 ...
- YII显示sql进行调试
进行插入測试: 一个简单控制器: function actionJia(){ $goods_model = new Goods(); //进行加入有别于查询不能使用以下这样的方式 // $goods_ ...
- EJB3.0高速入门项目开发步骤
EJB3.0开发步骤 1. 开发环境 IDE开发工具:Eclipse Java EE IDE for Web Developers EJB容器:jboss-4.2.3.GA 后台数据库:MysQL ...