POJ 2001 Shortest Prefixes(字典树活用)
Shortest Prefixes
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 21651 | Accepted: 9277 |
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
题意:输出每个单词以及它的独有前缀;
分析:将输入单词建树,记录每个节点访问次数,建树完成后枚举每个单词的节点并输出节
点对应的字母,找到访问次数为1的节点则停止查询该单词
数组实现
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int t[15000][26];
char ss[1001][21];
int num[15000],pos=1;
void insert(char *s)
{
int x,rt=0;
for(int i=0;s[i];i++)
{
x=s[i]-'a';
if(!t[rt][x])
t[rt][x]=pos++;
rt=t[rt][x];
num[rt]++;
}
}
void find(char *s)
{
int x,rt=0;
for(int i=0;s[i];i++)
{
x=s[i]-'a';
printf("%c", s[i]);
rt=t[rt][x];
if(num[rt]==1)
return ;
}
}
int main()
{
int c=0;
while(~scanf("%s",ss[c]))
insert(ss[c++]);
for(int i=0;i<c;i++)
{
printf("%s ",ss[i]);
find(ss[i]);
printf("\n");
}
return 0;
}
指针实现
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char ss[1001][21];
struct node
{
int vis;
struct node *next[26];
node()
{
vis=0;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*rt;
void insert(char *s)
{
node *p=rt;
for(int i=0;s[i];i++)
{
int x=s[i]-'a';
if(!p->next[x])
p->next[x]=new node;
p=p->next[x];
p->vis++;//访问次数增加
}
}
void find(char *s)
{
node *p=rt;
for(int i=0;s[i];i++)
{
printf("%c",s[i]);
int x=s[i]-'a';
p=p->next[x];
if(p->vis==1)//访问一次的就是独有的
return ;
}
}
int main()
{
rt=new node;
int c=0;
while(scanf("%s",ss[c])!=EOF)
insert(ss[c++]);
for(int i=0;i<c;i++)
{
printf("%s ",ss[i]);
find(ss[i]);
printf("\n");
}
return 0;
}
POJ 2001 Shortest Prefixes(字典树活用)的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- poj2001 Shortest Prefixes(字典树)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21642 Accepted: 926 ...
随机推荐
- [Kubernetes]安装和配置kubectl
安装kubectl 安装kubectl比较简单,几条命令即可(#后面为注释内容): #下载最新版本: curl -LO https://storage.googleapis.com/kubernete ...
- 数字图像处理的Matlab实现(1)—绪论
第1章 绪论 1.1 什么是数字图像处理 一幅图像可以定义为一个二维函数\(f(x,y)\),这里的\(x\)和\(y\)是空间坐标,而在任意坐标\((x,y)\)处的幅度\(f\)被称为这一坐标位置 ...
- 【转】MySQL— 基础
[转]MySQL— 基础 目录 一.MySQL概述 二.下载安装 三.数据库操作 四.数据表操作 五.表内容操作 一.MySQL概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司 ...
- 【转】HTTP
[转]HTTP 一.http概述 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTT ...
- 查询tensorflow中的函数用法
一下均在ubuntu环境下: (1)方法一,使用help()函数: 比如对于tf.placeholder(),在命令行中输入import tensorflow as tf , help(tf.plac ...
- Shell基础总结
一.用户登陆进入系统后的系统环境变量 $HOME 使用者自己的目录 $PATH 执行命令时所搜寻的目录 $TZ 时区 $MAILCHECK 每隔多少秒检查是否有新的信件 $PS1 在命令列时的提示号 ...
- Webpack2 中的 NamedModulesPlugin 与 HashedModuleIdsPlugin
要讨论Webpack 2中新增的这两个plugin的功能,还要先从使用Webpack打包的项目的前端资源缓存方案说起. 通常在使用了Webpack的项目中我们会使用CommonsChunkPlugin ...
- 搭建Unity安卓开发环境
原文见 https://blog.csdn.net/chenggong2dm/article/details/20654075 tiny教程 https://docs.unity3d.com/Pack ...
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 解决办法
♦ 未在 Java构建路径中 找到父类 "javax.servlet.http.HttpServlet" ♦ 解决办法: 项目右击 → Build Path → 右侧 Add L ...
- win10安装MarkdownPad 2报错This view has crashed的处理及md简单语法
# #开头是段落的意思 不带#号是普通内容 # 下划线的写法是两个#号 ## # 列表的写法,以*号开头 * spring * summer * autumn * winter # 以竖线开头 > ...