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 ...
随机推荐
- [python] 基础工具介绍好文推荐
Github上有个哥们写的,还不错,mark一下: https://github.com/lijin-THU/notes-python/blob/master/index.ipynb 相对全面的介绍了 ...
- golang printf中的%c,%d,%u.都分别代表输出的是什么类型的?
%表示格式化字符串输出 目前printf支持以下格式的输出,例如: printf("%c",a):输出单个字符. printf("%d",a):输出十进制整数. ...
- 【转】Python用数据说明程序员需要掌握的技能
[转]Python用数据说明程序员需要掌握的技能 https://blog.csdn.net/HuangZhang_123/article/details/80497951 当下是一个大数据的时代,各 ...
- HardNet解读
论文:Working hard to know your neighbor’s margins: Local descriptor learning loss 为什么介绍此文:这篇2018cvpr文 ...
- MySQL主从复制报错1594处理【转】
一.问题描述 Mysql主从复制模式中,slave上报错 “relay log read failure”,导致主从同步停止. mysql> show slave status\G ****** ...
- python3+selenium入门11-窗口切换
在打开新的浏览器窗口时,如果要定位新窗口的元素,需要先切换到这个新打开的窗口中,才能定位到该窗口下的元素. current_window_handle:获取当前句柄.可以把句柄理解成窗口的身份证 wi ...
- nginx proxy_set_header设置、自定义header
先来看下proxy_set_header的语法 语法: proxy_set_header field value; 默认值: proxy_set_header Host $proxy_host; pr ...
- 修改docker image存放位置
修改镜像和容器的默认存放路径 指定镜像和容器存放路径的参数是--graph=/var/lib/docker,我们只需要修改配置文件指定启动参数即可.刚好有个300g盘的挂在/data目录上,所以在这个 ...
- hadoop客户端如何配置
Hadoop集群主要是由三部分组成的:主节点.从节点和客户端,即master.slave和client.我们在搭建hadoop集群的时候通常只考虑了主节点和从节点的搭建,却忽略了客户端.当我们搭建完成 ...
- JQery插件zClip ----实现粘贴复制功能
使用了这个插件,但是用在table,td中话,我是一个列表来的,对此使用此插件还是有点问题的?点击其中的一个会全部都被选中. <script type="text/javascript ...