poj 2001 Shortest Prefixes(字典树)

约定不等于承诺〃 2021-10-24 03:30 335阅读 0赞

题目链接:http://poj.org/problem?

id=2001

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

  1. carbohydrate
  2. cart
  3. carburetor
  4. caramel
  5. caribou
  6. carbonic
  7. cartilage
  8. carbon
  9. carriage
  10. carton
  11. car
  12. carbonate

Sample Output

  1. carbohydrate carboh
  2. cart cart
  3. carburetor carbu
  4. caramel cara
  5. caribou cari
  6. carbonic carboni
  7. cartilage carti
  8. carbon carbon
  9. carriage carr
  10. carton carto
  11. car car
  12. carbonate carbona

Source

Rocky Mountain 2004

题意:

寻找在单词表中能唯一作为此单词的缩写!

代码例如以下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <malloc.h>
  4. #include <iostream>
  5. using namespace std;
  6. #define MAXN 26
  7. char str[1017][MAXN];
  8. typedef struct Trie
  9. {
  10. int v;//依据须要变化
  11. Trie *next[MAXN];
  12. //next是表示每层有多少种类的数,假设仅仅是小写字母。则26就可以。
  13. //若改为大写和小写字母。则是52。若再加上数字。则是62了
  14. } Trie;
  15. Trie* root;
  16. void createTrie(char *str)
  17. {
  18. int len = strlen(str);
  19. Trie *p = root, *q;
  20. for(int i = 0; i < len; i++)
  21. {
  22. int id = str[i]-'a';
  23. if(p->next[id] == NULL)
  24. {
  25. q = (Trie *)malloc(sizeof(Trie));
  26. q->v = 1;//初始v==1
  27. for(int j = 0; j < MAXN; j++)
  28. q->next[j] = NULL;
  29. p->next[id] = q;
  30. p = p->next[id];
  31. }
  32. else
  33. {
  34. p->next[id]->v++;
  35. p = p->next[id];
  36. }
  37. }
  38. // p->v = -1;//若为结尾,则将v改成-1表示
  39. }
  40. void findTrie(char *str)
  41. {
  42. char ss[MAXN];
  43. int len = strlen(str);
  44. Trie *p = root;
  45. for(int i = 0; i < len; i++)
  46. {
  47. int id = str[i]-'a';
  48. p = p->next[id];
  49. ss[i] = str[i];
  50. ss[i+1] = '\0';
  51. if(p->v == 1) //能够唯一标示该字符串的前缀
  52. {
  53. printf("%s %s\n",str,ss);
  54. return ;
  55. }
  56. // return 0;
  57. // if(p->v == -1) //字符集中已有串是此串的前缀
  58. // return -1;
  59. }
  60. //return p->v;
  61. // return -1; //此串是字符集中某串的前缀
  62. printf("%s %s\n",str,str);
  63. // return;
  64. }
  65. int main()
  66. {
  67. int cont = 0;
  68. root = (Trie *)malloc(sizeof(Trie));
  69. for(int i = 0; i < MAXN; i++)
  70. root->next[i] = NULL;
  71. while(scanf("%s",str[cont])!=EOF)
  72. {
  73. createTrie(str[cont]);
  74. cont++;
  75. }
  76. for(int i = 0; i < cont; i++)
  77. {
  78. findTrie(str[i]);
  79. }
  80. return 0;
  81. }

转载于:https://www.cnblogs.com/wzzkaifa/p/6979306.html

发表评论

表情:
评论列表 (有 0 条评论,335人围观)

还没有评论,来说两句吧...

相关阅读