Shortest Prefixes

2023-10-31

http://poj.org/problem?id=2001

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
典型的字典树,不过用map也可以过的;
//#include<malloc.h>
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node
{
int num;
struct node*next[27];
};
node *head;
void insert(char *s)//插入字母;
{
int len,i,k,j;
node *p1,*p2;
p1=head;
len=strlen(s);
for(i=0;i<len;i++)
{
if(p1->next[s[i]-'a']==0)
{
p2=(node*)malloc(sizeof(node));
for(j=0;j<26;j++)
p2->next[j]=0;
p1->next[s[i]-'a']=p2;
p1=p2;
p1->num=1;
}
else
{
p1=p1->next[s[i]-'a'];
p1->num++;
}
}
}
void find(char *s)//查询;
{
int i,j,k,len=strlen(s);
node*p=head;
for(i=0;i<len;i++)
{
if(p->num!=1)
{
printf("%c",s[i]);
p=p->next[s[i]-'a'];
}
else
break;
}
}
int main()
{
char s[1002][21];
int i,j,k,m,n;
head=(node*)malloc(sizeof(node));
for(i=0;i<26;i++)
head->next[i]=0;
head->num=2;
i=0;
while(gets(s[i]))
{
insert(s[i]);
i++;
}
for(j=0;j<i;j++)
{
printf("%s ",s[j]);
find(s[j]);
printf("\n");
}
return 0;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Shortest Prefixes 的相关文章

随机推荐

  • 推荐算法(一):协同过滤系列

    一 协同过滤 collaborative filtering 一种ItemCF推荐baseline 1 输入 user item相关矩阵 2 中间结果 item间相似度计算 item i与item j间相似度 分子 与二者均有关联的user
  • 【Hadoop生态圈】3.Zookeeper入门教程及集群环境搭建

    文章目录 1 简介 2 环境准备 3 修改Zookeeper配置文件 4 复制安装包到从节点并设置myid 5 启动集群 6 zkCli常用命令 1 简介 ZooKeeper是一个分布式的 开放源码的分布式应用程序协调服务 是Google的
  • ChatGPT是否会终结申请海外留学的文书时代?

    ChatGPT的爆火 也让不少准留学生们不禁会问 这一技术的产生是否会影响申请文书的写作 美国Insidehighered的专栏作家Jim Jump给出了自己的观点 ChatGPT对热爱文字和从事教育工作的人提出了特别的挑战 如果老师不能确
  • Unity中UGUI中各类UI元素跟随画面适应问题

    案例背景 这是一个即将发布陈web 版并且在手机上观看的 小交互 我要保证左上角按钮 保持相对位置不变 最终设置如下 简单版教程 很简单 Unity为每个UI元素提供了一个Rect Transform 里面有个Anchors 锚点 Unit
  • 如何搭建虚拟专有网络访问公司内网

    前言 因为公司开发都是内网环境 以往居家办公或非公司环境 都需要进行远程到公司电脑进行办公 为了方便部门同事出差驻场开发 搭建了虚拟专有网络 在实际搭建过程中使用了OpenVPN和SoftEtherVPN两种方式 做个总结记录 个人还是更推
  • python 解析大疆禅思L1 激光数据LDR格式

    个人微信 394467238 最近想把大疆禅思L1 录制的激光 LDR 数据 也就是把里面的数据一帧一帧的抽取出来 然后和图像数据做一个匹配 奈何问了一圈大疆的技术支持 就是不对外开放这个数据保存的协议 木有办法 只好自己动手尝试硬破解了
  • beanUtils封装表单数据到javaBean

    当表单数据多的时候避免太多的request getParament 方式获取数据 关键方法 BeanUtils populate p req getParameterMap 本例 获取前端表单数据 封装到javabean 练习中写了一些反射
  • OpenWRT安装docker内核kernel版本不够

    记录下 在openwrt中安装docker docker compose dockerd遇到如下错误 Collected errors pkg hash check unresolved cannot find dependency ker
  • 编程新手表示很想知道JAVA中Bean是什么?

    原文 编程新手表示很想知道JAVA中Bean是什么 NanSan 小编发现很多人都在问JAVA中Bean是什么 简单笼统的说就是一个类 一个可复用的类 这样的解释可能看着都还是云里雾里 跟没说一样 下面详细介绍吧 javaBean在MVC设
  • 拷贝构造函数的一个对象访问私有成员的问题

    最近遇到这样一个面试题 面试题 CString函数拷贝控制成员的编写 过程中遇到一个问题 真是当时让我疑惑不解 查查资料 原来是一时糊涂 看看人家的解答 不错 遂转一下 很简单 就是当时没转过弯来 原文如下 http blog csdn n
  • MapReduce运行流程

    MapRecude运行流程 1 客户端提交代码 job watiforcompletion 开始运行 2 请求到ResourceManager 经理 请求运行 ResourceManager返回jobId 和让客户端提交资源的路径 3 客户
  • 【黑马点评】达人探店

    黑马点评 达人探店 1 发布探店笔记 探店笔记类似点评网站的评价 往往是图文结合 对应的表有两个 tb blog 探店笔记表 包含笔记中的标题 文字 图片等 tb blog comments 其他用户对探店笔记的评价 具体发布流程 根据找到
  • STM32CubeProg 下载及安装教程

    先点赞 再看博客 顺便点个关注鼓励一下 如果文章看完 觉得不错的话可以点个收藏 日后不迷路 STM32CubeProg 下载及安装教程 1 前言 1 1 基本介绍 1 2 主要特点 1 3 准备工作 2 软件下载 2 1 Java 官网下载
  • 字符串转base64,base64转字符串

    JavaScript原生提供两个Base64相关方法 btoa 字符串或二进制值转为Base64编码 atob Base64编码转为原来的编码 注意 base64转码为 号的后台存储问题 可遍历将 号转换为其他字符 备注 利用这两个原生方法
  • 用 visio 2013 绘制倾斜立方体

    依次点击 形状 更多形状 常规 方块 2 选择 三维框 形状即可 后续可根据需求对其进行缩放 变形 反转等操作
  • Centos 7配置网络文件及命令

    1 查看网络配置文件 命令行执行ip addr或ifconfig可以知道为ens33 然后cd 到目录 etc sysconfig network scripts 可以看到ifcfg ens33就是我们要找的文件 然后进入编辑 配置ip d
  • Thread & Timer

    Thread Timer 待续
  • element v-loading 文字描述 icon颜色 字体颜色

    v loading mapInfoLoading element loading text 数据加载中 class loading map icon颜色和字体颜色
  • windows CE初次接触(一次升级长安致尚XT高德导航的经历)

    以前不知道windows 操作系统在车载导航方面也有应用 即windows CE 帮忙升级一个 长安致尚XT 汽车上的windows CE下的 高德导航 注意到这是 百度收购 了的 提供免费地图的品牌 现在这篇文章应该用不上了 高德导航是被
  • Shortest Prefixes

    http poj org problem id 2001 Description A prefix of a string is a substring starting at the beginning of the given stri