Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 21642   Accepted: 9269

Description

A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "carbo", and "carbon". Note that the empty string is not considered a prefix in this problem, but every non-empty string is considered to be a prefix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, "carbohydrate" is commonly abbreviated by "carb". In this problem, given a set of words, you will find for each word the shortest prefix that uniquely identifies the word it represents.

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

The 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

The 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 题意:先输出原来输入的字符串,在输出该字符串在字典树上的共有部分再多一个字符
注意:最后结尾要多输出一个回车
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int tree[][],vis[];
string s[];
int id,root,len,num=,t=;
void insert()
{
root=;
len=s[t].length();
for(int i=;i<len;i++)
{
id=s[t][i]-'a';
if(!tree[root][id])
tree[root][id]=++num;
vis[tree[root][id]]++;
root=tree[root][id];
}
} int search(string ss)
{
root=;
len=ss.length();
for(int i=;i<len;i++)
{
id=ss[i]-'a';
cout<<ss[i];
if(vis[tree[root][id]]==)
break;
root=tree[root][id];
}
return ;
}
int main()
{
while(cin>>s[t])
{
insert();
t++;
}
for(int i=;i<t;i++)
{
cout<<s[i]<<' ';
int temp;
temp=search(s[i]);
cout<<endl;
}
cout<<endl;//多输出一个回车
return ;
}

poj2001 Shortest Prefixes(字典树)的更多相关文章

  1. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  2. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  3. poj2001 Shortest Prefixes (trie树)

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  4. poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...

  5. POJ2001Shortest Prefixes(字典树)

    题目大意就是帮你给N条字符串,每条长度不超过20.问要将他们单一识别出来,每个字符串最短可以缩为多短. 如: abc abcdefg bc adef 这四个字符串就可分别缩写为 abc abcd b ...

  6. POJ2001 Shortest Prefixes (Trie树)

    直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...

  7. POJ2001 Shortest Prefixes

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  8. poj2001 Shortest Prefixes (trie)

    读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数. 然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了. 不知为何下面的程序一直有bug……不知是读入的问题? type nod ...

  9. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

随机推荐

  1. NULL、0、nullptr

    C的NULL 在C语言中,我们使用NULL表示空指针,也就是我们可以写如下代码: int *i = NULL;foo_t *f = NULL; 实际上在C语言中,NULL通常被定义为如下: #defi ...

  2. sg值的求解(NIM)

    硬币游戏2 挑战程序设计竞赛P315 1堆的情况: #include<bits/stdc++.h> ,grundy[],k=,A[]={,},n=; using namespace std ...

  3. Educational Codeforces Round 56 (Rated for Div. 2) E(1093E) Intersection of Permutations (树套树,pb_ds)

    题意和分析在之前的链接中有:https://www.cnblogs.com/pkgunboat/p/10160741.html 之前补题用三维偏序的cdq的分治A了这道题,但是感觉就算比赛再次遇到类似 ...

  4. Ros疑问汇总

    一.机器人描述文件三个: 机器人主体body文件: gazebo属性文件: 主文件 smartcar.urdf: 二.启动文件smartcar_display.rviz.launch:启动节点和模拟器 ...

  5. global作用域

    1   global在函数内部 $somevar=15; function addit () { GLOBAL $somevar; $somevar++ ; echo "somevar is ...

  6. DR客户端一直连接服务器....(6)

    DR客户端一直连接服务器....(非未选择网卡)解决方法: 控制面板-->网络和 Internet-->网络连接,打开本地连接(以太网)属性,将IPV4协议前面的对勾去掉,重启DR 这样D ...

  7. JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)

    一.结构 二.代码 1. package org.jpwh.model.collections.listofstrings; import org.jpwh.model.Constants; impo ...

  8. hdu6357 Hills And Valleys

    传送门 题目大意 给定一个序列A,求翻转A中一个区间之后的最长不降子序列的长度即翻转的区间 分析 发现直接枚举翻转的区间的话是无论如何都不行的,于是有一个非常神奇的做法.我们再设一个序列B = {0, ...

  9. WOJ 10 精英选拔

    神仙dp,膜Claris 题意:给一个长度为$n$的数列,求出不超过k次交换后的最大连续子区间和. 发现交换后的最优答案一定是这样的(0和2的长度可以为0)             0        ...

  10. Python程序设计4——控制语句

    1 print和import的更多信息 1.1 使用逗号输出 前面已经讲解过如何使用print来打印表达式,可以使用都好来打印多个表达式,只要用逗号隔开即可. >>> print ' ...