Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <string>
- #include <map>
- #include <set>
- #include <algorithm>
- #include <vector>
- #include <stdio.h>
- #include <cmath>
- #include <math.h>
- #include <queue>
- #include <stack>
- #include <climits>
- #include <deque>
- #include <ctime>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef unsigned int ui;
- #define mh() make_heap()
- #define poph() pop_heap()
- #define pushh() push_heap()
- #define sor(n) n.begin(), n.end()
- #define rsor(n) n.rbegin(), n.rend()
- #define mp make_pair
- #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
- #define p(T) pair<T,T>
- #define znac(l) abs(l)/l
- const ll ok = ll(1e9 + 7);
- map<string, ll>z;
- vector<ll>answ;
- struct top
- {
- top* next[11];
- short num;
- ll sc;
- ll end;
- };
- top* newtop(short num)
- {
- top* netop = new top();
- for (int i = 0; i < 11; i++)
- netop->next[i] = NULL;
- netop->num = num;
- netop->sc = 0;
- netop->end = 0;
- return netop;
- }
- void addbor(string s, top* cor)
- {
- for (int i = 0; i < s.size(); i++)
- {
- ll pos = s[i]-'0';
- if (s[i] == '?')
- pos = 10;
- if (cor->next[pos] != NULL)
- cor = cor->next[pos];
- else
- {
- cor->next[pos] = newtop(pos);
- cor = cor->next[pos];
- }
- if (i == s.size() - 1)
- cor->end++;
- }
- }
- void check(string h, top* cor, ll i, ll ym)
- {
- bool p = 1;
- for (int j = 0; j < 11; j++)
- {
- if (cor->next[j] != NULL)
- {
- p = 0;
- if (cor->next[j]->num < (h[i] - '0'))
- {
- cor->next[j]->sc += 1 * ym;
- }
- else
- if (cor->next[j]->num == (h[i] - '0')||j==10)
- {
- if (j == 10)
- {
- ym *= (h[i] - '0') - 0 + 1;
- }
- check(h, cor->next[j], i + 1, ym);
- }
- else
- {
- cor->next[j]->sc += ym - 1;
- }
- }
- }
- if (p)
- {
- if (cor->num == 10)
- {
- ym = (ym - 1) * 10 + cor->num+1;
- }
- cor->sc += ym;
- }
- }
- void eval(top* cor, ll sc, string s)
- {
- bool p = 0;
- if (cor->num != -1)
- if (cor->num == 10)
- {
- s += '?';
- sc *= 10;
- }
- else
- s += cor->num + '0';
- sc += cor->sc;
- for (int i = 0; i < 11; i++)
- {
- if (cor->next[i] != NULL)
- {
- p = 1;
- eval(cor->next[i], sc, s);
- }
- }
- if (!p)
- {
- answ[z[s]] = sc;
- }
- }
- string addnul(string s)
- {
- string y = "";
- for (int i = 1; i <= 9 - s.size(); i++)
- {
- y += "0";
- }
- return y+s;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- files;
- #endif
- ll n, m;
- cin >> n >> m;
- string prom;
- vector<string>in;
- top* cor = newtop(-1);
- for (int i = 1; i <= n; i++)
- {
- cin >> prom;
- in.push_back(addnul(prom));
- }
- for (int i = 1; i <= m; i++)
- {
- cin >> prom;
- prom = addnul(prom);
- addbor(prom,cor);
- z[prom] = i - 1;
- answ.push_back(0);
- }
- for (int i = 0; i < n; i++)
- {
- check(in[i], cor, 0, 1);
- }
- eval(cor, 0, "");
- for (int i = 0; i < answ.size(); i++)
- {
- cout << answ[i] << endl;
- }
- //system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement