Advertisement
Guest User

kopkpok

a guest
Oct 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. // uloha4-2.c -- Daniel Kavuliak, 21.9.2018 14:27
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct Node
  7. {
  8.   struct Node* children[26];
  9. }NODE;
  10.  
  11. int findPrefix(NODE** current, char* word, int n)
  12. {
  13.   int i = word[0] - 'A';
  14.  
  15.   if (word[0] == '\0')
  16.     return n;
  17.  
  18.   if ((*current)->children[i] == NULL)
  19.   {
  20.     (*current)->children[i] = (NODE*)calloc(1, sizeof(NODE));
  21.     return findPrefix(&((*current)->children[i]), word + 1, n);
  22.   }
  23.   else
  24.     return findPrefix(&((*current)->children[i]), word + 1, n + 1);
  25. }
  26.  
  27. int main()
  28. {
  29.   // sem napis svoje riesenie
  30.   int longest = 0, tmp;
  31.   char buf[50];
  32.   NODE* root = (NODE*)calloc(1, sizeof(NODE));
  33.  
  34.   while (scanf("%s", buf) > 0)
  35.   {
  36.     tmp = findPrefix(&root, buf, 0);
  37.     if (longest < tmp)
  38.       longest = tmp;
  39.   }
  40.  
  41.   printf("%d\n", longest);
  42.  
  43.   return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement