Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 17683   Accepted: 7686

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


题意:给每个单词分一个最短前缀

裸的前缀树/字典树
val纪录这个节点经过几次,经过就++
查询时遇到val==1直接退出
愚蠢的给insert传了一个s[i]+1呵呵呵
//
// main.cpp
// poj2001
//
// Created by Candy on 10/13/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=1e4+;
int t[N*][],val[N*],sz;
int n=;
char s[N][];
inline int id(char c){return c-'a';}
void insert(char a[]){
int len=strlen(a+),u=;
for(int i=;i<=len;i++){
int c=id(a[i]);
if(!t[u][c]) t[u][c]=++sz;
u=t[u][c];
val[u]++;
}
}
void query(char a[]){
int len=strlen(a+),u=;
for(int i=;i<=len;i++){
int c=id(a[i]);
if(val[u]==) break;
putchar(a[i]);
u=t[u][c];
}
putchar('\n');
}
int main(int argc, const char * argv[]) {
while(scanf("%s",s[++n]+)!=EOF) insert(s[n]);
for(int i=;i<=n;i++){
printf("%s ",s[i]+);
query(s[i]);
}
return ;
}
 

POJ2001Shortest Prefixes[Trie]的更多相关文章

  1. poj2001Shortest Prefixes(trie)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18687   Accepted: 808 ...

  2. POJ2001Shortest Prefixes(Trie树)

    传送门 题目大意:求最短唯一前缀 题解:Trie树 把单词一个个插入,每个字母节点v[]++;然后输出时输出到v[]为1的点, v[]=1说明只有这个单词经过. 代码 : #include<io ...

  3. poj 2001 Shortest Prefixes trie入门

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

  4. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  5. poj2001 Shortest Prefixes (trie树)

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

  6. POJ2001Shortest Prefixes(字典树)

    题目大意就是帮你给N条字符串,每条长度不超过20.问要将他们单一识别出来,每个字符串最短可以缩为多短. 如: abc abcdefg bc adef 这四个字符串就可分别缩写为 abc abcd b ...

  7. POJ 2001-Shortest Prefixes(Trie 入门)

    题意:给你一组串,求每个串在组中唯一标志的最短前缀 分析:保存树上经过各节点的单词个数,扫描每个串当经过节点单词个数是一(能唯一标志)结束 #include <map> #include ...

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

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

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

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

随机推荐

  1. http://www.mysqltutorial.org/python-mysql-query/

    This tutorial shows you how to query data from a MySQL database in Python by using MySQL Connector/P ...

  2. MySQL使用if判断

    select *,if(sva=1,"男","女") as ssva from taname where sva<>"" 12. ...

  3. struts通过action返回json

    其实struts2通过action返回json挺简单的,但是就是老要忘,所以索性写在博客上.好的,开始. 首先是引入必须的jar包: struts2-json-plugin-2.3.24.jar 然后 ...

  4. MySQL备份命令mysqldump参数说明与示例

    1. 语法选项说明 -h, --host=name主机名 -P[ port_num], --port=port_num用于连接MySQL服务器的的TCP/IP端口号 --master-data这个选项 ...

  5. 十一个行为模式之命令模式(Command Pattern)

    定义: 将一个请求封装成对象,使得请求发送者和请求接受者之间相互隔离,消除两者之间的耦合.引入命令类,使得不同请求对客户参数化,并且可以对命令添加附件操作,如:排队.撤销.日志.组合等. 结构图: C ...

  6. 《Effective C#》读书笔记

    Effiective C# Item1 : 使用属性代替成员变量 Effective C# Item2:运行时常量(readonly)优于编译时常量(const) Effective C# Item3 ...

  7. html5实现银联海购商品分类列表

    银联海购官网请点击 http://haigou.unionpay.com/ 1.实现效果预览展示如下: 2.源码如下 <!DOCTYPE html> <html> <he ...

  8. thinkcmf开发--关于控制器

    一.安装 安装---删除install文件--删除index.php--修改数据库信息--创建数据库 修改数据库信息:\data\conf\db.php(包括服务器地址) 二.创建mobile app ...

  9. HTML5移动端图片左右切换动画

    插件描述:HTML5移动端图片左右切换动画 小海今天要给大家分享一款很不错的图片左右切换焦点图动画,并且支持移动端触摸滑动.功能上,这款HTML5图片播放器支持鼠标滑动.手机端触摸滑动以及自动播放.外 ...

  10. iOS 多线程GCD简介

    一.简介 1.1 GCD (Grand Central Dispatch )是Apple开发的一个多核编程的解决方法. Grand 含义是“伟大的.宏大的”,Central含义“中央的”,Dispat ...