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. MySQL-注释-Navicat基本使用-复杂查询练习题-解题思路-pymysql操作数据库-SQL注入-05

    目录 mysql语句注释 navicat 的基本使用 特色(个人总结) 与数据服务器建立连接 创建&打开数据库.表 创建 打开 修改操作表结构 修改表结构 查询修改操作表数据 基本语句对应的操 ...

  2. jinfo Java配置信息工具

    jinfo(Configuration info for Java) jinfo的作用是实时地查看和调整虚拟机各项参数. jinfo 命令格式: jinfo [ option ] pid pid是虚拟 ...

  3. Winform中自定义xml配置文件,并配置获取文件路径

    场景 在Winform程序中,需要将一些配置项存到配置文件中,这时就需要自定义xml的配置文件格式.并在一些工具类中去获取配置文件的路径并加载其内容. 关注公众号霸道的程序猿获取编程相关电子书.教程推 ...

  4. 来几道水题 d050: 妳那裡現在幾點了?

    减去15即可(注意这个数小于15的情况) 题目:珊珊到了美国犹他州的杨百翰大学之后,文文禁不住对她的思念,常常想打电话给她,却又担心在美国的她是不是在睡觉.好不容易鼓起勇气打通了电话,第一句就先问:「 ...

  5. Ganglia环境搭建并监控Hadoop分布式集群

    简介 Ganglia可以监控分布式集群中硬件资源的使用情况,例如CPU,内存,网络等资源.通过Ganglia可以监控Hadoop集群在运行过程中对集群资源的调度,作为简单地运维参考. 环境搭建流程 1 ...

  6. 我用数据结构花了一夜给女朋友写了个h5走迷宫小游戏

    目录 起因 分析 画线(棋盘) 画迷宫 方块移动 结语 @(文章目录) 先看效果图(在线电脑尝试地址http://biggsai.com/maze.html): 起因 又到深夜了,我按照以往在公众号写 ...

  7. Git同步更新操作GitHub和码云仓库上面的代码

    一.前言 问题: 小编在生活中,一般都是将代码保存到github上,但由于国内的码云仓库确实速度比github快很多,用起来也很方便,于是后来就慢慢转码云了,当然小编在github上的代码也不想放弃更 ...

  8. python里的while循环和if循环

    一.循环的概念 循环是程序设计语言中反复执行某些代码的一种计算机处理过程,常见的有按照次数循环和按条件循环. 二.循环的基本格式 (while循环的基本格式) a=1#定义一个数 while a==1 ...

  9. Kubernetes Dashboard 终结者:KubeSphere

    原文链接:Kubernetes Dashboard 终结者:KubeSphere 2018 年 7 月份,青云在 Cloud Insight 云计算峰会上推出了一款全新的容器平台--KubeSpher ...

  10. [Python] 09 - Multi-processing

    前言 资源 Ref: Python3 多线程 Ref: Python3之多进程       # python中的多线程无法利用多核优势 更多的提高效率的策略,请参见:[Pandas] 01 - A g ...