POJ 2001 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
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
Source
裸的trie。
设置一个访问数组。
每次枚举就好
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 #include<queue>
6 #include<algorithm>
7 #include<cstdlib>
8 using namespace std;
9 const int MAXN=100001;
10 void read(int &n)
11 {
12 char c='+';int x=0,flag=1;
13 while(c<'0'||c>'9')
14 {c=getchar();if(c=='-')flag=-1;}
15 while(c>='0'&&c<='9')
16 {x=x*10+c-48;c=getchar();}
17 n=(x*flag);
18 }
19 struct TRIE
20 {
21 int ch[MAXN][51];
22 int sz;
23 int val[MAXN];
24 TRIE()
25 {memset(ch[0],0,sizeof(ch[0]));
26 memset(val,0,sizeof(val));
27 sz=1;}
28 int idx(char c)
29 {
30 return c-'a';
31 }
32 void Insert(char *s)
33 {
34 int l=strlen(s);int now=0;
35 for(int i=0;i<l;i++)
36 {
37 char c=idx(s[i]);
38 if(!ch[now][c])
39 {
40 memset(ch[sz],0,sizeof(ch[sz]));
41 ch[now][c]=sz++;
42 }
43 now=ch[now][c];
44 val[now]++;
45 //printf("%d ",val[now]);
46 }
47 }
48 void query(char *s)
49 {
50 int l=strlen(s);
51 int now=0;
52 for(int i=0;i<l;i++)
53 {
54 int c=idx(s[i]);
55 now=ch[now][c];
56 printf("%c",s[i]);
57 if(val[now]==1)
58 return ;
59 }
60 }
61 }trie;
62 char s[MAXN][201];
63 int n=1;
64
65 int main()
66 {
67
68 while(scanf("%s",s[n])==1)
69 {
70 n++;
71 trie.Insert(s[n-1]);
72 }
73 for(int i=1;i<n;i++)
74 {
75 printf("%s ",s[i]);
76 trie.query(s[i]);
77 printf("n");
78 }
79 return 0;
80 }
- 1588: [HNOI2002]营业额统计
- [git]撤销的相关命令:reset、revert、checkout
- Thrift教程初级篇——thrift安装环境变量配置第一个实例
- 1083: [SCOI2005]繁忙的都市
- 1015: [JSOI2008]星球大战starwar
- Tyvj P1813 [JSOI2008]海战训练
- 1820: [JSOI2010]Express Service 快递服务
- 3038: 上帝造题的七分钟2
- 1854: [Scoi2010]游戏
- Javascript字符串
- Codevs3278[NOIP2013]货车运输
- 关于使用lazytag的线段树两种查询方式的比较研究
- Java 持久化操作之 --XML
- 算法模板——splay区间反转 1
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法