poj 2001 字典树入门题(数组实现模板)
来源:互联网 发布:linux c 获取文件路径 编辑:程序博客网 时间:2024/05/16 23:52
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 14818 Accepted: 6399
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
Source
Rocky Mountain 2004
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int ch[20*1000+200][27];int sz;int val[20*1000+200];char s[1010][40];void init(){ sz=1; memset(ch,0,sizeof(ch)); memset(val,0,sizeof(val));}void insert(char *s){ int u=0,c; for(int i=0;i<strlen(s);i++) { c=s[i]-'a'; if(!ch[u][c]) ch[u][c]=sz++; u=ch[u][c]; val[u]++;//此题val应该放在for里面 } //val[u]=-1;}void search(char s[]){ int u=0,c; for(int i=0;i<strlen(s);i++) { c=s[i]-'a'; putchar(s[i]); if(val[ch[u][c]]==1) break; u=ch[u][c]; } putchar('\n');}int main(){ // freopen("in.txt","r",stdin); init(); int cnt=0; while(~scanf("%s",s[cnt])) { insert(s[cnt]); cnt++; } for(int i=0;i<cnt;i++) { printf("%s ",s[i]); search(s[i]); }}
0 0
- poj 2001 字典树入门题(数组实现模板)
- HDOJ1671 字典树入门题+模板程序(指针实现)
- 字典树模板(数组实现和指针实现)
- 【字典树模板(数组实现)】HDU 1251 统计难题
- HDOJ5687 字典树模板,数组实现
- 数组字典树模板
- poj 1195 (二维树状数组入门,模板题)
- Poj 2001 Shortest Prefix(字典树模板)
- POJ 2001 Shortest Prefixes(字典树入门)
- POJ 2001:Shortest Prefixes(字典树入门)
- 字典树入门——POJ 2001
- poj--2503+字典树入门题
- POJ 2503 Babelfish 字典树入门题
- poj 2418 字典树+dfs 入门题
- POJ 2503:Babelfish(字典树入门)
- 字典树数组实现
- (模板题)poj 2418 Hardwood Species(字典树)
- 字典树入门及实现(JAVA)
- linux基本命令-RPM包管理
- 从零开始学习OpenCL开发(一)架构
- 匈牙利算法
- 一个中转脚本
- Android学习 StateMachine与State模式
- poj 2001 字典树入门题(数组实现模板)
- 设计模式学习笔记--代理模式
- 漫谈数据挖掘从入门到进
- jquerr对象的height()/innerHeight()/outerHeight()/outerHeight(true)区别
- 安卓手电筒小程序
- 动态规划——算法总结(三)
- Demo Java 2 method to implement Multi-Thread
- Map.keySet()排序问题
- 匈牙利算法