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,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- mysql服务的启动和停止 net stop mysql net start mysql
第一招.mysql服务的启动和停止 net stop mysql net start mysql 第二招.登陆mysql 语法如下: mysql -u用户名-p用户密码 键入命令mysql -uroo ...
- [Java 7][msvcr100.dll] Error when load Eclipse
[Problem] After I updated to Java 7, I could not load Eclipse. Here comes the erros: Eclipse: eclips ...
- C#-设置窗体在显示器居中显示
在窗体的属性中查看:StartPosition属性,该属性的设置中有一个"CenterScreen"的选择项,该项就是设置窗体局中显示的.
- PCL 点云数据操作 OpenCV遍历数据
1.对于点云类型实例cloud,对其第i个点进行赋值操作,使用cloud.point[i].x 和 cloud.point[i].y 和cloud.point[i].z 分别对其XYZ坐标赋值. cl ...
- 微信公众平台java开发详解(工程代码+解析)
原文:http://blog.csdn.net/pamchen/article/details/38718947 说明:本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初 ...
- jbpm入门样例
1. jBPM的简介 jBPM是JBOSS下的一个开源java工作流项目,该项目提供eclipse插件,基于Hibernate实现数据持久化存储. 參考 http://www.jbos ...
- java_Cookie_example(你上次访问的时间)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...
- springMVC学习笔记三
十三.springMVC和spring集成 配置文件,spring的配置路径applicationContext.xml 在默认的web-inf下面 strut的配置文件默认在src下面 用了什么框架 ...
- 常用免费快递查询API对接案例
现在许多电商公司和ERP都会寻找比较适用的集成快递查询接口,减少对接难度,现在整理一下常用的免费快递查询接口,并附上调用案例,如果有觉得不对的地方,望能够一起沟通探讨! 一.快递查询接口 目前有提供免 ...
- Service的启动与停止、绑定与解绑
---恢复内容开始--- Service的意义就在于当软件停止之后还可以在背景中进行运行,换句话也就是说,比如一个音乐播放器,当我们退出音乐播放器的时候,还是希望它在背景中运行,也就是一直播放着音乐, ...