-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2564-substring-xor-queries.cpp
More file actions
36 lines (34 loc) · 1.12 KB
/
2564-substring-xor-queries.cpp
File metadata and controls
36 lines (34 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
vector<vector<int>> substringXorQueries(string s, vector<vector<int>>& queries) {
vector<vector<int>> ans;
int n = (int) s.size();
int q = (int) queries.size();
unordered_map<int, pair<int, int>> mp;
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
mp[0] = {i, i};
break;
}
}
for (int i = 0; i < n; i++) {
if (s[i] == '0') continue;
int cur = 0;
for (int j = i; j < min(n, i + 32); j++) {
cur <<= 1;
if (s[j] == '1') cur++;
if (mp.find(cur) != mp.end()) {
if (i < mp[cur].first) mp[cur] = {i, j};
} else {
mp[cur] = {i, j};
}
}
}
for (int i = 0; i < q; i++) {
int target = queries[i][0] ^ queries[i][1];
if (mp.find(target) == mp.end()) ans.push_back({-1, -1});
else ans.push_back({mp[target].first, mp[target].second});
}
return ans;
}
};