Advertisement
filashkov

Untitled

Nov 22nd, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. // T06-15.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8.  
  9. using namespace std;
  10.  
  11. const int ALPHABET_SIZE = 28;
  12.  
  13. struct Trie
  14. {
  15.     struct Trie* children;
  16.     bool visited;
  17.     long long value;
  18. };
  19.  
  20. int index(char c)
  21. {
  22.     if (c == '-')
  23.     {
  24.         return 26;
  25.     }
  26.     if (c == '.')
  27.     {
  28.         return 27;
  29.     }
  30.     return c - 'a';
  31. }
  32.  
  33. int main(void)
  34. {
  35.     int n;
  36.     scanf("%d", &n);
  37.     struct Trie* root = NULL;
  38.     struct Trie* currentNode;
  39.     for (int i = 0; i < n; i++)
  40.     {
  41.         char s[101];
  42.         scanf("%s", s);
  43.         int string_length = strlen(s);
  44.         currentNode = root;
  45.         for (int j = 0; j < string_length; j++)
  46.         {
  47.             cout << "IF " << s[j] << endl;
  48.             if (currentNode == NULL)
  49.             {
  50.                 currentNode->children = (struct Trie*)malloc(ALPHABET_SIZE * sizeof(struct Trie));
  51.                 currentNode = currentNode->children + index(s[j]);
  52.                 cout << "YES " << s[j] << endl;
  53.                 currentNode->value = -1;
  54.             }
  55.             else
  56.             {
  57.                 cout << "What?" << endl;
  58.             }
  59.         }
  60.         long long current_value;
  61.         scanf("%lld", &current_value);
  62.         currentNode->value = current_value;
  63.     }
  64.     int m;
  65.     scanf("%d", &m);
  66.     for (int i = 0; i < m; i++)
  67.     {
  68.         bool p = false;
  69.         char s[101];
  70.         scanf("%s", s);
  71.         int n = strlen(s);
  72.         currentNode = root;
  73.         for (int j = 0; j < n; j++)
  74.         {
  75.             currentNode = currentNode->children + index(s[j]);
  76.             if (currentNode == NULL)
  77.             {
  78.                 p = true;
  79.                 break;
  80.             }
  81.         }
  82.         if (p)
  83.         {
  84.             printf("-1\n");
  85.             continue;
  86.         }
  87.         printf("%lld\n", currentNode->value);
  88.     }
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement