poj Shortest Prefixes 2001 (字典树模板)
2015-10-17 10:47
330 查看
Shortest Prefixes
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
Sample Output
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 15927 | Accepted: 6880 |
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
//题意:
给出很多单词,然你找出可以唯一标识这个单词的最短前缀。。。线段树模板
#include<stdio.h> #include<string.h> #include<algorithm> #define N 10100 using namespace std; char s[1010][30]; int ch [30]; int word ; int sz; int init() { sz=1; memset(ch[0],0,sizeof(ch[0])); memset(word,0,sizeof(word)); } void insert(char s[]) { int i,j,l=strlen(s); int k=0; for(i=0;i<l;i++) { j=s[i]-'a'; if(!ch[k][j]) { memset(ch[sz],0,sizeof(sz)); ch[k][j]=sz++; } k=ch[k][j]; word[k]++; } } void find(char s[]) { int i,j,l=strlen(s); int k=0; for(i=0;i<l;i++) { j=s[i]-'a'; k=ch[k][j]; printf("%c",s[i]); if(word[k]==1)//此时它是可以唯一标识的 return ; } } int main() { init(); int n=0,i; while(scanf("%s",s )!=EOF) { insert(s ); n++; } for(i=0;i<n;i++) { printf("%s ",s[i]); find(s[i]); printf("\n"); } return 0; }
相关文章推荐
- SQL DEFAULT 约束 高级教程
- join方法
- 在Mac上配置Android adb命令
- 电磁学第1章作业题答案
- 谈谈java虚拟机中的参数。
- 安卓Eclipse如何快速修改工程名及包名
- 谈谈Java技术优化的相关信息
- JS获取系统时间、计算两个日期天数、比较日期大小
- Mac系统打开命令行终端及查看操作系统版本号的方法
- 同步锁
- 单例
- Java中的Unix时间转换
- 【未完】mongodb安装+副本集搭建+数据导入
- 每个程序员都应该学会的重构方法
- shutdownInput
- 数据结构与算法是什么
- 帮忙看看。。。。
- iOS开发见闻-第1期
- UFT (自定义检查点——票数*价格是否等于总数)
- Android四大组件详解