Shortest Prefixes 字典树 前缀树 入门题 题解

Shortest Prefixes 题解 (1)题目(2)题解
(1)题目 Shortest Prefixes
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
(2)题解
这道题的题意是 输出最短的前缀唯一地(没有歧义)标识这个单词。字典树的入门题,只需要在原来的模板上改动一些即可。解题思路是 输出在字典中有相同的前缀+1个字符来唯一识别这个单词。
字典树(前缀树)的模板:
https://blog.csdn.net/u011787119/article/details/46991691
代码如下:
#include#include#includeusing namespace std;const int maxn = 26;char s[1010][30];struct Trie{Trie *Next[maxn];int cnt;Trie(){cnt = 1;memset(Next, NULL, sizeof(Next));}}*root;void insert(char *str){int len = strlen(str);Trie *p = root, *q;for (int i = 0; i Next[id] == NULL){q = new Trie();p->Next[id] = q;p = p->Next[id];}else{p=p->Next[id];(p->cnt)++;}}}void find(char *str){int len = strlen(str);Trie *p = root;for (int i = 0; i Next[id];if (p->cnt > 1) ///输出这些字符串相同的前缀{printf(\”%c\”, str[i]);}else ///当p->cnt==1时,多输出一个字符来唯一的标识这个字符串(本题的关键) {printf(\”%c\”, str[i]);return;}}}int main(){root = new Trie();int n = 0; //模仿别人的博客来进行输入while (scanf(\”%s\”, s[n])!=EOF){insert(s[n]);n++;}for (int i = 0; i
这道题参考的这篇博客
链接: https://blog.csdn.net/caduca/article/details/43531875.
《Shortest Prefixes 字典树 前缀树 入门题 题解》来自互联网同行内容,若有侵权,请联系我们删除!
科技资讯SEO上一篇 : 如何成为秀色第一坊的常客?
下一篇 : 如何在PPTV上观看CCTV直播?
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!