POJ 2001 Shortest Prefixes 字典树Trie
2014-03-07 15:40
169 查看
C - Shortest Prefixes
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d
& %I64u
Submit Status
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
trie树的重新练习,与以前做的差不多,都是水题……在建字典树的时候,每个字母的权值都加1 ,如果超过1,说明已经有几个单词重复了。
所以要找出惟一能识别的最短字符串,那就是找到字母为1的时候……
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d
& %I64u
Submit Status
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
trie树的重新练习,与以前做的差不多,都是水题……在建字典树的时候,每个字母的权值都加1 ,如果超过1,说明已经有几个单词重复了。
所以要找出惟一能识别的最短字符串,那就是找到字母为1的时候……
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <list> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #define PI acos(-1.0) #define mem(a,b) memset(a,b,sizeof(a)) #define sca(a) scanf("%d",&a) #define pri(a) printf("%d\n",a) #define f(i,a,n) for(i=a;i<n;i++) #define F(i,a,n) for(i=a;i<=n;i++) #define MM 100005 #define M 1005 #define INF 10000007 using namespace std; string ss[M]; struct Trie { int ch[MM][27],sz; int val[MM]; Trie():sz(1) { memset(ch,0,sizeof(ch)); memset(val,0,sizeof(val)); } int idx(char c) {return c-'a';} void insert(char *s) { int u=0,i,c,l=strlen(s); for(i=0;i<l;i++) { c=idx(s[i]); if(!ch[u][c]) ch[u][c]=sz++; u=ch[u][c]; val[u]++; } } void query(string s) { int u=0,i,c,flag=0,l=s.size(); string sss; for(i=0;i<l;i++) { if(val[u]==1) {cout<<s<<' '<<sss<<endl;flag=1;break;} c=idx(s[i]); sss+=s[i]; u=ch[u][c]; } if(flag==0) cout<<s<<' '<<sss<<endl; } }T; int main() { char s[23]; int i=0; while(gets(s)) { T.insert(s); ss[i++]=s; } for(int j=0;j<i;j++) { T.query(ss[j]); } return 0; }
相关文章推荐
- POJ 2001 Shortest Prefixes 【LCP(Trie)】
- poj 2001 Shortest Prefixes Trie
- POJ 2001 Shortest Prefixes【第一棵字典树】
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
- poj 2001 Shortest Prefixes trie
- [字典树] poj 2001 Shortest Prefixes
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
- POJ 2001-Shortest Prefixes【字典树】
- poj 2001 Shortest Prefixes ——字典树入门
- poj 2001 Shortest Prefixes 【字典树】【找每一个字符串在字符串集里面的 最短且可唯一标识 的前缀】
- POJ 2001 Shortest Prefixes【字典树】
- poj 2001 Shortest Prefixes 字典树的输出问题的解释~~ 2012-4-14
- 【POJ 2001 Shortest Prefixes】+ 字典树
- poj 2001 Shortest Prefixes 字典树
- poj 2001 Shortest Prefixes 字典树
- poj 2001 Shortest Prefixes 字典树(模版题)
- Shortest Prefixes POJ - 2001(字典树)
- POJ【2001】Shortest Prefixes----字典树
- POJ 2001 Shortest Prefixes // Trie
- POJ 2001 Shortest Prefixes【字典树】