Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Trie{
  5.         int nr;
  6.         struct Trie *v[27];
  7. };
  8.  
  9. typedef struct Trie Trie;
  10.  
  11. void Init(Trie **t){
  12.         int i;
  13.         (*t) = (Trie*)malloc(sizeof(Trie));
  14.         (*t) -> nr = 0;
  15.         for(i = 0; i < 27; ++i){
  16.                 (*t) -> v[i] = 0;
  17.         }
  18. }
  19.  
  20. void Insert(Trie *t, char *s){
  21.         if(*s == '\0'){
  22.                 t -> nr++;
  23.         }
  24.         else{
  25.                 int poz = *s - 'a';
  26.                 if(t -> v[poz] == NULL){
  27.                         Init(&t -> v[poz]);
  28.                 }
  29.                 Insert(t -> v[poz], s + 1);
  30.         }
  31. }
  32.  
  33. int Query(Trie *t, char *s){
  34.         if(*s == '\0'){
  35.                 return t -> nr;
  36.         }
  37.         else{
  38.                 int poz = *s - 'a';
  39.                 if(t -> v[poz] != NULL){
  40.                         return Query(t -> v[poz], s + 1);
  41.                 }
  42.                 else{
  43.                         return 0;
  44.                 }
  45.         }
  46. }
  47.  
  48. int main()
  49. {
  50.         freopen("date.in", "r", stdin);
  51.         freopen("date.out", "w", stdout);
  52.  
  53.         int n, i;
  54.         char s[20];
  55.         Trie *root;
  56.         Init(&root);
  57.  
  58.         scanf("%d", &n);
  59.         for(i = 0; i < n; ++i){
  60.                 scanf("%s", s);
  61.                 Insert(root, s);
  62.         }
  63.  
  64.         scanf("%d", &n);
  65.         for(i = 0; i < n; ++i){
  66.                 scanf("%s", s);
  67.                 printf("%d\n", Query(root, s));
  68.         }
  69.         return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement