Advertisement
artemgf

Бор

Feb 4th, 2018
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stdio.h>
  9. #include <cmath>
  10. #include <math.h>
  11. #include <queue>
  12. #include <stack>
  13. #include <climits>
  14. #include <deque>
  15. #include <ctime>
  16.  
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. typedef unsigned int ui;
  22.  
  23. #define mh() make_heap()
  24. #define poph() pop_heap()
  25. #define pushh() push_heap()
  26. #define sor(n) n.begin(), n.end()
  27. #define rsor(n) n.rbegin(), n.rend()
  28. #define mp make_pair
  29. #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
  30. #define p(T) pair<T,T>
  31. #define znac(l) abs(l)/l
  32. const ll ok = ll(1e9 + 7);
  33.  
  34. map<string, ll>z;
  35. vector<ll>answ;
  36. struct top
  37. {
  38.     top* next[11];
  39.     short num;
  40.     ll sc;
  41.     ll end;
  42. };
  43.  
  44. top* newtop(short num)
  45. {
  46.     top* netop = new top();
  47.     for (int i = 0; i < 11; i++)
  48.         netop->next[i] = NULL;
  49.     netop->num = num;
  50.     netop->sc = 0;
  51.     netop->end = 0;
  52.     return netop;
  53. }
  54.  
  55. void addbor(string s, top* cor)
  56. {
  57.     for (int i = 0; i < s.size(); i++)
  58.     {
  59.         ll pos = s[i]-'0';
  60.         if (s[i] == '?')
  61.             pos = 10;
  62.         if (cor->next[pos] != NULL)
  63.             cor = cor->next[pos];
  64.         else
  65.         {
  66.             cor->next[pos] = newtop(pos);
  67.             cor = cor->next[pos];
  68.         }
  69.         if (i == s.size() - 1)
  70.             cor->end++;
  71.     }
  72. }
  73.  
  74. void check(string h, top* cor, ll i, ll ym)
  75. {
  76.     bool p = 1;
  77.     for (int j = 0; j < 11; j++)
  78.     {
  79.         if (cor->next[j] != NULL)
  80.         {
  81.             p = 0;
  82.             if (cor->next[j]->num < (h[i] - '0'))
  83.             {
  84.                 cor->next[j]->sc += 1 * ym;
  85.             }
  86.             else
  87.                 if (cor->next[j]->num == (h[i] - '0')||j==10)
  88.                 {
  89.                     if (j == 10)
  90.                     {
  91.                         ym *= (h[i] - '0') - 0 + 1;
  92.                     }
  93.                     check(h, cor->next[j], i + 1, ym);
  94.                 }
  95.                 else
  96.                 {
  97.                     cor->next[j]->sc += ym - 1;
  98.                 }
  99.         }
  100.     }
  101.     if (p)
  102.     {
  103.         if (cor->num == 10)
  104.         {
  105.             ym = (ym - 1) * 10 + cor->num+1;
  106.         }
  107.         cor->sc += ym;
  108.     }
  109. }
  110.  
  111. void eval(top* cor, ll sc, string s)
  112. {
  113.     bool p = 0;
  114.     if (cor->num != -1)
  115.         if (cor->num == 10)
  116.         {
  117.             s += '?';
  118.             sc *= 10;
  119.         }
  120.         else
  121.             s += cor->num + '0';
  122.     sc += cor->sc;
  123.     for (int i = 0; i < 11; i++)
  124.     {
  125.         if (cor->next[i] != NULL)
  126.         {
  127.             p = 1;
  128.             eval(cor->next[i], sc, s);
  129.         }
  130.     }
  131.     if (!p)
  132.     {
  133.         answ[z[s]] = sc;
  134.     }
  135. }
  136.  
  137. string addnul(string s)
  138. {
  139.     string y = "";
  140.     for (int i = 1; i <= 9 - s.size(); i++)
  141.     {
  142.         y += "0";
  143.     }
  144.     return y+s;
  145. }
  146.  
  147. int main()
  148. {
  149. #ifndef ONLINE_JUDGE
  150.     files;
  151. #endif
  152.     ll n, m;
  153.     cin >> n >> m;
  154.     string prom;
  155.     vector<string>in;
  156.     top* cor = newtop(-1);
  157.     for (int i = 1; i <= n; i++)
  158.     {
  159.         cin >> prom;
  160.         in.push_back(addnul(prom));
  161.     }
  162.    
  163.     for (int i = 1; i <= m; i++)
  164.     {
  165.         cin >> prom;
  166.         prom = addnul(prom);
  167.         addbor(prom,cor);
  168.         z[prom] = i - 1;
  169.         answ.push_back(0);
  170.     }
  171.     for (int i = 0; i < n; i++)
  172.     {
  173.         check(in[i], cor, 0, 1);
  174.     }
  175.     eval(cor, 0, "");
  176.     for (int i = 0; i < answ.size(); i++)
  177.     {
  178.         cout << answ[i] << endl;
  179.     }
  180.     //system("pause");
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement