POJ 2001 Shortest Prefixes
来源:互联网 发布:移动数据被禁用怎么开 编辑:程序博客网 时间:2024/06/15 13:31
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 19683 Accepted: 8538
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<cstdio>#include<cstring>using namespace std;char str[1010][50];struct Node{int cnt;Node *next[26];}*root;void init(){root=new Node;for(int i=0;i<26;i++)root->next[i]=NULL;}void insert(char *str){Node *ptr=root;int len=strlen(str);for(int i=0;i<len;i++){int tmp=str[i]-'a';if(ptr->next[tmp]==NULL){ptr->next[tmp]=new Node;ptr=ptr->next[tmp];ptr->cnt=1;for(int i=0;i<26;i++){ptr->next[i]=NULL;}}else{ptr=ptr->next[tmp];ptr->cnt++;}}return;}void search(char *str){int len=strlen(str);Node* ptr=root;for(int i=0;i<len;i++){putchar(str[i]);int tmp=str[i]-'a';ptr=ptr->next[tmp];if(ptr->cnt==1) break;}putchar('\n');return;}int main(){int cnt=0;init();while(~scanf("%s",str[cnt])){insert(str[cnt++]);}for(int i=0;i<cnt;i++){cout<<str[i]<<' ';search(str[i]);}}
阅读全文
0 0
- POJ 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- POJ-2001-Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ-2001-Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ-2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- 数字三角形
- Protobuf 在Ubuntu 14.04上的编译与使用
- RxAndroid线程调度器(五)
- C语言快速排序算法及三种优化方式
- 简单计算器
- POJ 2001 Shortest Prefixes
- Java并发编程:如何创建线程?
- Android Service生命周期,本地服务远程服务,前台服务后台服务,启动方式,与IntentService区别全面解析
- eclipse中jd反编译安装
- tensorflow学习二
- Linux指令合集
- PCA的理论基础
- CSS_视频播放
- MySQL与Java基础部分学习体会