POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
2015-08-06 17:11
399 查看
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
Sample Output
看完题后,感慨:简直不能忍,这么裸的字典树,接下来就是无脑式敲 Trie 树了,这个题目可以拿来练练手速~。另外,一时没到比较合适的函数名,望 勿喷
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)
题解就略了,容我贴下代码~
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 15574 | Accepted: 6719 |
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 树了,这个题目可以拿来练练手速~。另外,一时没到比较合适的函数名,望 勿喷
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)
题解就略了,容我贴下代码~
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
/****************************>>>>HEADFILES<<<<****************************/ #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <sstream> #include <algorithm> using namespace std; /****************************>>>>>DEFINE<<<<<*****************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w",stdout) #define rep(i,a,b) for(int i = a;i <= b;i++) #define rep1(i,a) for(int i = 1;i <= a;i++) #define rep0(i,a) for(int i = 0;i < a;i++) #define MP(a,b) make_pair(a,b) #define PB(a) push_back(a) #define fst first #define snd second #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 /****************************>>>>>>DEBUG<<<<<<****************************/ #define out(x) cout<<x<<"" /****************************>>>>SEPARATOR<<<<****************************/ const int maxk = 26; const int maxl = 20+5; int N,M; struct Node { int cnt; Node* pNext[maxk]; Node() : cnt(0) { rep0(i,maxk) pNext[i] = NULL; } }; struct Trie { Node* const pRoot; Trie() : pRoot(new Node()) {} void AddWord(const char str[],int len); // int FindPredix(const char str[],int len); void Fuck(const char str[],int len); void Release(const Node *p); }dic; void Trie::AddWord(const char str[],int len) { Node* ptr = pRoot; for(int i = 0;i < len;i++) { int nPos = str[i] - 'a'; if(ptr->pNext[nPos] == NULL) ptr->pNext[nPos] = new Node(); ptr->cnt++; ptr = ptr->pNext[nPos]; } ptr->cnt++; } void Trie::Release(const Node* p) { for(int i = 0;i < maxk;i++) if(p->pNext[i] != NULL) Release(p->pNext[i]); delete p; } void Trie::Fuck(const char str[],int len) { Node* ptr = pRoot; for(int i = 0;i < len;i++) { int nPos = str[i] - 'a'; if(ptr->pNext[nPos] == NULL) return; if(ptr->cnt == 1) return; putchar(str[i]); ptr = ptr->pNext[nPos]; } } char buf[1005][maxl]; int main() { //FIN; int cnt = 0; while(~scanf("%s",buf[cnt])) dic.AddWord(buf[cnt],strlen(buf[cnt])),cnt++; for(int i = 0;i < cnt;i++) { printf("%s ",buf[i]); dic.Fuck(buf[i],strlen(buf[i])); puts(""); } dic.Release(dic.pRoot); return 0; }
相关文章推荐
- UESTC 169 快快告诉我时间
- STM32F4, USB HS with ULPI and Suspend/Wakeup
- TCP三次握手和四次挥手协议
- 51、如何提取android代码中的字符串为系统资源文件 (I18N)
- Java中快速排序的实现
- linux shell 管道命令(pipe)使用及与shell重定向区别
- unity, audio falloff
- Centreon升级 2.4.5 -> 2.6.0
- Oracle学习之DATAGUARD(九) 自动Failover
- 用Python抓取全站中的404错误
- cas客户端验证流程
- epoll
- Threejs创建及设置face
- UESTC 170 三国
- leetcode Compare Version Numbers版本号比较
- MapReduce初步理解及一些简单计算应用
- DirectX3D游戏开发一 第一个DirectX3D项目
- 使用DatagramSocket发送、接收数据(Socket之UDP套接字)
- Fraction to Recurring Decimal
- 优步UBER司机全国各地奖励政策汇总:北京、上海、广州、深圳、佛山、天津、南京、武汉、成都、重庆、济南、西安、宁波、青岛、长沙、苏州