poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 6734 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
carbonateSample Output
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbonaSource
题意:给不超过1000个单词,输出能代表这个单词的唯一前缀,若没有則直接把整个单词输出~输出格式详见output~
trie,基本的数据结构,个人认为就是链表,不过是树形的而已~
ps: 被读数据坑到了~ 刚开始写的是 while(scanf("%s",bank[n++]) != -1) { inserts(bank[n-1]); } ,然后就各种WA,唉~
还好努力找到了错误~ 改成了while(scanf("%s",bank[n]) != -1){inserts(bank[n]); n++; },就神奇的过了,可能多个回车什么的导致错误吧。
#include<iostream>
#include<vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include<algorithm>
#define ll long long
#define eps 1e-8
using namespace std; struct nodes
{
int cnt;
struct nodes *next[];//26个后继
nodes()//结构体内的初始化函数
{
int i;
cnt = ;
for(i = ; i < ; i++)
next[i] = NULL;
}
} root,*temp; void inserts(char *word)
{
nodes *cur = &root;
while(*word )
{
int t = *word - 'a';
if(cur->next[t] == NULL)
{
temp = (nodes *)malloc(sizeof(nodes));//动态申请节点
temp->cnt = ;
for(int i = ; i < ; i++)
temp->next[i] = NULL;
cur->next[t] = temp;
}
cur = cur->next[t];
cur->cnt++;
word++;
}
} void searchs(char *word)
{
nodes *cur = &root;
while(*word && cur)//结束条件,缺一不可
{
if(cur->cnt == ) break;
printf("%c",*word);
cur = cur->next[*word - 'a'];
word++;
}
printf("\n");
} int main(void)
{
char bank[][];
int i,n = ; while(scanf("%s",bank[n]) != -)
{
inserts(bank[n]);
n++;
}
for(i = ; i < n; i++)
{
printf("%s ",bank[i]);
searchs(bank[i]);
}
return ;
}
poj 2001 Shortest Prefixes(字典树trie 动态分配内存)的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001 Shortest Prefixes(字典树活用)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21651 Accepted: 927 ...
- 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 ...
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- poj2001 Shortest Prefixes(字典树)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21642 Accepted: 926 ...
随机推荐
- Delphi XE10百集视频教程计划
1. 前言 本人现在的职业是Java程序员,一直想学习一个做桌面应用的编程语言,几年前无意中接触到Delphi,比VB功能强大,比C++语法更容易理解,加上Oracle的PL/SQL的底子,最终决定学 ...
- JVM系列(四)— 原子性、可见性与有序性
上一篇讲了Java内存模型的相关知识,模型设计正是围绕着并发过程中如何处理原子性,可见性和有序性这3个特征来建立的 一.原子性(Atomicity) 原子性的概念无需多说,熟悉事物的4个特性的应该比较 ...
- 懒散惯了,该收收心了,两天了,封装了一个R0下注册表类
写得乱七八糟. 看着自己写的代码,感觉都不像自己了. 我写的代码,风格这么差了么?思路这么乱了么? 我写代码这么累么? 不像以前的我了... 这段时间,太懒散了... 该继续努 ...
- 对于CDH LZO的安装
LZO好处.可以对hive这种快文件进行压缩,然后做索引达到速度大幅度改进索引 先是按照这个教程(网上很多可以找找看看) https://blog.csdn.net/Post_Yuan/articl ...
- Linux常用命令的缩写含义
命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户rpm:redhat package manager 红帽子打包管 ...
- Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2
给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1,null,nul ...
- 编译安装Python3.6.1
系统:CentOS 7 Python: 3.6.1 去官方网站下载Python的源码包 然后准备开发环境和服务器平台开发 注意:python编译还依赖一个zlib-devel(本人第一次编译就因为不知 ...
- vue.js_03_vue.js的样式和修饰符
1.vue.js的样式 <body> <div id="app"> <h1 :style="styleObj1">这是一个h ...
- dubbo入门学习(三)-----dubbo整合springboot
springboot节省了大量的精力去配置各种bean,因此通过一个简单的demo来整合springboot与dubbo 一.创建boot-user-service-provider 本篇博文基于上篇 ...
- 软件-DiskSpeekUp:DiskSpeekUp(磁盘整理工具)
ylbtech-软件-DiskSpeekUp:DiskSpeekUp(磁盘整理工具) Disk SpeedUP是一个完全自由和极快的碎片整理工具来分析,碎片整理和优化计算机性能的峰值磁盘. 它是安全没 ...