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 ...
随机推荐
- IntelliJ IDEA 配置
1.让IntelliJ IDEA 驼峰选择生效 驼峰选择,就是选择的时候按照驼峰规则选择单词,不是选择整个单词 让IntelliJ IDEA 驼峰选择生效 2.查看当前类中的所有方法 Alt+7 3. ...
- CVE-2019-0797漏洞:Windows操作系统中的新零日在攻击中被利用
https://securelist.com/cve-2019-0797-zero-day-vulnerability/89885/ 前言 在2019年2月,卡巴实验室的自动漏洞防护(AEP)系统检测 ...
- 网页块元素定位建议使用的xpath方式
取上图的新手上路文字 使用xpath "//div[@class='pbm mbm bbda cl']//li[contains(string(),'用户组')]/span/a/text() ...
- matlab处理手写识别问题
初学神经网络算法--梯度下降.反向传播.优化(交叉熵代价函数.L2规范化) 柔性最大值(softmax)还未领会其要义,之后再说 有点懒,暂时不想把算法重新总结,先贴一个之前做过的反向传播的总结ppt ...
- python3+selenium框架设计07-unittest单元测试框架
可以自行百度学习下单元测试框架,或者看Python3学习笔记26-unittest模块 在项目下新建一个entrance.py文件.并使用之前的测试用例进行演示.目前项目结构. 在entrance ...
- C++ URLencode library
I need a library that can URLencode a string/char array. Now, I can hex encode an ASCII array like h ...
- C++游戏开发需要阅读的书籍
如果要自学游戏程序开发的话,可以看看下面的,呵呵. 游戏开发资料(PDF书都是中文版的,非英文,很多是本人自己扫描制作,从未网上发布过,所以独家啦): 1.Gamebryo 2.2游戏引擎(盛大.腾 ...
- 【转】Java并发编程:并发容器之CopyOnWriteArrayList
Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才会真正把内容Copy出去形成一个新的内容然后再改, ...
- cocos2dx 3.x 修改NDK_ROOT、ANDROID_SDK_ROOT、ANT_ROOT路径
CMD到setup.py目录 Python setup.py -h 查看帮助: Options: -h,--help showthis help message and exi ...
- 基于OpenSSL自建CA和颁发SSL证书
关于SSL/TLS介绍见文章 SSL/TLS原理详解.关于证书授权中心CA以及数字证书等概念,请移步 OpenSSL 与 SSL 数字证书概念贴 . openssl是一个开源程序的套件.这个套件有三个 ...