Shortest Prefixes(trie树唯一标识)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 15948 | Accepted: 6887 |
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
题解:让找唯一能确定一个单词的前缀;我们想到当唯一确定的时候必定word[k]是1;那么很好解决了
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int MAXN=;
char s[][];
int ch[MAXN][],word[MAXN];
int sz;
void initial(){
sz=;
mem(word,);
}
void insert(char *s){
int len=strlen(s);
int j,k=;
for(int i=;i<len;i++){
j=s[i]-'a';
if(!ch[k][j]){
mem(ch[sz],);
ch[k][j]=sz++;
}
k=ch[k][j];
word[k]++;
}
}
void find(char *s){
int len=strlen(s);
int j,k=;
for(int i=;i<len;i++){
j=s[i]-'a';
k=ch[k][j];
printf("%c",s[i]);
if(word[k]==)return;
}
}
int main(){
int x=;
initial();
while(~scanf("%s",s[x])){
insert(s[x]);x++;
}
for(int i=;i<x;i++){
printf("%s ",s[i]);
find(s[i]);puts("");
}
return ;
}
Shortest Prefixes(trie树唯一标识)的更多相关文章
- poj2001 Shortest Prefixes (trie树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- poj2001 Shortest Prefixes(字典树)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21642 Accepted: 926 ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
随机推荐
- input 输入验证
js验证输入框内容 只能输入英文 只能输入英文 无法粘贴,右键不会弹出粘贴菜单 只能输入数字: 只能输入数字,小数点: 只能输入数字,小数点,下划线: 只能输入英文和数字: 只能输入汉字: 禁止输入法 ...
- JavaSE复习日记 : 接口
/* * 接口 * 引用数据类型: * 类,接口,数组; * * 接口是一种引用数据类型,可以看作是一个特殊的类,它存在的目的是为了解决没有多重继承引起的功能弱的问题而设计的,一个类只能有一个父类,但 ...
- vs2012C#编程环境设置智能提示
vs2012 智能提示和 显示行号的问题 路径为 菜单里 工具-->选项-->文本编辑器-->C# 如图所示 自动列出成员就是 vs里面的智能提示 行号就会显示所写代码的行 ...
- A - 数塔
A - 数塔 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- Java 动态代理(转)
一.代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后 处理消息等.代理类与委托类之间通常会存在 ...
- linux杂记(十)what is BASH Shell
first,what is shell?其实只要是碰过计算机的,对于OS(Operation System操作系统,不管是linux.unix.windows)有点概念的人大多都听过这个名词,因为只要 ...
- websocket 通信协议
//WEBSOKET java SERVICE http://my.oschina.net/u/590484/blog/71797 UPDATE:前些天有网友mail和我讨论websocket协议,当 ...
- page cache和buffer cache
因为要优化I/O性能,所以要理解一下这两个概念,这两个cache着实让我迷糊了好久,通过查资料大概明白了两者的区别,试着说下. page cache:文件系统层级的缓存,从磁盘里读取的内容是存储到这里 ...
- 11-3URLTestDemo实例操作完成URL单元测试
11-3URLTestDemo 1.File -> New -> Project 在左边模板中选择Visual C#里的Web,对应到的项目类型选择ASP.NET MVC3 Web App ...
- 激活工具 – Microsoft Toolkit 2.4.7
Microsoft Toolkit是一款很出名的Windows/Office激活工具,最早是因为激活Office 2010出名的,想必不少人也用过吧?Microsoft Toolkit从2.4.1版本 ...