poj 2001Shortest Prefixes(字典树)
来源:互联网 发布:informix数据库客户端 编辑:程序博客网 时间:2024/04/25 14:19
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 19395 Accepted: 8417
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
Rocky Mountain 2004
[Submit] [Go Back] [Status] [Discuss]
【题目大意】
给你一些单词,让你求出他们最短的前缀,当然,这个前缀不能有歧义,例如给出单词 carton cart car
carton的 前缀 就不能是cart,因为cart的前缀是cart,同理cart的前缀也不能是car。
要找到每个单词独一无二且是最短的前缀,car的前缀不能是,”c“ “ca” ,因为他们在别的单词中也有出现,
如果找不到独一无二,那个这个单词本身就是他自己的前缀。
- 思路:trie建树,设置一个变量num,计算每个单词出现的次数。
- 查找的时候如果num = 1则说明这是第一次出现过的单词,直接返回。
#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <string.h>#include <map>#include <set>#include <queue>#include <deque>#include <list>#include <bitset>#include <stack>#include <stdlib.h>#define lowbit(x) (x&-x)#define e exp(1.0)#define eps 1e-8//ios::sync_with_stdio(false);// auto start = clock();// cout << (clock() - start) / (double)CLOCKS_PER_SEC<<endl;typedef long long ll;typedef long long LL;using namespace std;typedef unsigned long long ull;const int maxn=1050;struct Trie{ int num; int nxt[30];}trie[maxn*30];int a,n;char s[maxn][30];void insert(char s[]){ int p=1; for(int i=0;s[i];i++) { if(trie[p].nxt[s[i]-'a']==0) { trie[p].nxt[s[i]-'a']=++a; trie[a].num=0; } p=trie[p].nxt[s[i]-'a']; trie[p].num++; }}void search(char s[]){ int p=1; for(int i=0;s[i];i++) { if(trie[p].num==1) return ; cout<<s[i]; p=trie[p].nxt[s[i]-'a']; }}int main(){ ios::sync_with_stdio(false); n=0; a=1; while(cin>>s[n]) insert(s[n++]); for(int i=0;i<n;i++) { cout<<s[i]<<' '; search(s[i]); cout<<endl; } 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(字典树)
- 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 2001Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes ( 字典树 )
- POJ 2001 Shortest Prefixes 字典树Trie
- 联合体
- # hadoop入门第六篇-Hive实例
- 微信小程序登陆及登陆状态保持前后端php代码 缓存redis
- js 验证各种格式类型的正则表达式
- zephyr--堆内存池(heap memory pool)
- poj 2001Shortest Prefixes(字典树)
- Dubbo快速入门 一
- c++常见内存错误
- 字典树(链式+数组模拟)--最基础的算法,最详细的注释
- VirtualBox 安装
- 287. Find the Duplicate Number
- 16:三角形判断
- Oracle的merge into使用小结
- QT入门安装篇+helloworld(辛酸血泪)