题目链接:POJ 2001

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

Rocky Mountain 2004

Solution

题意

给出若干个由小写字母组成的单词,对每个单词找出最短的前缀,使得该前缀不是其他任何字符串的前缀。

如果整个单词都是其他单词的前缀就输出整个单词。

题解

Trie

标记每个结点结束的前缀是多少个单词的前缀,查找时只要找到某个唯一的前缀就输出。

Code

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 1e4 + 10; int nxt[maxn][30];
int val[maxn];
int tot = 1; int index(char c) {
return c - 'a';
} void insert(string s) {
int len = s.size();
int p = 0;
for (int i = 0; i < len; ++i) {
int c = index(s[i]);
if (!nxt[p][c]) {
nxt[p][c] = tot++;
}
p = nxt[p][c];
++val[p];
}
}
void query(string s) {
int len = s.size();
int p = 0;
for (int i = 0; i < len; ++i) {
int c = index(s[i]);
p = nxt[p][c];
cout << s[i];
if(val[p] == 1) return;
}
} string s[maxn];
int n = 1; int main() {
ios::sync_with_stdio(false);
cin.tie(0);
while(cin >> s[n]) {
insert(s[n]);
n++;
}
for(int i = 1; i < n; ++i) {
cout << s[i] << " ";
query(s[i]);
cout << endl;
}
return 0;
}

POJ 2001 Shortest Prefixes (Trie)的更多相关文章

  1. poj 2001 Shortest Prefixes trie入门

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

  2. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

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

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

  4. OpenJudge/Poj 2001 Shortest Prefixes

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

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

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

  6. POJ 2001 Shortest Prefixes(字典树)

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

  7. POJ 2001 Shortest Prefixes(字典树活用)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21651   Accepted: 927 ...

  8. POJ 2001 Shortest Prefixes 【Trie树】

    <题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...

  9. poj 2001 Shortest Prefixes(特里)

    主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...

随机推荐

  1. 通过export方式导出,在导入时要加{ },export default则不需要

    怎么就是记不住呢?? 通过export方式导出,在导入时要加{ },export default则不需要

  2. python 类和对象下

    类的常用函数 issubclass() 检测一个类是否是另外一个类的子类 格式1:issubclass(被检测类,父类) 返回值:布尔值 格式1:issubclass(被检测类,(父类1,父类2,父类 ...

  3. 详解linux io flush

    通过本文你会清楚知道 fsync().fdatasync().sync().O_DIRECT.O_SYNC.REQ_PREFLUSH.REQ_FUA的区别和作用. fsync() fdatasync( ...

  4. 绿盟扫出来个http host 漏洞

    这个漏洞搞了大半天,想过从后台拦截,也想过从前台拦截,都是无从下手!网上也找了很多资料,有点乱,后来自己结合网上的办法,搞出如下解决办法:在tomcat server.xml里配置host 因为外网是 ...

  5. 调试Xamarin.Android时出现缺少"Mono.Posix 2.0.0"的错误

    1.在http://originaldll.com/file/mono.posix.dll/31191.html中下载mono.posix 2.0.0 dll 2.以管理员权限运行Visual Stu ...

  6. nginx实现高性能负载均衡的Tomcat集群

    1. 安装软件: nginx 两个apache-tomcat 安装过程省略. 2.配置两个tomcat的http端口,第一个为18080,第二个为28080 注意:需要把server.xml文件中所有 ...

  7. MySQL日志文件与分析

    1.查询日志.慢查询日志.二进制日志对比 查询日志 general_log 会记录用户的所有操作,其中包含增删查改等 可以指定输出为表 慢查询日志 slow_log 只要超过定义时间的所有操作语句都记 ...

  8. Node.js的适用场景?

    1).实时应用:如在线聊天,实时通知推送等等(如socket.io) 2).分布式应用:通过高效的并行I/O使用已有的数据 3).工具类应用:海量的工具,小到前端压缩部署(如grunt),大到桌面图形 ...

  9. 如何加快Vue项目的开发速度

    如何加快Vue项目的开发速度 本文摘自奇舞周刊,侵权删. 现如今的开发,比如内部使用的管理平台这种项目大都时间比较仓促.实际上来说,在使用了webpack + vue 这一套来开发的话已经大大了提高了 ...

  10. jQuery判断checkbox是否选中的4种方法

    方法一: ).checked) { // do something } 方法二: if($('#checkbox-id').is(':checked')) { // do something } 方法 ...