(trie) Shortest Prefixs(P2001)
来源:互联网 发布:马尔科夫矩阵怎么算 编辑:程序博客网 时间:2024/06/10 04:01
这个自己写的,要注意新开一个指针得用new,
#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<set>#include<cstring>using namespace std;#define N 26struct tree{int num;tree* next[N];tree(){num=1;memset(next,NULL,sizeof(next));}}*t;char hi[1001][30];void create(tree *tt,char *go){int i,j,k;tree *cur ;if (!*go)return ;k=*go-'a';cur=tt->next[k];//cout<<"yes";if (cur==NULL){//cout<<"1";cur=new tree;tt->next[k]=cur;create(cur,go+1);}else{//cout<<"2";cur->num++;create(cur,go+1);}}void put(tree *tt,char *go,bool f){//cout<<*go<<endl;int i,j,k;if (!*go||tt==NULL)return ;//cout<<"yes";k=*go-'a';tree *cur = tt->next[k];if (cur->num==1){printf("%c",*go);}else{printf("%c",*go);put(cur,go+1,false);}}int main(){freopen("in.txt","r",stdin);int i,j,k;char go[111];//t= new tree;t=new tree;int n=0;while (cin>>hi[n]){create(t,hi[n]);n++;}for (i=0;i<n;i++){printf("%s ",hi[i]);put(t,hi[i],true);printf("\n");}return 0;}
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 9212 Accepted: 3912
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".
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
Source
- (trie) Shortest Prefixs(P2001)
- UVA 11488 Hyper Prefixs Sets(字典树Trie)
- pku2001 Shortest Prefixes (trie)
- POJ2001[Shortest Prefixes]--Trie
- poj 2001 Shortest Prefixes(Trie)
- poj 2001 Shortest Prefixes trie
- poj 2001 Shortest Prefixes Trie
- POJ 2001 Shortest Prefixes // Trie
- Shortest Proper Prefix---Trie树
- poj2001 Shortest Prefixes (trie树)
- [poj 2001]Shortest Prefixes [Trie]
- poj2001 Shortest Prefixes trie树
- 【Trie树】POJ2001 Shortest Prefixes
- POJ Shortest Prefixes (Trie)
- Shortest Prefixes【trie模板题】
- zju2346 Shortest Prefixes(字典树 trie)
- zoj 2346 || poj 2001 Shortest Prefixes(Trie~!)
- poj 2001 Shortest Prefixes Trie树
- MySQL 存储过程初研究
- Javascript是实现HTML5强大功能的重要语言
- 1-10000这10000个数乱序的写出来,但是其中有个数字写错了(可能重复,或者不是1-10000中的数字),如何快速找出?
- Android系统编译
- Tomcat 配置
- (trie) Shortest Prefixs(P2001)
- C++.net
- socket server select模型值得注意的地方
- js常用实例:qq。。。
- 读书摘要 Where do disruptive ideas come from?
- 互斥量、临界区、信号量和时间的作用与区别
- Windows Phone开发遇到的一些事(一 饼状图,扇形,弧线等)
- BCGControlbar
- real-uid,effictive uid,saved-uid