// incorrect/felix-hasucs-bothsides-pq.cpp
#include "hieroglyphs.h"
#include<bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
//erases non-common elements
void clean(vi& a, vi& b) {
vi ap;
vi bp;
set<int> as;
set<int> bs;
for (int x : a) as.insert(x);
for (int x : b) bs.insert(x);
for (int x : a) if (bs.count(x)) ap.push_back(x);
for (int x : b) if (as.count(x)) bp.push_back(x);
swap(a, ap);
swap(b, bp);
}
map<int, int> coordinate_compress(vi& a, vi& b) {
int cc = 0;
map<int, int> mp;
map<int, int> rmp;
for (int& x : a) {
if (!mp.count(x)) {
mp[x] = cc++;
rmp[mp[x]] = x;
}
x = mp[x];
}
for (int& x : b) {
if (!mp.count(x)) {
mp[x] = cc++;
rmp[mp[x]] = x;
}
x = mp[x];
}
return rmp;
}
bool is_subsequence(const vi& a, const vi& b) {
int j = 0;
for (int x : a) {
if (j < (int)b.size() && b[j] == x) {
j++;
}
}
return j == (int)b.size();
}
vector<int> get_candidate(vector<int> a, vector<int> b) {
int n = a.size();
int m = b.size();
vi occ_a(max(n, m)+1, 0);
vi occ_b(max(n, m)+1, 0);
for (int i=0; i < n; ++i) {
occ_a[a[i]]++;
}
for (int i=0; i < m; ++i) {
occ_b[b[i]]++;
}
vi c;
queue<int> qa;
queue<int> qb;
for (int i=0; i < n; ++i) {
if (occ_a[a[i]] <= occ_b[a[i]]) {
qa.push(i);
}
}
for (int i=0; i < m; ++i) {
if (occ_a[b[i]] > occ_b[b[i]]) {
qb.push(i);
}
}
int i_a_curr = 0;
int i_b_curr = 0;
int i_a_next = 0;
int i_b_next = 0;
vi occ_a_curr = vi(occ_a);
vi occ_a_next = vi(occ_a);
vi occ_b_curr = vi(occ_b);
vi occ_b_next = vi(occ_b);
while(!qa.empty() && !qb.empty()) {
while(i_a_next < qa.front()) {
occ_a_next[a[i_a_next]]--;
i_a_next++;
}
while(i_b_next < qb.front()) {
occ_b_next[b[i_b_next]]--;
i_b_next++;
}
int x = a[i_a_next];
int y = b[i_b_next];
int occ_x = occ_a_next[x];
int occ_y = occ_b_next[y];
bool a_good = (occ_a_next[y] >= occ_y && occ_b_curr[x] > occ_b_next[x]);
bool b_good = (occ_b_next[x] >= occ_x && occ_a_curr[y] > occ_a_next[y]);
if (a_good && b_good) return vi();
if (!a_good && !b_good) return vi();
if(a_good) {
c.push_back(x);
qa.pop();
while(i_a_curr <= i_a_next) {
occ_a_curr[a[i_a_curr]]--;
i_a_curr++;
}
while(b[i_b_curr] != x) {
occ_b_curr[b[i_b_curr]]--;
i_b_curr++;
}
occ_b_curr[b[i_b_curr]]--;
i_b_curr++;
}
else {
c.push_back(y);
qb.pop();
while(i_b_curr <= i_b_next) {
occ_b_curr[b[i_b_curr]]--;
i_b_curr++;
}
while(a[i_a_curr] != y) {
occ_a_curr[a[i_a_curr]]--;
i_a_curr++;
}
occ_a_curr[a[i_a_curr]]--;
i_a_curr++;
}
}
while(!qa.empty()) {
c.push_back(a[qa.front()]);
qa.pop();
}
while(!qb.empty()) {
c.push_back(b[qb.front()]);
qb.pop();
}
return ((is_subsequence(a, c) && is_subsequence(b, c)) ? c : vi());
}
vi lcs(const vi& a, const vi& b) {
int n = a.size();
int m = b.size();
vvi dp(n+1, vi(m+1));
for (int i=1; i <= n; ++i) {
for (int j=1; j <= m; ++j) {
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
if (a[i-1] == b[j-1]) dp[i][j] = max(dp[i][j], dp[i-1][j-1]+1);
}
}
vi c;
int ci = n;
int cj = m;
while (ci > 0 && cj > 0) {
if (a[ci-1] == b[cj-1] && dp[ci][cj] == dp[ci-1][cj-1]+1) {
c.push_back(a[ci-1]);
ci--;
cj--;
}
else {
if (dp[ci][cj] == dp[ci-1][cj]) {
ci--;
}
else {
cj--;
}
}
}
reverse(c.begin(), c.end());
return c;
}
vector<int> ucs_quad(vi a, vi b) {
vi c = lcs(a, b);
int n = a.size();
int m = b.size();
int l = c.size();
vvi nxt(max(n, m)+1, vi(l+2, l)); //nxt[x][i] = first k such that c[k] = x and k >= i, l if such k does not exist
for (int i=0; i < l; ++i) {
for (int j=0; j <= i; ++j) {
nxt[c[i]][j] = min(nxt[c[i]][j], i);
}
}
vvi dp(n+1, vi(m+1, -1)); //dp[i][j] = maximum k so that there is a common subseq of a[0..i), b[0..j) which is not a subseq of c[0..k), -1 if no subseq
for (int i=1; i <= n; ++i) {
for (int j=1; j <= m; ++j) {
if (a[i-1] == b[j-1]) {
dp[i][j] = nxt[a[i-1]][dp[i-1][j-1]+1];
}
dp[i][j] = max(dp[i][j], max(dp[i-1][j], dp[i][j-1]));
}
}
if(dp[n][m] == l) return vector<int>({-1});
return c;
}
vector<int> ucs(vector<int> a, vector<int> b) {
clean(a, b);
if (a.empty() || b.empty()) {
return vector<int>();
}
map<int, int> mp = coordinate_compress(a, b);
vi c;
if ((int)a.size() <= 500 && (int)b.size() <= 500) c = ucs_quad(a, b);
else {
c = get_candidate(a, b);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
vi rc = get_candidate(a, b);
reverse(rc.begin(), rc.end());
if (c != rc) return {-1};
}
if (c.empty() || c[0] == -1) return {-1};
for (int& x : c) x = mp[x];
return c;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
268 ms |
16064 KB |
Output is correct |
6 |
Correct |
238 ms |
15780 KB |
Output is correct |
7 |
Correct |
246 ms |
15764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
268 ms |
16064 KB |
Output is correct |
6 |
Correct |
238 ms |
15780 KB |
Output is correct |
7 |
Correct |
246 ms |
15764 KB |
Output is correct |
8 |
Correct |
157 ms |
11188 KB |
Output is correct |
9 |
Correct |
164 ms |
11192 KB |
Output is correct |
10 |
Correct |
160 ms |
11092 KB |
Output is correct |
11 |
Correct |
156 ms |
11424 KB |
Output is correct |
12 |
Correct |
157 ms |
11160 KB |
Output is correct |
13 |
Correct |
164 ms |
11156 KB |
Output is correct |
14 |
Correct |
161 ms |
11160 KB |
Output is correct |
15 |
Correct |
159 ms |
11160 KB |
Output is correct |
16 |
Correct |
163 ms |
11156 KB |
Output is correct |
17 |
Correct |
159 ms |
11076 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
1640 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
72 ms |
6392 KB |
Output is correct |
23 |
Correct |
128 ms |
10416 KB |
Output is correct |
24 |
Correct |
125 ms |
10556 KB |
Output is correct |
25 |
Correct |
120 ms |
10532 KB |
Output is correct |
26 |
Correct |
78 ms |
6484 KB |
Output is correct |
27 |
Correct |
148 ms |
11160 KB |
Output is correct |
28 |
Correct |
152 ms |
11032 KB |
Output is correct |
29 |
Incorrect |
153 ms |
11156 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
6840 KB |
Output is correct |
2 |
Correct |
32 ms |
6772 KB |
Output is correct |
3 |
Correct |
24 ms |
5956 KB |
Output is correct |
4 |
Correct |
23 ms |
5740 KB |
Output is correct |
5 |
Correct |
27 ms |
6480 KB |
Output is correct |
6 |
Correct |
12 ms |
4236 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
28 ms |
6668 KB |
Output is correct |
17 |
Correct |
19 ms |
5956 KB |
Output is correct |
18 |
Correct |
18 ms |
5040 KB |
Output is correct |
19 |
Correct |
14 ms |
4872 KB |
Output is correct |
20 |
Correct |
25 ms |
6068 KB |
Output is correct |
21 |
Correct |
17 ms |
5048 KB |
Output is correct |
22 |
Correct |
15 ms |
5544 KB |
Output is correct |
23 |
Incorrect |
24 ms |
6696 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
11188 KB |
Output is correct |
2 |
Correct |
164 ms |
11192 KB |
Output is correct |
3 |
Correct |
160 ms |
11092 KB |
Output is correct |
4 |
Correct |
156 ms |
11424 KB |
Output is correct |
5 |
Correct |
157 ms |
11160 KB |
Output is correct |
6 |
Correct |
164 ms |
11156 KB |
Output is correct |
7 |
Correct |
161 ms |
11160 KB |
Output is correct |
8 |
Correct |
159 ms |
11160 KB |
Output is correct |
9 |
Correct |
163 ms |
11156 KB |
Output is correct |
10 |
Correct |
159 ms |
11076 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
1640 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
72 ms |
6392 KB |
Output is correct |
16 |
Correct |
128 ms |
10416 KB |
Output is correct |
17 |
Correct |
125 ms |
10556 KB |
Output is correct |
18 |
Correct |
120 ms |
10532 KB |
Output is correct |
19 |
Correct |
78 ms |
6484 KB |
Output is correct |
20 |
Correct |
28 ms |
6840 KB |
Output is correct |
21 |
Correct |
32 ms |
6772 KB |
Output is correct |
22 |
Correct |
24 ms |
5956 KB |
Output is correct |
23 |
Correct |
23 ms |
5740 KB |
Output is correct |
24 |
Correct |
27 ms |
6480 KB |
Output is correct |
25 |
Correct |
12 ms |
4236 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
7 ms |
2112 KB |
Output is correct |
35 |
Correct |
28 ms |
6488 KB |
Output is correct |
36 |
Correct |
47 ms |
7052 KB |
Output is correct |
37 |
Correct |
32 ms |
6596 KB |
Output is correct |
38 |
Correct |
33 ms |
6596 KB |
Output is correct |
39 |
Correct |
30 ms |
6724 KB |
Output is correct |
40 |
Correct |
126 ms |
9860 KB |
Output is correct |
41 |
Correct |
81 ms |
7572 KB |
Output is correct |
42 |
Correct |
36 ms |
4996 KB |
Output is correct |
43 |
Correct |
24 ms |
5096 KB |
Output is correct |
44 |
Correct |
32 ms |
5120 KB |
Output is correct |
45 |
Correct |
25 ms |
4636 KB |
Output is correct |
46 |
Correct |
123 ms |
8656 KB |
Output is correct |
47 |
Correct |
109 ms |
8620 KB |
Output is correct |
48 |
Correct |
107 ms |
8664 KB |
Output is correct |
49 |
Correct |
138 ms |
8320 KB |
Output is correct |
50 |
Correct |
113 ms |
8396 KB |
Output is correct |
51 |
Correct |
100 ms |
8352 KB |
Output is correct |
52 |
Correct |
113 ms |
7852 KB |
Output is correct |
53 |
Correct |
106 ms |
7948 KB |
Output is correct |
54 |
Correct |
98 ms |
7820 KB |
Output is correct |
55 |
Correct |
18 ms |
2480 KB |
Output is correct |
56 |
Correct |
40 ms |
6636 KB |
Output is correct |
57 |
Correct |
40 ms |
6064 KB |
Output is correct |
58 |
Correct |
47 ms |
6816 KB |
Output is correct |
59 |
Correct |
75 ms |
7092 KB |
Output is correct |
60 |
Correct |
111 ms |
7652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
268 ms |
16064 KB |
Output is correct |
9 |
Correct |
238 ms |
15780 KB |
Output is correct |
10 |
Correct |
246 ms |
15764 KB |
Output is correct |
11 |
Correct |
157 ms |
11188 KB |
Output is correct |
12 |
Correct |
164 ms |
11192 KB |
Output is correct |
13 |
Correct |
160 ms |
11092 KB |
Output is correct |
14 |
Correct |
156 ms |
11424 KB |
Output is correct |
15 |
Correct |
157 ms |
11160 KB |
Output is correct |
16 |
Correct |
164 ms |
11156 KB |
Output is correct |
17 |
Correct |
161 ms |
11160 KB |
Output is correct |
18 |
Correct |
159 ms |
11160 KB |
Output is correct |
19 |
Correct |
163 ms |
11156 KB |
Output is correct |
20 |
Correct |
159 ms |
11076 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
1640 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
72 ms |
6392 KB |
Output is correct |
26 |
Correct |
128 ms |
10416 KB |
Output is correct |
27 |
Correct |
125 ms |
10556 KB |
Output is correct |
28 |
Correct |
120 ms |
10532 KB |
Output is correct |
29 |
Correct |
78 ms |
6484 KB |
Output is correct |
30 |
Correct |
148 ms |
11160 KB |
Output is correct |
31 |
Correct |
152 ms |
11032 KB |
Output is correct |
32 |
Incorrect |
153 ms |
11156 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |