POJ 2001 Shortest Prefixes 字典树Trie
来源:互联网 发布:java上传excel并解析 编辑:程序博客网 时间:2024/04/19 14:32
C - Shortest Prefixes
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64uDescription
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
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 10000007using 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;}
0 0
- POJ 2001 Shortest Prefixes 字典树Trie
- POJ 2001 Shortest Prefixes(字典树Trie)
- POJ 2001 Shortest Prefixes(字典树Trie)
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
- POJ 2001 Shortest Prefixes (字典树 TRIE)
- poj 2001 Shortest Prefixes Trie树
- poj 2001 Shortest Prefixes(Trie树)
- poj 2001 Shortest Prefixes Trie树
- poj 2001 Shortest Prefixes(trie树)
- POJ 2001 - Shortest Prefixes (Trie树)
- poj 2001 Shortest Prefixes(trie树)
- poj -- 2001 Shortest Prefixes (Trie 树)
- POJ-2001 Shortest Prefixes(Trie树)
- poj 2001 Shortest Prefixes (Trie树)
- poj Shortest Prefixes ——trie树、字典树,基础!!
- poj 2001 Shortest Prefixes(Trie)
- poj 2001 Shortest Prefixes trie
- poj 2001 Shortest Prefixes Trie
- 东软睿道实训日记——第八天
- 关于延迟调用
- POM.xml详解
- 自娱自乐
- linux todo
- POJ 2001 Shortest Prefixes 字典树Trie
- shell编程:引用变量、内部变量、条件测试、字符串比较、整数比较、文件比较、逻辑操作、引号、通配符、调试执行
- Linux中shell文件操作大全
- SQL_Server_2008修改sa密码的方法
- gpssh-exkeys ganglia perf stat
- UVA 10791 - Minimum Sum LCM
- vs2010及vs2008快捷键大全
- 对SSO和一般登陆的理解
- 一些软件设计的原则