Advertisement
Guest User

z1l8

a guest
Dec 5th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZE 26
  5.  
  6. struct Node;
  7. struct Node {
  8. int count;
  9. char symbol;
  10. struct Node *parent;
  11. struct Node *edges[SIZE];
  12. } Node;
  13.  
  14. void read_data(struct Node *root) {
  15. int c;
  16. struct Node *current = root;
  17. while ((c = getchar()) != EOF) {
  18. if (c == '\n') {
  19. current->count++;
  20. current = root;
  21. continue;
  22. }
  23. int idx = c - 'a';
  24. if (current->edges[idx] == NULL) {
  25. current->edges[idx] = (struct Node *)calloc(sizeof(Node), 1);
  26. current->edges[idx]->parent = current;
  27. current->edges[idx]->symbol = c;
  28. }
  29.  
  30. current = current->edges[idx];
  31. }
  32. }
  33.  
  34. static char word[1000000];
  35.  
  36. void travel(struct Node *node) {
  37. while (node->count--) {
  38. struct Node *parent = node->parent;
  39. int idx = 0;
  40.  
  41. while (parent != NULL) {
  42. word[idx++] = parent->symbol;
  43. parent = parent->parent;
  44. }
  45.  
  46. while (idx--)
  47. putchar(word[idx]);
  48.  
  49. printf("%c\n", node->symbol);
  50. }
  51.  
  52. for (int i = 0; i < SIZE; i++) {
  53. struct Node *edge = node->edges[i];
  54. if (edge != NULL)
  55. travel(edge);
  56. }
  57. }
  58.  
  59. int main(void) {
  60. struct Node *root = (struct Node *)calloc(sizeof(Node), 1);
  61. read_data(root);
  62. putchar('\n');
  63. travel(root);
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement