// incorrect/felix-hasucs-plus-quadratic.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);
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 |
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 |
# |
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 |
229 ms |
15608 KB |
Output is correct |
6 |
Correct |
217 ms |
15776 KB |
Output is correct |
7 |
Correct |
206 ms |
15676 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 |
229 ms |
15608 KB |
Output is correct |
6 |
Correct |
217 ms |
15776 KB |
Output is correct |
7 |
Correct |
206 ms |
15676 KB |
Output is correct |
8 |
Correct |
154 ms |
10900 KB |
Output is correct |
9 |
Correct |
156 ms |
10860 KB |
Output is correct |
10 |
Correct |
172 ms |
10924 KB |
Output is correct |
11 |
Correct |
161 ms |
10928 KB |
Output is correct |
12 |
Correct |
152 ms |
10880 KB |
Output is correct |
13 |
Correct |
147 ms |
10796 KB |
Output is correct |
14 |
Correct |
152 ms |
10876 KB |
Output is correct |
15 |
Correct |
181 ms |
10900 KB |
Output is correct |
16 |
Correct |
177 ms |
10844 KB |
Output is correct |
17 |
Correct |
169 ms |
10840 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
1388 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
81 ms |
6328 KB |
Output is correct |
23 |
Correct |
134 ms |
10300 KB |
Output is correct |
24 |
Correct |
154 ms |
10260 KB |
Output is correct |
25 |
Correct |
133 ms |
10220 KB |
Output is correct |
26 |
Correct |
88 ms |
6480 KB |
Output is correct |
27 |
Correct |
173 ms |
10916 KB |
Output is correct |
28 |
Correct |
172 ms |
10920 KB |
Output is correct |
29 |
Incorrect |
185 ms |
10884 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
6212 KB |
Output is correct |
2 |
Correct |
34 ms |
6240 KB |
Output is correct |
3 |
Correct |
25 ms |
5700 KB |
Output is correct |
4 |
Correct |
22 ms |
5484 KB |
Output is correct |
5 |
Correct |
27 ms |
6252 KB |
Output is correct |
6 |
Correct |
14 ms |
4240 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
47 ms |
6204 KB |
Output is correct |
17 |
Correct |
19 ms |
5556 KB |
Output is correct |
18 |
Correct |
18 ms |
5128 KB |
Output is correct |
19 |
Correct |
14 ms |
4872 KB |
Output is correct |
20 |
Correct |
25 ms |
5788 KB |
Output is correct |
21 |
Correct |
16 ms |
5048 KB |
Output is correct |
22 |
Correct |
15 ms |
5444 KB |
Output is correct |
23 |
Incorrect |
24 ms |
6324 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
10900 KB |
Output is correct |
2 |
Correct |
156 ms |
10860 KB |
Output is correct |
3 |
Correct |
172 ms |
10924 KB |
Output is correct |
4 |
Correct |
161 ms |
10928 KB |
Output is correct |
5 |
Correct |
152 ms |
10880 KB |
Output is correct |
6 |
Correct |
147 ms |
10796 KB |
Output is correct |
7 |
Correct |
152 ms |
10876 KB |
Output is correct |
8 |
Correct |
181 ms |
10900 KB |
Output is correct |
9 |
Correct |
177 ms |
10844 KB |
Output is correct |
10 |
Correct |
169 ms |
10840 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
1388 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
81 ms |
6328 KB |
Output is correct |
16 |
Correct |
134 ms |
10300 KB |
Output is correct |
17 |
Correct |
154 ms |
10260 KB |
Output is correct |
18 |
Correct |
133 ms |
10220 KB |
Output is correct |
19 |
Correct |
88 ms |
6480 KB |
Output is correct |
20 |
Correct |
29 ms |
6212 KB |
Output is correct |
21 |
Correct |
34 ms |
6240 KB |
Output is correct |
22 |
Correct |
25 ms |
5700 KB |
Output is correct |
23 |
Correct |
22 ms |
5484 KB |
Output is correct |
24 |
Correct |
27 ms |
6252 KB |
Output is correct |
25 |
Correct |
14 ms |
4240 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
584 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
3 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
604 KB |
Output is correct |
34 |
Correct |
13 ms |
1884 KB |
Output is correct |
35 |
Correct |
26 ms |
6004 KB |
Output is correct |
36 |
Correct |
42 ms |
6728 KB |
Output is correct |
37 |
Correct |
58 ms |
6464 KB |
Output is correct |
38 |
Correct |
28 ms |
6216 KB |
Output is correct |
39 |
Correct |
28 ms |
6212 KB |
Output is correct |
40 |
Correct |
188 ms |
9496 KB |
Output is correct |
41 |
Correct |
79 ms |
7320 KB |
Output is correct |
42 |
Correct |
20 ms |
4928 KB |
Output is correct |
43 |
Correct |
39 ms |
5032 KB |
Output is correct |
44 |
Correct |
21 ms |
4932 KB |
Output is correct |
45 |
Correct |
20 ms |
4548 KB |
Output is correct |
46 |
Correct |
109 ms |
8656 KB |
Output is correct |
47 |
Correct |
109 ms |
8408 KB |
Output is correct |
48 |
Correct |
113 ms |
8404 KB |
Output is correct |
49 |
Correct |
129 ms |
8356 KB |
Output is correct |
50 |
Correct |
120 ms |
8320 KB |
Output is correct |
51 |
Correct |
115 ms |
8280 KB |
Output is correct |
52 |
Correct |
118 ms |
7596 KB |
Output is correct |
53 |
Correct |
110 ms |
7820 KB |
Output is correct |
54 |
Correct |
100 ms |
7604 KB |
Output is correct |
55 |
Correct |
16 ms |
2384 KB |
Output is correct |
56 |
Correct |
39 ms |
6376 KB |
Output is correct |
57 |
Correct |
42 ms |
5776 KB |
Output is correct |
58 |
Correct |
48 ms |
6436 KB |
Output is correct |
59 |
Correct |
81 ms |
6720 KB |
Output is correct |
60 |
Correct |
112 ms |
7232 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
584 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
604 KB |
Output is correct |
18 |
Correct |
3 ms |
652 KB |
Output is correct |
19 |
Correct |
2 ms |
572 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
752 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 |
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 |
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 |
229 ms |
15608 KB |
Output is correct |
9 |
Correct |
217 ms |
15776 KB |
Output is correct |
10 |
Correct |
206 ms |
15676 KB |
Output is correct |
11 |
Correct |
154 ms |
10900 KB |
Output is correct |
12 |
Correct |
156 ms |
10860 KB |
Output is correct |
13 |
Correct |
172 ms |
10924 KB |
Output is correct |
14 |
Correct |
161 ms |
10928 KB |
Output is correct |
15 |
Correct |
152 ms |
10880 KB |
Output is correct |
16 |
Correct |
147 ms |
10796 KB |
Output is correct |
17 |
Correct |
152 ms |
10876 KB |
Output is correct |
18 |
Correct |
181 ms |
10900 KB |
Output is correct |
19 |
Correct |
177 ms |
10844 KB |
Output is correct |
20 |
Correct |
169 ms |
10840 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
1388 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
81 ms |
6328 KB |
Output is correct |
26 |
Correct |
134 ms |
10300 KB |
Output is correct |
27 |
Correct |
154 ms |
10260 KB |
Output is correct |
28 |
Correct |
133 ms |
10220 KB |
Output is correct |
29 |
Correct |
88 ms |
6480 KB |
Output is correct |
30 |
Correct |
173 ms |
10916 KB |
Output is correct |
31 |
Correct |
172 ms |
10920 KB |
Output is correct |
32 |
Incorrect |
185 ms |
10884 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |