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,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- inferred 和 freefrom
“Inferred” is the default setting for storyboards and it means the scene will show a navigation bar ...
- Spring ProxyFactory
ProxyFactory 是 Spring AOP的实现方式之一.下面介绍下ProxyFactory的用法. 1.接口定义 public interface UserReadService { pub ...
- 使用openssl库实现des,3des加密
原文地址: 使用openssl库实现des,3des加密 主要是调整了一下格式,以及一些变量的类型,以解决在VC2008下无法编译通过的问题. #include <stdio.h> #in ...
- 获取WMI硬件清单
WMI服务能够报告详细的硬件信息.通常,每个硬件都来自它们自己的WMI代理类.但是要找出这些硬件类的名字是不容易. 所有硬件类都在同一个WMI根下面,你可以在根类查询所有的硬件: Get-WmiObj ...
- nape.dynamics.InteractionFilter
(转载http://tomyail.com/blog/1123) Nape定义了三种交互方式: Collision(碰撞) Sensor(感应) Fluid(浮力) 默认情况下两个物体只会发生Coll ...
- Android设计模式(1)----单例模式
在非常多设计模式中.我相信大多数程序员最早接触的设计模式就是单例模式啦,当然了我也不例外. 单例模式应用起来应该是全部设计模式中最简单的.单例模式尽管简单,可是假设你去深深探究单例模式,会涉及到非常多 ...
- 5.6 在线DDL (online DDL)详解
ONLINE ddl 原理: online ddl相关参数 测试原表数据是否能进行ONLINE DDL方法: online DDL 局限性 测试
- careercup-中等难度 17.7
17.7 给定一个整数,打印该整数的英文描述(例如“One Thousand,Two Hundred Thirty Four”). 解法: 举个例子,在转换19 323 984时,我们可以考虑分段处理 ...
- qt helper
qt帮助文档(中文版) http://www.kuqin.com/qtdocument/index.html qt基础 http://www.devbean.net/2012/08/qt-study- ...
- Mysql数据库备份和按条件导出表数据
Mysql数据库备份和按条件导出表数据 一.备份数据库 # mysqldump -u root -p dbcurr>/home/20090219.sql mysqldum为备份命令,- ...