字典树:POJ 2001
来源:互联网 发布:js定时执行函数 编辑:程序博客网 时间:2024/06/05 11:02
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 14486 Accepted: 6243
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".
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
carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonate
Sample Output
carbohydrate carbohcart cartcarburetor carbucaramel caracaribou caricarbonic carbonicartilage carticarbon carboncarriage carrcarton cartocar carcarbonate carbona
#include <stdio.h>#include <iostream>#include <string.h> using namespace std;struct tree{int cnt;tree *p[26]; }*root;void CreateTree(char *s){tree *r, *tmp;r = root;int i, j, len;len = strlen(s);for(i = 0;i < len;i ++){if(r -> p[s[i]-'a'] == NULL){tmp = new tree;tmp -> cnt = 0;for(j = 0;j < 26;j ++) tmp -> p[j] = NULL;r -> p[s[i] - 'a'] = tmp;}r = r -> p[s[i]-'a'];r -> cnt ++;}}void search(char *s){tree *r;r = root;int i, len;len = strlen(s);for(i = 0;i < len;i ++){r = r -> p[s[i] - 'a'];printf("%c",s[i]);if(r -> cnt == 1) break;}printf("\n");}int main(void){char str[1001][25];root = new tree;for(int i = 0;i < 26;i ++)root -> p[i] = NULL;int i = 0, k;while(scanf("%s", str[i]) != EOF){CreateTree(str[i]);i ++;}k = i;for(int j = 0;j < k;j ++){printf("%s ", str[j]);search(str[j]);} return 0;}
阅读全文
0 0
- POJ 2001 字典树
- 字典树 poj 2001
- POJ 2001 字典树
- poj-2001字典树
- POJ 2001 字典树
- poj 2001 字典树
- poj 2001字典树
- POJ 2001 字典树
- 字典树:POJ 2001
- POJ 2001 Trie 字典树
- POJ 2001 trie字典树
- poj 2001 字典树问题
- POJ 2001:Shortest Prefixes(字典树)
- POJ-2001(trie字典树)
- POJ 2001 字典树裸模版
- poj 2001 Shortest Prefixes(字典树)
- Trie 字典树 poj 2001 示例
- Trie 字典树 poj 2001 示例
- 使用Echarts绘制中国地图看这篇就够了(认真脸!)
- 并查集 hdu 1232 畅通工程
- c#文件夹常用操作,属性设置,遍历、压缩
- 三维计算机视觉(六)--3DSC(3D形状上下文特征)
- Java虚拟机类加载机制
- 字典树:POJ 2001
- Selenium + PhantomJS + python 简单实现爬虫的功能
- 《着色器和屏幕特效》读书笔记第九章-游戏可玩性和屏幕特效
- 1025. 反转链表 (25)----Python
- 什么是RUP
- Error: …… ,Manifest merger failed with multiple 解决方案
- 设置Maven项目使用的jdk版本
- Java类加载机制——案例分析
- Uvalive 7037 The Problem Needs 3D Arrays (最大密集子图)