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. 【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization

    Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, ...

  2. 【JavaWeb】Cookie&Session

    Cookie&Session Cookie 什么是 Cookie Cookie 即饼干的意思 Cookie 是服务器通知客户端保存键值对的一种技术 客户端有了 Cookie 后,每次请求都发送 ...

  3. kubernetes环境部署单节点redis

    kubernetes部署redis数据库(单节点) redis简介 Redis 是我们常用的非关系型数据库,在项目开发.测试.部署到生成环境时,经常需要部署一套 Redis 来对数据进行缓存.这里介绍 ...

  4. .NET 5网络操作的改进

    随着.net 5在11月的发布,现在是谈论网络栈中许多改进的好时机.这包括对HTTP.套接字.与网络相关的安全性和其他网络通信的改进.在这篇文章中,我将重点介绍一些版本中更有影响力和更有趣的变化. H ...

  5. 通过trace分析优化其如何选择执行计划

    mysql5.6提供了对sql的跟踪trace,通过trace文件能够进一步了解为什么优化其选择执行计划a而不选b执行计划,帮助我们更好的理解优化其的行为. 使用方式:首先打开trace,设置格式为j ...

  6. 目前用下来最溜的MacOS微信多开工具!

    一个生活微信,一个工作微信是很多上班族的基本配置. 但由于微信客户端在PC端上只能打开一个,这使得在上班时候就非常不便,一个号在PC端上登录,一个在手机上使用,但是上班时候又不能一直看手机,不然老板还 ...

  7. APM调用链产品对比

    APM调用链产品对比 随着企业经营规模的扩大,以及对内快速诊断效率和对外SLA(服务品质协议,service-level agreement)的追求,对于业务系统的掌控度的要求越来越高,主要体现在: ...

  8. FTP使用Socket SSL流程认证(一)

    关于Ftp使用SSL流程认证 本文章使用的是C#,ftp服务器为FileZilla 注:如果不是使用的Socket可以使用FtpWebRequst类,说实话,该类比较简单,但现在说的是SOCKET,网 ...

  9. Centos 安装postgreSQL9.4.3

    rpm -ivh http://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7.2-x86_64/pgdg-centos94-9.4-3 ...

  10. 路由协议-RIP协议

    一.路由协议的发展历程和分类 距离矢量路由协议--听信"谣言",使用跳数作为度量值,最大16(0-15)跳:RIP 链路状态路由协议--"地图"路由协议:OSP ...