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 ...
随机推荐
- re 模块 (正则的使用)
一.正则表达式 英文全称: Regular Expression. 简称 regex或者re.正则表达式是对字符串操作的一种逻辑公式. 我们一般使用正则表达式对字符串进行匹配和过滤. 使用正则的优缺点 ...
- python随机生成图片
#-*-coding:utf-8-*- import tensorflow as tf import numpy as np import cv2 image = tf.random_uniform( ...
- Region服务器工作原理
- Drupal创建Omega 4.x 子主题layout笔记
Adding a new region to your Omega 4.0 subtheme (Drupal) Drupal: Creating a custom layout with Omega ...
- Ionic 列表、文本 自动 换行
1.采用row 布局的row-warp 来处理 <div class="item item-icon-right"> <span>图片相册</span ...
- 用js创建标签
html部分: <div id="div_1"> <table id="tab"></table> </div> ...
- 【DM8168学习笔记3】CodSourcery GCC Tool Chain安装过程记录
eagle@eagle-desktop:~$ cd/home/eagle/desktop eagle@eagle-desktop:~/desktop$ cd./vboxshared eagle@eag ...
- Spring Boot入门样例-001-Java和Maven安装配置
Spring Boot入门样例-001-Java和Maven安装配置 本文说明Java和Maven在windows下的安装和配置 前言 本Spring Boot入门样例准备工作参考: Spring B ...
- 公司jar包提交到集群的方法
yarn -jar xx.jar 此时包会提交到集群上运行 也可以把jar包放到hbase 的lib下面用hbase jar 方式调用
- 统一建模语言简介UML
统一建模语言(Unified Modeling Language,UML)是用来设计软件蓝图的可视化建模语言,1997 年被国际对象管理组织(OMG)采纳为面向对象的建模语言的国际标准.它的特点是简单 ...