题目链接: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. python中%代表什么意思?

    http://zhidao.baidu.com/link?url=MQLeRPckNfavTJYvMQbVj_pdNn5SSadtFvfEk7nNCusPcPW4T1O45esIuttuBW3EnSB ...

  2. JDK1.8 - > 1.7

    原文地址  https://blog.csdn.net/hwjean/article/details/52537722 JDK 1.8 -> 1.7 1. 配置好环境变量(我的是64bit系统) ...

  3. 继承Process类,run函数的简单使用

    #定义一个类 继承Process类 from multiprocessing import Process import os import time class Download(Process): ...

  4. js的浏览器判断方法

    使用navigator.userAgent来判断浏览器类型. 1.浏览器版本号函数: var br=navigator.userAgent.toLowerCase();   var browserVe ...

  5. python随机生成个人信息

    python随机生成个人信息 #!/usr/bin/env python3 # -*- coding:utf-8 -*- import sys import random class Personal ...

  6. Ubuntu下配置了ssh,但是连接很慢

    ssh登录服务器时总是要停顿等待一下才能连接上,这是因为OpenSSH服务器有一个DNS查找选项UseDNS默认是打开的. UseDNS选项打开状态下,当客户端试图登录OpenSSH服务器时,服务器端 ...

  7. 容器下载的是centos8的镜像,scp出现packet_write_wait: Connection to **** port 22: Broken pipe 问题解决

    解决方案:在~/.ssh目录新建文件config vi ~/.ssh/config         #Added lines to fix.    Host *    IPQoS lowdelay t ...

  8. Spring Bean 的生命周期,如何被管理的

    1. 实例化一个Bean,也就是我们通常说的new 2. 按照Spring上下文对实例化的Bean进行配置,也就是IOC注入 3. 如果这个Bean实现了BeanNameAware接口,会调用它实现的 ...

  9. Netflix:我们为什么要将GraphQL引入前端架构?

    作者|Artem Shtatnov译者|无明 在这篇文章中,我们将分享 Netflix 在这些应用程序的前端架构中引入 GraphQL 所积累的经验. 在内部,我们把用于管理广告创建和组装的主要应用程 ...

  10. .net 开源项目地址

    https://github.com/dotnet/corefx 这个是.net core的 开源项目地址 https://github.com/aspnet 这个下面是asp.net core 框架 ...