poj2001Shortest Prefixes【字典树找最短唯一前缀】
来源:互联网 发布:暗黑破坏神3攻略软件 编辑:程序博客网 时间:2024/04/30 13:38
Language:
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 16113 Accepted: 6950
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
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int maxn=500010;int node;int val[maxn];int word[maxn];int ch[maxn][30];char str[10010][50];void init(){node=1;memset(word,0,sizeof(word));memset(ch[0],0,sizeof(ch[0]));}int idx(char c){return c-'a';}void insert(char *s){int i,j,k,u=0;int len=strlen(s);for(i=0;i<len;++i){int c=idx(s[i]);if(!ch[u][c]){memset(ch[node],0,sizeof(ch[node]));val[node]=0;ch[u][c]=node++;}u=ch[u][c];word[u]++;}val[u]=1;}void findprefix(char *s){int i,u=0;int len=strlen(s);for(i=0;i<len;++i){int c=idx(s[i]);u=ch[u][c];if(word[u]==1)break;}s[i+1]='\0';printf("%s\n",s);}int main(){int t,i,j,k,n=0;init();while(scanf("%s",str[n++])!=EOF){insert(str[n-1]);}for(i=0;i<n;++i){printf("%s ",str[i]);findprefix(str[i]);}printf("\n");return 0;}
0 0
- poj2001Shortest Prefixes【字典树找最短唯一前缀】
- poj2001Shortest Prefixes(字典树)
- POJ2001Shortest Prefixes
- poj2001Shortest Prefixes
- POJ 2001 Shortest Prefixes 字典树经典题,求最短唯一前缀)
- Prefixes前缀
- poj 2001 Shortest Prefixes 【字典树】【找每一个字符串在字符串集里面的 最短且可唯一标识 的前缀】
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
- poj 2001 Shortest Prefixes 字典树找前缀
- POJ Shortest Prefixes -- 最短前缀问题 -- 字典树
- 找出能唯一标示一个字符串的最短前缀 Trie POJ 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes 数据结构Trie树(字典树、前缀树)
- Shortest Prefixes 【字典树 求给定字符串的 最短确定前缀】
- [经典面试题][字典树]字符串唯一前缀问题
- 字典树裸题 Shortest Prefixes
- Shortest Prefixes(字典树)
- POJ2001_Shortest Prefixes(字典树)
- POJ2001 - Shortest Prefixes - 字典树
- 第十二周项目三 图遍历算法实现
- SQL学习笔记(1)之DML语句
- “NDK integration is deprecated in the current plugin” Error
- SAP FI 新增BSEG可替代的字段 GGB1 表GB01 程序:RGUGBR00
- git忽略对已入库文件的修改
- poj2001Shortest Prefixes【字典树找最短唯一前缀】
- html引用百度地图API
- 【学习OpenCV】图像加噪
- 09-排序2 Insert or Merge
- 12到C++面试题(有趣)
- Zepto 的使用注意事项
- 关于window.opener无效问题
- dialect does not support sequences
- 相机假自动对焦添加