POJ_2001_Shortest_Prefixes_(Trie)
描述
http://poj.org/problem?id=2001
给出一组单词,求每个单词的最小唯一前缀.
最小唯一前缀:该前缀不能是其他单词的前缀,并且最小,如果不存在,则为该单词本身.
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16921 | Accepted: 7338 |
Description
In the sample input below, "carbohydrate" can be abbreviated to
"carboh", but it cannot be abbreviated to "carbo" (or anything shorter)
because there are other words in the list that begin with "carbo".
An exact match will override a prefix match. For example, the prefix
"car" matches the given word "car" exactly. Therefore, it is understood
without ambiguity that "car" is an abbreviation for "car" , not for
"carriage" or any of the other words in the list that begins with "car".
Input
input contains at least two, but no more than 1000 lines. Each line
contains one word consisting of 1 to 20 lower case letters.
Output
output contains the same number of lines as the input. Each line of the
output contains the word from the corresponding line of the input,
followed by one blank space, and the shortest prefix that uniquely
(without ambiguity) identifies this word.
Sample Input
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
Sample Output
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona
Source
分析
唯一前缀就要求前缀的末位置只出现过一次,所以在Trie中insert的时候顺便记下每个点经过的次数.查找时一直找到第一个只出现了一次的点,或者查完整个单词.
p.s.打打Trie模板,已经忘干净了.
#include <cstdio>
#include <cstring>
using namespace std; const int type=; struct Trie{
struct node{
node* next[type];
int num;
node(){
num=;
for(int i=;i<type;i++) next[i]=NULL;
}
}*root;
Trie(){ root=new node; }
inline int id(char c) { return c-'a'; }
void insert(char *c){
node* o=root;
while(*c){
int t=id(*c);
if(o->next[t]==NULL) o->next[t]=new node;
o=o->next[t];
o->num++;
c++;
}
}
void search(char *c){
node* o=root;
while(*c){
int t=id(*c);
o=o->next[t];
printf("%c",*c);
if(o->num==) return;
c++;
}
}
}tree;
char c[][]; int main(){
int cnt=;
while(scanf("%s",c[++cnt])!=EOF) tree.insert(c[cnt]);
for(int i=;i<=cnt;i++){
printf("%s ",c[i]);
tree.search(c[i]);
printf("\n");
}
return ;
}
POJ_2001_Shortest_Prefixes_(Trie)的更多相关文章
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5646 Accepted: 1226 Description In an ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
随机推荐
- ThinkPad E40无线网卡驱动安装 FOR CENTOS6.3
1.看一下咱们用的本本的无线是咋子无线网卡,如下: [root@liaohg Downloads]# lspci | grep Wireless 03:00.0 Network controller: ...
- JDK+Tomcat+MyEclipse发布JSP项目——不能成功的问题
JDK.Tomcat和MyEclipse安装完成后,在将web Project发布到Tomcat时出现金叹号,如图 原因:由于将Tomcat安装在C盘,所以需要启用管理员权限. 解决途径:用管理员身份 ...
- HTML解析引擎:Jumony 开源项目
Jumony Core首先提供了一个近乎完美的HTML解析引擎,其解析结果无限逼近浏览器的解析结果.不论是无结束标签的元素,可选结束标签的元素,或是标记属性,或是CSS选择器和样式,一切合法的,不合法 ...
- Webservice学习之——即时发布与定制发布
一.工具 myEclipse tomcat 6.0 以上版本 axis-bin-1_4.zip 二.即时发布 1.解压 axis-bin-1_4.zip 2.axis-bin-1_4.zip\axi ...
- PAT_1046 划拳
啦啦啦.今天晚上火车回学校了.= =还是挺舍不得家里的. 题目描述: 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字 ...
- C++动态分配内存
动态分配(Dynamic Memory)内存是指在程序运行时(runtime)根据用户输入的需要来分配相应的内存空间. 1.内存分配操作符new 和 new[] Example: (1)给单个元素动态 ...
- (LightOJ 1004) Monkey Banana Problem 简单dp
You are in the world of mathematics to solve the great "Monkey Banana Problem". It states ...
- Spark-Cassandra-Connector 插入数据函数saveToCassandra
在spark-shell中将数据保存到cassandra: var data = normalfill.map(line => line.split("\u0005")) d ...
- ubuntu 安装phpmyadmin
参考文章: http://www.111cn.net/database/mysql/43957.htm access错误的解决方法 1 首先下载 去官网下载phpmyadmin 2 然后直接 ...
- localstorage 使用
localstorage作为HTML5的一个特殊属性,在发布时就备受关注:最近总结了其一些小的用法,希望可以抛砖引玉. 因HTML5本地存储只能存字符串,所以所有数据存储的话,都要转化成字符串:而js ...