#include <iostream>
int *a[21];
struct ll{
int v;
ll *n;
};
ll r[1000000];
struct trie{
trie *next[3];
ll *res;
};
trie seq[20000000], *nxt = seq;
int b[1000000];
void dfs(trie *p, int d, int s){
if(s == 1)
while(p->res)
b[p->res->v] = a[d][0],
p->res = p->res->n;
else{
if(p->next[0]){
for(int i=0; i<s/2; ++i)
a[d+1][i] = a[d][i];
dfs(p->next[0], d+1, s/2);
}
if(p->next[1]){
for(int i=0; i<s/2; ++i)
a[d+1][i] = a[d][i+s/2];
dfs(p->next[1], d+1, s/2);
}
if(p->next[2]){
for(int i=0; i<s/2; ++i)
a[d+1][i] = a[d][i+s/2];
for(int i=0; i<s/2; ++i)
a[d+1][i] += a[d][i];
dfs(p->next[2], d+1, s/2);
}
}
}
int main(){
std::cin.tie(0) -> sync_with_stdio(0);
int l, q; std::cin >> l >> q;
for(int i=0; i<=l; ++i)
a[i] = new int[1<<(l-i)];
for(int i=0; i<1<<l; ++i){
char c; std::cin >> c;
a[0][i] = c-'0';
}
for(int i=0; i<q; ++i){
trie *p = seq;
for(int j=0; j<l; ++j){
char c; std::cin >> c;
int r = c == '?' ? 2 : c - '0';
if(!p->next[r]) p->next[r] = ++nxt;
p = p->next[r];
}
r[i].v = i;
r[i].n = p->res;
p->res = r+i;
}
dfs(seq, 0, 1<<l);
for(int i=0; i<q; ++i) std::cout << b[i] << " \n"[i==q-1];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |