Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 17608   Accepted: 7658

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

Source

 题意:

找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串

 题解:

在裸字典树的基础上,trie[now].w表示now这个节点被用过几次,当我们扫一个单词时,
如果它的某一个字母只用过一次,即只有它用过,那么这以前就是它的前缀。

AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int N=;
const int M=;
struct node{
int next[M];
int w;
}tire[N*M];
char s[N][M];
int cnt,n;
void build(int num){
int now=,len=strlen(s[num]);
for(int i=;i<len;i++){
int x=s[num][i]-'a'+;
if(!tire[now].next[x])
tire[now].next[x]=++cnt;
now=tire[now].next[x];
tire[now].w++;
}
}
void query(int num){
int now=,len=strlen(s[num]);
for(int i=;i<len;i++){
int x=s[num][i]-'a'+;
now=tire[now].next[x];
printf("%c",s[num][i]);
if(tire[now].w==) break;
}
}
int main(){
while(scanf("%s",s[++n])==) build(n);n--;
for(int i=;i<=n;i++) printf("%s ",s[i]),query(i),putchar('\n');
return ;
}

poj2011的更多相关文章

随机推荐

  1. Python 开发初识

    从今天开始记录自己的python开发之路,用博客记录自己的学习经历,以及学习小结,小的项目模块,努力充实,做最好的自己!!!

  2. POJ2152 Fire (树形DP)

    题意:n个城市n-1条边 组成一棵树 在每个城市修建消防站会有一个花费costi 每个城市能防火当且仅当地图上距离他最近的消防站距离小于di   问如何修建消防站 使地图上所有的城市都有预防火灾的能力 ...

  3. JQuery 的toggle() 方法如何使用?

    JQuery中的toggle()方法,相当于点一个元素时,重复循环两个函数,而这两个函数可以作为toggle()函数的两个参数传进去,当第一次点击的时候会执行前面的参数,而第二次点击时执行的是后面的参 ...

  4. Linux:Apache改静态网页、个人用户主页、虚拟网站主机、Apache访问控制

    Apache改静态网页  1.概述: Apache是web服务器(静态解析,如HTML),tomcat是java应用服务器(动态解析,如JSP.PHP) Tomcat只是一个servlet(jsp也翻 ...

  5. iframe使用大全

    <iframe src=”you page’s url” width=”100″ height=”30″ frameborder=”no” border=”0″ marginwidth=”0″ ...

  6. Mac安装virtualwrapper时报错No module named virtualenvwrapper

    1. 前言 我在使用mac安装virtualwrapper的时候遇到了问题,搞了好长时间,才弄好,在这里总结一下分享出来,供遇到相同的问题的朋友使用,少走些弯路. 2. 问题说明 Mac默认系统的py ...

  7. Spring 使用注解注入 学习(四)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  8. 《编译原理》构造 LL(1) 分析表的步骤 - 例题解析

    <编译原理>构造 LL(1) 分析表的步骤 - 例题解析 易错点及扩展: 1.求每个产生式的 SELECT 集 2.注意区分是对谁 FIRST 集 FOLLOW 集 3.开始符号的 FOL ...

  9. 【Codeforces 9989C】A Mist of Florescence

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 四个大角 然后每个大角里面包着一些其他颜色的就好 [代码] #include <bits/stdc++.h> using name ...

  10. 【Codeforces 979B】Treasure Hunt

    [链接] 我是链接,点我呀:) [题意] 每次你可以将一个字符变成一个不同于本身的字符. 每个人需要改变n次(且不能不改变) 设每个人的字符串中出现次数最多的字符出现的次数为cnt[0~2] 问你谁的 ...