OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址:
http://bailian.openjudge.cn/practice/2001
http://poj.org/problem?id=2001
2.题目:
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12208 Accepted: 5210 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
3.思路:
4.代码:
//2010-05-09
//create by wuzhihui
#include<iostream>
#include <cstdio>
#include <cstdlib> using namespace std; #define chars 26
#define MAXCONTENT 1002
#define MAXLENGTH 21 typedef struct TrieNode
{
int num;
TrieNode *next[chars];
}TrieNode,*TrieTree;
char str[MAXCONTENT][MAXLENGTH]; TrieTree root=NULL; void insert(char *s)
{
int k=; if(root==NULL)
{
root=(TrieTree)malloc(sizeof(TrieNode));
root->num=;
for(k=;k<chars;k++) root->next[k]=NULL;
}
TrieTree p=root; int i=;
int index;
while(s[i]!='\0')
{
index=s[i]-'a';
if(p->next[index]==NULL)
{
p->next[index]=(TrieTree)malloc(sizeof(TrieNode));
p->next[index]->num=;
for(k=;k<chars;k++) p->next[index]->next[k]=NULL;
}
p=p->next[index];
p->num++;
i++;
}
} void search(char *s)
{
int i=,index;
TrieTree p=root;
while(s[i]!='\0')
{
index=s[i]-'a';
p=p->next[index];
putchar(s[i]);
if(p->num==) break;
i++;
}
putchar('\n');
}
int main()
{
int size=,i;
//int n=12;
while(scanf("%s",str[size])!=EOF)
//while(n-- && scanf("%s",str[size])!=EOF)
{
insert(str[size]);
size++;
}
for(i=;i<size;i++)
{
printf("%s ",str[i]);
search(str[i]);
}
//system("pause");
return ;
}
OpenJudge/Poj 2001 Shortest Prefixes的更多相关文章
- 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(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- POJ 2001 Shortest Prefixes(字典树活用)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21651 Accepted: 927 ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- 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 Description A prefix of a string is a substring starting at the ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- SQLite使用教程3 数据类型
http://www.runoob.com/sqlite/sqlite-data-types.html SQLite 数据类型 SQLite 数据类型是一个用来指定任何对象的数据类型的属性.SQLit ...
- eclipse 护眼色
eclipse windows 窗口背景颜色 保护视力 博客分类: 工具使用 EclipseWindows eclipse 窗口背景颜色 windows 窗口背景颜色
- 关于NoSQL数据库你应该知道的10件事
关系数据库模型已经流行了几十年了,但是一种新类型的数据库——被称为NoSQL,正在引起企业的注意.下面是关于它的优势和劣势的一个概述.二十多年以来,对数据库管理来说,关系数据库(RDBMS)模型一直是 ...
- error: variable '__this_module' has initializer but incomplete type错误解决
版权所有,转载必须说明转自 http://my.csdn.net/weiqing1981127 原创作者:南京邮电大学 通信与信息系统专业 研二 魏清 问题描述:使用SAM9X25 内核版本是2. ...
- Jquery easyui treegrid实现树形表格的行拖拽
前几天修改了系统的一个功能——实现树形列列表的行拖拽,以达到排序的目的.现在基本上功能实现,现做一个简单的总结. 1.拿到这个直接网上搜,有好多,但是看了后都觉得不是太复杂就是些不是特别想看的例子,自 ...
- URL是否有效
unit Unit1;interfaceuses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...
- Tricks Device (hdu 5294 最短路+最大流)
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- javascript---遇到关于this的相关问题(解决this)(持续更新中...)
1.在原型中使用this <!doctype html> <html lang="en"> <head> <meta charset=&q ...
- jQuery 学习笔记(未完待续)
一.jQuery概述 宗旨: Write Less, Do More. 基础知识: 1.符号$代替document.getElementById()函数 2.使 ...
- ERROR:_OBJC_CLASS_$_ADBannerView
http://stackoverflow.com/questions/4127489/iads-integration-with-cocos2d You are getting that error ...