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. Spring注解的步骤

    Spring框架提供DI(属性注解)和IOC(类/Bean的注解)注解. 注解:标注.注入和解析.解释;标注和解释一部分代码的作用在框架中:就是配置文件的另外一种实现方式@Type.@Taget;减少 ...

  2. jsp学习笔记 - 内置对象 pageContext

    1.pageContext几乎可以操作所有的页面内置对象 pageContext.getRequest();    得到的对象只是属于ServletRequest类,httpServletReques ...

  3. Spring框架系列(七)--Spring常用注解

    Spring部分: 1.声明bean的注解: @Component:组件,没有明确的角色 @Service:在业务逻辑层使用(service层) @Repository:在数据访问层使用(dao层) ...

  4. document.write() 和 document.writeln区别

    document.write() 和 document.writeln 都是JavaScript向客户端写入的方法,writeln是以行方式输出的,但并不是指页面实际效果中的换行,两种方法在查看源代码 ...

  5. P1223 排队接水

    题目描述 有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小. 输入输出格式 输入格式: 输入文件共两行,第一行为n:第二行分别 ...

  6. python_ 学习笔记(hello world)

    python中的循环语句 循环语句均可以尾随一个else语句块,该块再条件为false后执行一次 如果使用break跳出则不执行. for it in [1,2,3,4]: print(it,end= ...

  7. hihocoder 1032 最长回文子串(Manacher)

    传送门 #include<queue> #include<cmath> #include<cstdio> #include<cstring> #incl ...

  8. Git--删除远程仓库文件但不删除本地仓库资源

    我们在使用idea开发的过程中经常会出现新建项目的时候直接把xxx.iml文件也添加到了git trace 当然这并不会出现什么问题,问题是当我们把xxx.iml文件push到我们github上之后, ...

  9. Ajax学习总结(2)——Ajax参数详解及使用场景介绍

    一.定义和用法 AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJA ...

  10. Uva10562

    Professor Homer has been reported missing. We suspect that his recent research works might have had ...