POJ 2001 - Shortest Prefixes《字典树,求每个单词的最简化》
来源:互联网 发布:sql计算年龄语句 编辑:程序博客网 时间:2024/06/08 01:27
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
这个题主要是对最简化的理解-.-开始一直不理解。。。
只要那个单词有一个是他自己特有的元素。那他就是最简的了-.-
car是因为它就三个=.=再多也没有了
题解:
先把单词添加入树,并求出每个树出现的次数,
再查找最简树。。。。
代码:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;int n,wei;struct node{ char hh[22]; int ll; int hao;char qianzui[22]; }ch[1100];struct trie{ struct trie *hai[26]; int op;}di[26000];struct trie *root;bool cmp(node xx,node yy){ int ll=min(xx.ll,yy.ll); for (int i=0;i<=xx.ll;i++) { if (xx.hh[i]!=yy.hh[i]) return xx.hh[i]<yy.hh[i]; }}bool cm(node xx,node yy){return xx.hao<yy.hao;}void jia(int ii){char xx[22];strcpy(xx,ch[ii].hh); struct trie *kk; kk=root;int lp=-1; int ll=strlen(xx); for (int i=0;i<ll;i++) { if (!kk->hai[xx[i]-'a']) { kk->hai[xx[i]-'a']=&di[wei++]; memset(kk->hai[xx[i]-'a'],0,sizeof(trie)); kk->hai[xx[i]-'a']->op=0; } kk=kk->hai[xx[i]-'a']; kk->op++; }}void zhao(int ii){char xx[22];strcpy(xx,ch[ii].hh); struct trie *kk; kk=root;int lp=0; int ll=strlen(xx); for (int i=0;i<ll;i++) { if (!lp) printf("%c",xx[i]); kk=kk->hai[xx[i]-'a']; if (kk->op==1) lp=1; } printf("\n");}int main(){n=0;while (gets(ch[n].hh),ch[n].hh[0]){ ch[n].ll=strlen(ch[n].hh); ch[n].hao=n;n++;} sort(ch,ch+n,cmp); wei=0;root=&di[wei++]; memset(root,0,sizeof(trie)); bool fafe=true; for (int i=0;i<n;i++) { jia(i); } sort(ch,ch+n,cm); for (int i=0;i<n;i++) { printf("%s ",ch[i].hh); zhao(i);} return 0;}
0 0
- POJ 2001 - Shortest Prefixes《字典树,求每个单词的最简化》
- POJ 2001:Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- poj 2001 -- Shortest Prefixes ( 字典树 )
- POJ 2001 Shortest Prefixes ( 字典树 )
- POJ 2001 Shortest Prefixes 字典树Trie
- POJ 2001 Shortest Prefixes(字典树Trie)
- poj 2001 Shortest Prefixes(字典树)
- [字典树] poj 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes 字典树
- POJ 2001-Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- POJ【2001】Shortest Prefixes----字典树
- POJ-2001--Shortest Prefixes (字典树)
- poj 2001 Shortest Prefixes 字典树
- poj 2001 Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- win7+ubuntu双系统重装win7
- Ubuntu 安装最新版本的Node.js
- 蓝牙(转载)
- Android studio开发工具开启篇
- 当我们说线程安全时,到底在说什么——Java进阶系列(二)
- POJ 2001 - Shortest Prefixes《字典树,求每个单词的最简化》
- What is hosts?
- Python中input 和raw_input的异同点
- ubuntu安装微信
- 一张图读懂互联网大咖
- owin学习笔记(1)
- 泛型程序设计
- Instruments(转载)
- Android_如何获取Android唯一标识(唯一序列号)