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

Trie树真是神奇呢

/**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int chnum=26;
struct NODE{
int ct;//该节点被使用的次数
short a[26];//子节点
}n[15000];
int cnt;//总节点数
char c[2000][25];
void ins(char s[]){
int num=0,len=strlen(s);
int i,j;
for(i=0;i<len;i++)
{
if(n[num].a[s[i]-'a']==0){
cnt++;//增加节点
n[num].a[s[i]-'a']=cnt;
num=cnt;
}
else num=n[num].a[s[i]-'a'];//用已有结点 n[num].ct++;//该节点使用次数+1
}
return;
}
void Print(char s[]){
int num=0,len=strlen(s);
printf("%s ",s);
for(int i=0;i<len;i++){
num=n[num].a[s[i]-'a'];
printf("%c",s[i]);
if(n[num].ct==1) {
//printf("\n");//由于有些词是被完全包含在其他词里的,故不能在这里换行
break;}
}
printf("\n");
return;
}
int main(){
int i=0,j;
while(cin>>c[i] && c[i]!='\0')ins(c[i++]);
for(j=0;j<i;j++)Print(c[j]);
return 0;
}

POJ2001 Shortest Prefixes的更多相关文章

  1. poj2001 Shortest Prefixes(字典树)

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

  2. poj2001 Shortest Prefixes (trie树)

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

  3. poj2001 Shortest Prefixes (trie)

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

  4. POJ2001 Shortest Prefixes (Trie树)

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

  5. 【POJ2001】Shortest Prefixes

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18724   Accepted: 810 ...

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

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

  7. POJ 2001:Shortest Prefixes

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16782   Accepted: 728 ...

  8. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  9. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

随机推荐

  1. C10K 问题引发的技术变革

    C10K 问题引发的技术变革 http://rango.swoole.com/archives/381

  2. httpserver

    改了下 # -*- coding:utf-8 -*- from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler HOST = &quo ...

  3. HoloLens开发手记 - 应用程序模型 App model

    HoloLens使用Universal Windows Platform (UWP)提供的应用模型.UWP应用模型定义了应用如何被安全和完全地安装.更新.版本控制和移除.它管理了应用生命周期 - 应用 ...

  4. 「拉勾网」薪资调查的小爬虫,并将抓取结果保存到excel中

    学习Python也有一段时间了,各种理论知识大体上也算略知一二了,今天就进入实战演练:通过Python来编写一个拉勾网薪资调查的小爬虫. 第一步:分析网站的请求过程 我们在查看拉勾网上的招聘信息的时候 ...

  5. C++学习准则

    C++学习准则  1.把C++当成一门新的语言学习(和C没啥关系!真的): 2.看<Thinking In C++>,不要看<C++变成死相>(C++编程思想,翻译的非常差): ...

  6. IT人员如何保护视力

    最近感觉眼比较难受,有点干,估计是因为用上老婆淘汰的iPhone5C后屏幕太小,而我又是一个手机瘾重点患者的原因.为了保持自己5.0+的视力,做了以下工作,分享给各位朋友: Win7电脑将字体放大到1 ...

  7. 关于Hibernate的sequence diagram

  8. PLSQL Developer不支持Oracle 64位客户端解决方法

    问题描述: 在虚拟机同网段,搭建Oracle 11.2.04数据库64位的,本机操作系统Win10 x64和PLSQL 9.03,目前想利用PLSQL远程登录ORACLE数据库操作.当初用 insta ...

  9. 【JavaEE企业应用实战学习记录】struts2登录

    <%-- login.jsp Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 16:26 To chang ...

  10. Sublime Text 3 常用插件以及安装方法(vue 插件)

    使用Package Control组件安装 也可以安装package control组件,然后直接在线安装: 按Ctrl+` 调出console 粘贴以下代码到底部命令行并回车: { import u ...