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
carbonate

Sample Output

carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

Source

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的更多相关文章

  1. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  2. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  3. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

  4. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  5. POJ 2001 Shortest Prefixes(字典树活用)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21651   Accepted: 927 ...

  6. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  7. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  8. poj 2001 Shortest Prefixes(特里)

    主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...

  9. poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...

随机推荐

  1. 【不积跬步,无以致千里】mysql 多行合并函数

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  2. Java安全之对称加密、非对称加密、数字签名

    原文地址: http://blog.csdn.net/furongkang/article/details/6882039 Java中加密分为两种方式一个是对称加密,另一个是非对称加密.对称加密是因为 ...

  3. 远程重启IIS服务

    方法一: $UserName = "administrator" $serverpass = "pass" $server = "10.4.19.60 ...

  4. Java数据抓取经验【转载】

    本人担任职友集的java工程师五年,其中抓取数据占主要的一部分,抓取的信息只要有两部分,职位和简历,其中职位的抓取量为日均插入量为30万,更新量 为60万,抓取全国300多个人才网站.职友集(现在改名 ...

  5. Nginx加多个tomcat实现负载均衡,动静分离

    一:Nginx+Tomcat的动静分离 所谓动静分离就是通过nginx(或apache等)来处理用户端请求的图片.html等静态的文件,tomcat(或weblogic)处理jsp.do等动态文件,从 ...

  6. Oracle DB 执行表空间时间点恢复

    • 列出在执行表空间时间点恢复(TSPITR) 时会发生的操作 • 阐释TSPITR 使用的术语的定义 • 确定适合将TSPITR 用作解决方案的情况 • 确定时间点恢复的正确目标时间 • 确定不能使 ...

  7. UIWebView的使用,简单浏览器的实现

    #import "ViewController.h" @interface ViewController () <UIWebViewDelegate> @propert ...

  8. Android_Menu_PopupMenu

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  9. Centos7 安装mongodb3.2.9 过程

    1:wget --no-check-certificate  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.2.9.tg ...

  10. VFS对象总结

    关键术语: 超级快(super block)对象: 一个超级块对应一个具体的文件系统(已经安装的文件系统类型如 ext2,此处是实际的文件系统,不是 VFS). iNode 对象: inode是内核文 ...