Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 18724   Accepted: 8101

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

字典树裸题,如果不会请参考其他算法书。

代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<cstring>
using namespace std;
int a[][];
int quan[];
char chuan[][];
int num1=,num2=;
void insert(char x[]){
int len=strlen(x);
int now=;
for(int i=;i<len;i++){
int to=x[i]-'a';
if(!a[now][to]){
memset(a[++num2],,sizeof(a[num2]));
a[now][to]=num2;
}
now=a[now][to];
quan[now]++;
}
}
void dfs(char x[]){
int len=strlen(x);
int now=;
for(int i=;i<len;i++){
int to=x[i]-'a';
if(!a[now][to]){printf("");return;}
now=a[now][to];
printf("%c",x[i]);
if(quan[now]==) return;
}
}
int main(){
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
memset(quan,,sizeof(quan));
memset(chuan[],,sizeof(chuan[]));
while(scanf("%s",&chuan[num1])!=EOF){
insert(chuan[num1]);
num1++;
}
for(int i=;i<num1;i++){
printf("%s ",chuan[i]);
dfs(chuan[i]);
printf("\n");
}
}

【POJ2001】Shortest Prefixes的更多相关文章

  1. 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)

    [CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...

  2. 【leetcode】Shortest Palindrome(hard)★

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. 【Leetcode】Shortest Palindrome

    Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...

  4. 【算法】shortest distance

    好不容易找到的. #include<iostream> #include<iomanip> #include<cmath> using namespace std; ...

  5. 线性基【CF845G】Shortest Path Problem?

    Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...

  6. 【题解】Shortest Cycle

    原题链接:CF1205B   题目大意   给定\(n\)个整数\(a_1,a_2,a_3, \dots ,a_n\),若\(i \neq j\)且\(a_i \land a_j \neq 0\),则 ...

  7. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  8. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  9. 【LeetCode】二分 binary_search(共58题)

    [4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...

随机推荐

  1. 反序列化JSON

    本人编程生涯刚刚起步,以下是个人理解,如果有些不对的地方,请各位在评论区指出,如果有更详细的博客也可以推荐给我. 首先要根据JSON创建一个实体类,并且要实现Serializable接口,再创建一个J ...

  2. SpringBoot系列__02HelloWorld探究

    在前文中,我们创建了一个简单的hello world,现在,利用这个简单的程序,来简单分析一下SpringBoot的启动过程. 如果你是使用过SSM框架的人,尤其是4.0之前的版本,相信你使用过xml ...

  3. Flutter开发环境配置(MAC版)

    一.配置镜像 打开命令终端,输入命令open ~/.bash_profile,打开bash_profile文本,添加镜像路径并保存 export PUB_HOSTED_URL=https://pub. ...

  4. HBase介绍、安装与应用案例

    搭建环境 部署节点操作系统为CentOS,防火墙和SElinux禁用,创建了一个shiyanlou用户并在系统根目录下创建/app目录,用于存放 Hadoop等组件运行包.因为该目录用于安装hadoo ...

  5. spring mvc Response header content type

    Xml代码 <bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAd ...

  6. 加入百度地图遇到 framework not found BaiduMapAPI***

    新建一个文件夹,把需要的静态库和.h文件放到文件夹里面.就解决啦.虽然不知道为什么这样可以,但是可以使用啦~~~

  7. MAC sublime常用快捷键(慢慢补)

    1、 FN + 左方向键:向左选择一行 2、FN + 右方向键:向右选择一行 3、FN + 上方向键:跳到页头 4、FN + 下方向键:跳到页尾 5、FN + SHIFT + 左方向键|上方向键:从当 ...

  8. zipkin+elk微服务日志收集分析系统

    docker安装elk日志分析系统 在win10上安装docker环境 tip:win7/8 win7.win8 系统 win7.win8 等需要利用 docker toolbox 来安装,国内可以使 ...

  9. [c++] 面试题之犄角旮旯 第壹章

    记录C/C++语言相关的问题. 算法可视化:https://visualgo.net/en <data structure and algorithm in c++> By Adam 有免 ...

  10. 自己制作一个简单的操作系统二[CherryOS]

    自己制作一个简单的操作系统二[CherryOS] 我的上一篇博客 自己制作一个简单的操作系统一[环境搭建], 详细介绍了制作所需的前期准备工作 一. 一点说明 这个操作系统只是第一步, 仅仅是开机显示 ...