POJ 2001 Shortest Prefixes(字典树)
2015-10-06 17:56
453 查看
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
/************************************************************************/
题意:有多个字符串,现要用各自的前缀唯一表示各自的字符
对于唯一的说法,当有两个字符串car和carriage时,若用前缀car来表示carriage是不可行的,这并不唯一,因为前缀car也有可能是表示car的,所以我们不得不用前缀carr来表示carriage
解题思路:一道字典树的入门题,也算是模板题吧
将给定的字符串建立一棵字典树,每个字符出现的次数都要记录下来,而询问阶段只需遍历字典树,当某个字符出现次数为1或者当前字符串遍历完,都可以将其作为前缀表示
样例可以表示成如下这棵树:
圈内表示字符以及其出现的次数
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 0 | Accepted: 0 |
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
/************************************************************************/
题意:有多个字符串,现要用各自的前缀唯一表示各自的字符
对于唯一的说法,当有两个字符串car和carriage时,若用前缀car来表示carriage是不可行的,这并不唯一,因为前缀car也有可能是表示car的,所以我们不得不用前缀carr来表示carriage
解题思路:一道字典树的入门题,也算是模板题吧
将给定的字符串建立一棵字典树,每个字符出现的次数都要记录下来,而询问阶段只需遍历字典树,当某个字符出现次数为1或者当前字符串遍历完,都可以将其作为前缀表示
样例可以表示成如下这棵树:
圈内表示字符以及其出现的次数
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<stack> #include<math.h> #include<vector> #include<map> #include<set> #include<stdlib.h> #include<cmath> #include<string> #include<algorithm> #include<iostream> #define exp 1e-10 using namespace std; const int N = 25; const int inf = 1000000000; const int mod = 2009; struct tree { int t; int a[26]; }s[100000]; int p=0; void init(int z) { s[z].t=0; for(int i=0;i<26;i++) s[z].a[i]=-1; } void buildtree(char *c) { int i,k; for(i=k=0;c[i]!='\0';i++) { if(s[k].a[c[i]-'a']==-1) { s[k].a[c[i]-'a']=++p; init(k=p); } else k=s[k].a[c[i]-'a']; s[k].t++; } } void find(char *c) { int i,k; for(i=k=0;c[i]!='\0';i++) { k=s[k].a[c[i]-'a']; if(s[k].t==1) { c[i+1]='\0'; break; } } puts(c); } char ch[50*N] ; int main() { int i,j; init(0); for(i=0;~scanf("%s",ch[i]);i++) buildtree(ch[i]); for(j=0;j<i;j++) { printf("%s ",ch[j]); find(ch[j]); } return 0; }菜鸟成长记
相关文章推荐
- oc MRC初步认识
- Properties类(一)
- gethostbyname() -- 用域名或主机名获取IP地址
- 位运算符
- java 8函数式编程与Lambda表达式
- [经济学原理|微观部分]市场失灵与政府调节
- 黑马程序员——OC语言基础---id类型及应用场景
- 单链表逆转
- 高性能并发双向队列
- oc NSDate
- 【Scala学习笔记】3. Tuple、Array、Map
- [经济学原理|微观部分]一般均衡与福利经济学
- ReLU
- [wordpress使用]003_添加新文章
- [经济学原理|微观部分]生产要素价格和收入分配
- 遇到Host ‘xxx’ is not allowed to connet to this MySQL server 问题
- oc 字符串 数组 简单使用
- 关于Python多线程的理解
- javascript的介绍,实现和输出以及语法-javascript学习之旅(1)
- [经济学原理|微观部分]博弈论