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

题意:给你多个字符串,对每一个字符串你要用最简单但是确定代表这个字符串的前缀表示它。

思路:先把所有的字符串都插入到trie上,这个字符经过的节点val值都加1,那么一个字符的唯一前缀就是从它的头字符一直往后走,当节点val为1时就代表只有这个字符串,那么前缀就是之前的这些数,这里要注意前缀是祺本身的情况要特判。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const long double eps=1e-13;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 5000
#define maxnode 50000
char s[maxn][30],str[maxn][30];
int val[maxnode];
int ch[maxnode][30];
int sz;
void init()
{
sz=0;
memset(ch[0],0,sizeof(ch[0]));
memset(val,0,sizeof(val)); }
int idx(char s){
return s-'a';
} void charu(char *s)
{
int u=0,i,j;
int len=strlen(s);
for(i=0;i<len;i++){
int c=idx(s[i]);
if(!ch[u][c]){
sz++;
ch[u][c]=sz;
val[sz]++;
u=sz;
}
else{
u=ch[u][c];
val[u]++;
}
}
}
int chazhao(char *s)
{
int i,j,u=0;
int t,len=strlen(s);
t=len-1;
for(i=0;i<len;i++){
int c=idx(s[i]);
if(val[ch[u][c] ]==1){
t=i;break;
}
u=ch[u][c];
}
return t;
}
int main()
{
int n,m,i,j,tot;
tot=0;
init();
while(scanf("%s",s[++tot])!=EOF){
charu(s[tot]);
}
for(i=1;i<=tot;i++){
int k=chazhao(s[i]);
for(j=0;j<=k;j++){
str[i][j]=s[i][j];
}
str[i][j]='\0'; }
for(i=1;i<=tot;i++){
printf("%s %s\n",s[i],str[i]);
}
}

poj2001 Shortest Prefixes (trie树)的更多相关文章

  1. poj2001 Shortest Prefixes(字典树)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21642   Accepted: 926 ...

  2. POJ2001 Shortest Prefixes (Trie树)

    直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...

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

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

  4. POJ2001 Shortest Prefixes

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

  5. poj 2001 Shortest Prefixes trie入门

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

  6. poj2001 Shortest Prefixes (trie)

    读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数. 然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了. 不知为何下面的程序一直有bug……不知是读入的问题? type nod ...

  7. POJ 2001 Shortest Prefixes (Trie)

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

  8. POJ 2001 Shortest Prefixes(字典树)

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

  9. poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...

随机推荐

  1. Sentry(v20.12.1) K8S 云原生架构探索,SENTRY FOR JAVASCRIPT Source Maps 详解

    系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...

  2. Tomcat-8.5.23 基于域名和端口的虚拟主机

    下载tomcat yum install java -y cd /opt/ wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.23/b ...

  3. maven生命周期与插件

    目录 Maven生命周期 clean default site 命令与对应周期 插件与绑定 插件目标 插件绑定 内置绑定 自定义绑定 插件配置 本文主要是针对<maven实战>书中关键知识 ...

  4. Java反射全解析(使用、原理、问题、在Android中的应用)

    前言 今天说Java模块内容:反射. 反射介绍 正常情况下,我们知晓我们要操作的类和对象是什么,可以直接操作这些对象中的变量和方法,比如一个User类: User user=new User(); u ...

  5. 分布式系统:xxl-job改造spring-cloud

    目录 改造原因 主要改造思路 调度中心 调度中心 执行器侧 总结 修改后的源码仓库地址:GitHub. : 改造原因 原有的xxl-job使用自己实现的http协议进行注册以及调度等,与目前框架中本身 ...

  6. 04--Docker数据卷和数据卷容器

    .为什么要使用数据卷: Docker容器产生的数据,如果不通过docker commit生成新的镜像,使得数据做为镜像的一部分保存下来,那么当容器删除后,数据自然也就没有了.为了能保存数据在docke ...

  7. [Ceoi2004]Journey

    题目描述 给出N个点,及你的出发点K. 接下来N-1行描述有关边的开始点,结束点,边长.保证图中不会有环 接下来给出数字J,代表你要走多少个点. 接下来J个数字,代表你要走过的点的编号.当然你可以自己 ...

  8. [从源码学设计]蚂蚁金服SOFARegistry之延迟操作

    [从源码学设计]蚂蚁金服SOFARegistry之延迟操作 0x00 摘要 SOFARegistry 是蚂蚁金服开源的一个生产级.高时效.高可用的服务注册中心. 本系列文章重点在于分析设计和架构,即利 ...

  9. 基于HBuilderX+UniApp+ColorUi+UniCloud 优宝库 开发实战(一)

    1.   优宝库介绍 优宝库是基于阿里妈妈.淘宝联盟 淘宝商品Api,前端使用HBuilderX + UniApp + ColorUi,后端采用UniClound 精选淘宝商品进行推荐的App程序.下 ...

  10. odoo之技巧合集一

    罗列一些odoo开发中的简单但有效的方法: 1.重写odoo登录代码 参考链接:odoo10-重写登录方法 from odoo import models, fields, api, SUPERUSE ...