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

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

看完题后,感慨:简直不能忍,这么裸的字典树。接下来就是无脑式敲 Trie 树了。这个题目能够拿来练练手速~。另外。一时没到比較合适的函数名,望 勿喷

题解就略了,容我贴下代码~

/****************************>>>>HEADFILES<<<<****************************/
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define rep1(i,a) for(int i = 1;i <= a;i++)
#define rep0(i,a) for(int i = 0;i < a;i++)
#define MP(a,b) make_pair(a,b)
#define PB(a) push_back(a)
#define fst first
#define snd second
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
/****************************>>>>>>DEBUG<<<<<<****************************/
#define out(x) cout<<x<<""
/****************************>>>>SEPARATOR<<<<****************************/
const int maxk = 26;
const int maxl = 20+5;
int N,M;
struct Node
{
int cnt;
Node* pNext[maxk];
Node() : cnt(0)
{
rep0(i,maxk) pNext[i] = NULL;
}
};
struct Trie
{
Node* const pRoot;
Trie() : pRoot(new Node()) {}
void AddWord(const char str[],int len);
// int FindPredix(const char str[],int len);
void Query(const char str[],int len);
void Release(const Node *p);
}dic;
void Trie::AddWord(const char str[],int len)
{
Node* ptr = pRoot;
for(int i = 0;i < len;i++)
{
int nPos = str[i] - 'a';
if(ptr->pNext[nPos] == NULL) ptr->pNext[nPos] = new Node();
ptr->cnt++;
ptr = ptr->pNext[nPos];
}
ptr->cnt++;
}
void Trie::Release(const Node* p)
{
for(int i = 0;i < maxk;i++) if(p->pNext[i] != NULL) Release(p->pNext[i]);
delete p;
}
void Trie::Query(const char str[],int len)
{
Node* ptr = pRoot;
for(int i = 0;i < len;i++)
{
int nPos = str[i] - 'a';
if(ptr->pNext[nPos] == NULL) return;
if(ptr->cnt == 1) return;
putchar(str[i]);
ptr = ptr->pNext[nPos];
}
}
char buf[1005][maxl];
int main()
{
//FIN;
int cnt = 0;
while(~scanf("%s",buf[cnt]))
dic.AddWord(buf[cnt],strlen(buf[cnt])),cnt++;
for(int i = 0;i < cnt;i++)
{
printf("%s ",buf[i]);
dic.Query(buf[i],strlen(buf[i]));
puts("");
}
dic.Release(dic.pRoot);
return 0;
}

POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】的更多相关文章

  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 trie入门

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

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

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

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

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

  5. POJ 2001 Shortest Prefixes(字典树)

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

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

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

  7. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  8. poj 2001 Shortest Prefixes

    字典树的简单应用. #include<stdio.h> #include<string.h> ][]; struct node{ int cnt; node *next[]; ...

  9. poj 2001 Shortest Prefixes(字典树)

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

随机推荐

  1. Zabbix整合MegaCLI实现物理硬盘的自动发现和监控

    MegaCLI是LSI提供的用户空间管理RAID卡(LSI芯片)工具,适用于大多数的Dell服务器. MegaCLI介绍: http://zh.community.dell.com/techcente ...

  2. BZOJ 1010: [HNOI2008]玩具装箱toy(DP+斜率优化)

    [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊 ...

  3. 【bzoj2127】happiness 网络流最小割

    题目描述 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友如果能同时选文 ...

  4. topK问题解法

    topK问题的最佳解法是堆排,下面介绍用堆排来解决该问题. 堆排解决topK问题的思路,取出前K个数,最重要的就是要减少比较的次数,用堆排维护一个K大小的堆,比如一个小顶堆,则堆顶为堆中最小的值,将堆 ...

  5. JavaScript简明教程之Node.js

    Node.js是目前非常火热的技术,但是它的诞生经历却很奇特. 众所周知,在Netscape设计出JavaScript后的短短几个月,JavaScript事实上已经是前端开发的唯一标准. 后来,微软通 ...

  6. ionic2添加自定义文字

    上次更新到如何添加自定义图标,紧接着这次更新ionic2如何添加自定义字体 首先你要有自己的字体文件以.ttf结尾的文件字体 :推荐个字体文件网站(相对来说流氓软件比较少的)http://www.ps ...

  7. HTTP调试工具:Fiddler介绍

    原文发布时间为:2010-08-25 -- 来源于本人的百度文章 [由搬家工具导入] 这个工具我已经使用比较长时间了,对我的帮助也挺大,今天我翻译的微软的文章,让更多的朋友都来了解这个不错的工具,也是 ...

  8. windows 加入域

    点击computer,右击选system ,点 change setting,填写domain和computer-name 加入域,下次登陆加入,在域中会检查computer name

  9. [LeetCode] Merge Intervals 排序sort

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  10. linux中的strip命令简介------给文件脱衣服【转】

    转自:http://blog.csdn.net/stpeace/article/details/47090255 版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则 ...