#include <bits/stdc++.h>
using namespace std;
const int L = 20;
using Node = map<int, int>;
string s;
int l;
Node seg[1 << (L + 2)];
class Trie {
public:
vector<Trie *> ptrs;
void construct() { ptrs = vector<Trie *>(3, nullptr); }
void add(int x, int l) {
if (l == 0) {
return;
}
int d = x % 3;
if (ptrs[d] == nullptr) {
ptrs[d] = (Trie *)calloc(sizeof(Trie), 1);
ptrs[d]->construct();
}
ptrs[d]->add(x / 3, l - 1);
}
bool _exists(int x, int l) {
if (l == 0) {
return true;
}
int d = x % 3;
if (ptrs[d] == nullptr) {
return false;
}
return ptrs[d]->_exists(x / 3, l - 1);
}
bool exists(int x, int l) {
string s;
for (int i = 0; i < l; ++i) {
s.push_back(x % 3 + '0');
x /= 3;
}
for (int i = 0; i < l; ++i) {
x = 3 * x + (s[i] - '0');
}
return _exists(x, l);
}
};
Trie *trie;
void construct(int v, int tl, int tr, int d) {
if (tl == tr) {
seg[v][0] = s[tl] - '0';
return;
}
int tm = (tl + tr) / 2;
construct(2 * v, tl, tm, d + 1);
construct(2 * v + 1, tm + 1, tr, d + 1);
for (auto &i : seg[2 * v]) {
if (trie->exists(3 * i.first, l - d)) {
seg[v][3 * i.first] = i.second;
}
if (trie->exists(3 * i.first + 2, l - d)) {
seg[v][3 * i.first + 2] = seg[2 * v][i.first] + seg[2 * v + 1][i.first];
}
}
for (auto &i : seg[2 * v + 1]) {
if (trie->exists(3 * i.first + 1, l - d)) {
seg[v][3 * i.first + 1] = i.second;
}
}
seg[2 * v].clear();
seg[2 * v + 1].clear();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin >> l >> q;
cin >> s;
vector<int> queries;
trie = (Trie *)calloc(sizeof(Trie), 1);
trie->construct();
while (q--) {
string sq;
cin >> sq;
int x = 0, y = 0;
for (char &c : sq) {
if (c == '?') {
c = '2';
}
}
for (int i = sq.length() - 1; i >= 0; --i) {
x = 3 * x + (sq[i] - '0');
}
for (int i = 0; i < sq.length(); ++i) {
y = 3 * y + (sq[i] - '0');
}
queries.push_back(x);
trie->add(y, l);
}
construct(1, 0, (1 << l) - 1, 0);
for (auto &i : queries) {
cout << seg[1][i] << "\n";
}
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:105:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int i = 0; i < sq.length(); ++i) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |