博客
关于我
Shortest Prefixes(trie树唯一标识)
阅读量:621 次
发布时间:2019-03-13

本文共 2323 字,大约阅读时间需要 7 分钟。

Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 15948   Accepted: 6887

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

carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonate

Sample Output

carbohydrate carbohcart cartcarburetor carbucaramel caracaribou caricarbonic carbonicartilage carticarbon carboncarriage carrcarton cartocar carcarbonate carbona 题解:让找唯一能确定一个单词的前缀;我们想到当唯一确定的时候必定word[k]是1;那么很好解决了 代码:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #define mem(x,y) memset(x,y,sizeof(x)) 8 using namespace std; 9 typedef long long LL;10 const int MAXN=10100;11 char s[1010][30];12 int ch[MAXN][30],word[MAXN];13 int sz;14 void initial(){15 sz=1;16 mem(word,0);17 }18 void insert(char *s){19 int len=strlen(s);20 int j,k=0;21 for(int i=0;i

 

转载地址:http://nueaz.baihongyu.com/

你可能感兴趣的文章
mysql 默认事务隔离级别下锁分析
查看>>
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>
mysql-5.6.17-win32免安装版配置
查看>>
mysql-5.7.18安装
查看>>
MySQL-8.0.16 的安装与配置
查看>>
MySQL-Buffer的应用
查看>>
mysql-cluster 安装篇(1)---简介
查看>>
mysql-connector-java.jar乱码,最新版mysql-connector-java-8.0.15.jar,如何愉快的进行JDBC操作...
查看>>
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>
MySQL-Explain的详解
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>