POJ 2001-Shortest Prefixes【字典树】

快来打我* 2022-07-24 07:03 225阅读 0赞

Shortest Prefixes














Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16880   Accepted: 7327

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

解题思路:

输入完上面的所有字符后我们要 ctrl+z,输出每个字符的最长序列,只要这里面又一次出现就算。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<queue>
  5. using namespace std;
  6. typedef struct node
  7. {
  8. int cnt;
  9. node *next[26];
  10. }Trie;
  11. Trie root;
  12. void ct(char *str)
  13. {
  14. int len,i,j;
  15. len=strlen(str);
  16. Trie *p=&root,*q;
  17. for(i=0;i<len;i++)
  18. {
  19. int id=str[i]-'a';
  20. if(p->next[id]==NULL)
  21. {
  22. q=new Trie;
  23. q->cnt=1;
  24. for(j=0;j<26;j++)
  25. q->next[j]=NULL;
  26. p->next[id]=q;
  27. p=p->next[id];
  28. }
  29. else
  30. {
  31. p->next[id]->cnt++;
  32. p=p->next[id];
  33. }
  34. }
  35. }
  36. void Find_Trie(char*str)
  37. {
  38. int len,i,j,id;
  39. len=strlen(str);
  40. Trie*p=&root;
  41. for( i=0; i<len; i++){
  42. id=str[i]-'a';
  43. if( p->next[id]==NULL)
  44. return ;
  45. if(p->next[id]->cnt==1)
  46. {
  47. printf("%c",str[i]);
  48. return ;
  49. }
  50. else if(p->next[id]->cnt>1)
  51. {
  52. printf("%c",str[i]);
  53. p=p->next[id];
  54. }
  55. }
  56. return ;
  57. }
  58. char xx[1050][25];
  59. int main()
  60. {
  61. char map[1050];
  62. int ii=0;
  63. while(scanf("%s",map)!=EOF)
  64. {
  65. ct(map);
  66. strcpy(xx[ii],map);
  67. ii++;
  68. }
  69. for(int j=0;j<ii;j++)
  70. {
  71. printf("%s ",xx[j]);
  72. Find_Trie(xx[j]);
  73. printf("\n");
  74. }
  75. return 0;
  76. }

发表评论

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

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

相关阅读