// incorrect/felix-radewoosh-56-eliminateprefix.cpp
//Mateusz Radecki, O(n+m)
#include "hieroglyphs.h"
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=1007*1000;
int n, m;
int ilea[nax];
int ileb[nax];
int potrzebuje[nax];
vi pozy[nax];
int potrzebujewa[nax];
int potrzebujewb[nax];
vi licz_harde(vi a, vi b)
{
n=a.size();
m=b.size();
for (int i=0; i<nax; i++)
ilea[i]=ileb[i]=0;
for (int i : a)
ilea[i]++;
for (int i : b)
ileb[i]++;
vector<pii> za, zb;
for (int i=0; i<n; i++)
if (ilea[a[i]]<=ileb[a[i]])
za.push_back({a[i], i});
for (int i=0; i<m; i++)
if (ilea[b[i]]>ileb[b[i]])
zb.push_back({b[i], i});
{
potrzebujewa[zb.size()]=n;
int wsk=zb.size();
for (int i=n-1; i>=0; i--)
{
if (wsk && zb[wsk-1].first==a[i])
{
wsk--;
potrzebujewa[wsk]=i;
}
}
}
{
potrzebujewb[za.size()]=m;
int wsk=za.size();
for (int i=m-1; i>=0; i--)
{
if (wsk && za[wsk-1].first==b[i])
{
wsk--;
potrzebujewb[wsk]=i;
}
}
}
vi ret;
int wska=0;
int wskb=0;
int gdz=0;
for (int i : a)
{
if (wska<(int)za.size() && i==za[wska].first)
{
while(gdz<m && b[gdz]!=i)
gdz++;
if (gdz==m)
return {-1};
gdz++;
ret.push_back(i);
wska++;
}
if (wskb<(int)zb.size() && i==zb[wskb].first && gdz<=zb[wskb].second && potrzebujewb[wska]>zb[wskb].second)
{
ret.push_back(i);
gdz=zb[wskb].second+1;
wskb++;
}
}
if ((int)ret.size()<(int)za.size()+(int)zb.size())
return {-1};
return ret;
}
vi licz_proste(vi a, vi b, int numwy)
{
n=a.size();
m=b.size();
for (int i=0; i<nax; i++)
ilea[i]=ileb[i]=0;
for (int i : a)
ilea[i]++;
for (int i : b)
ileb[i]++;
vector<pii> za;
for (int i=0; i<n; i++)
if (ilea[a[i]]<=ileb[a[i]]-numwy)
za.push_back({a[i], i});
{
potrzebuje[za.size()]=m;
int wsk=za.size();
for (int i=m-1; i>=0; i--)
{
if (wsk && za[wsk-1].first==b[i])
{
wsk--;
potrzebuje[wsk]=i;
}
}
if (wsk)
return {-1};
}
for (int i=0; i<nax; i++)
pozy[i].clear();
for (int i=m-1; i>=0; i--)
pozy[b[i]].push_back(i);
vi ret;
int wsk=0;
int gdz=0;
for (int i : a)
{
if (wsk<(int)za.size() && i==za[wsk].first)
{
while(gdz<m && b[gdz]!=i)
gdz++;
assert(gdz<m);
gdz++;
ret.push_back(i);
wsk++;
}
else
{
while(!pozy[i].empty() && pozy[i].back()<gdz)
pozy[i].pop_back();
if (!pozy[i].empty() && potrzebuje[wsk]>pozy[i].back())
{
ret.push_back(i);
gdz=pozy[i].back()+1;
}
}
}
return ret;
}
vi prosty(vi a, vi b)
{
n=a.size();
m=b.size();
for (int i=0; i<nax; i++)
pozy[i].clear();
for (int i=m-1; i>=0; i--)
pozy[b[i]].push_back(i);
int gdz=0;
vi ret;
for (int i : a)
{
while(!pozy[i].empty() && pozy[i].back()<gdz)
pozy[i].pop_back();
if (!pozy[i].empty())
{
ret.push_back(i);
gdz=pozy[i].back()+1;
}
}
return ret;
}
int zawiera(vi kto, vi kogo)
{
int wsk=0;
for (int i : kto)
if (wsk<(int)kogo.size() && i==kogo[wsk])
wsk++;
return wsk==(int)kogo.size();
}
vi _ucs(vi a, vi b)
{
vi odp=licz_harde(a, b);
vi raz=licz_proste(a, b, 0);
vi dwa=licz_proste(b, a, 1);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
vi trz=licz_proste(a, b, 0);
vi czt=licz_proste(b, a, 1);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
reverse(trz.begin(), trz.end());
reverse(czt.begin(), czt.end());
vi x=prosty(a, b);
vi y=prosty(b, a);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
vi z=prosty(a, b);
vi w=prosty(b, a);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
reverse(z.begin(), z.end());
reverse(w.begin(), w.end());
if (zawiera(odp, raz) && zawiera(odp, dwa) && zawiera(odp, trz) && zawiera(odp, czt) && zawiera(odp, x) && zawiera(odp, y) && zawiera(odp, z) && zawiera(odp, w))
return odp;
return {-1};
}
vi ucs(vi a, vi b) {
//return _ucs(a, b);
vi cpref;
vi csuf;
while (!a.empty() && !b.empty() && a.back() == b.back()) {
csuf.push_back(a.back());
a.pop_back();
b.pop_back();
}
reverse(csuf.begin(), csuf.end());
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
while (!a.empty() && !b.empty() && a.back() == b.back()) {
cpref.push_back(a.back());
a.pop_back();
b.pop_back();
}
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
vi rc = _ucs(a, b);
if (rc == vi({-1})) return rc;
vi c;
for (int x : cpref) c.push_back(x);
for (int x : rc) c.push_back(x);
for (int x : csuf) c.push_back(x);
return c;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
37480 KB |
Output is correct |
2 |
Correct |
12 ms |
37468 KB |
Output is correct |
3 |
Correct |
13 ms |
37484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
37464 KB |
Output is correct |
2 |
Correct |
13 ms |
37468 KB |
Output is correct |
3 |
Correct |
13 ms |
37500 KB |
Output is correct |
4 |
Correct |
11 ms |
37340 KB |
Output is correct |
5 |
Correct |
54 ms |
40092 KB |
Output is correct |
6 |
Correct |
66 ms |
47496 KB |
Output is correct |
7 |
Correct |
27 ms |
39376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
37464 KB |
Output is correct |
2 |
Correct |
13 ms |
37468 KB |
Output is correct |
3 |
Correct |
13 ms |
37500 KB |
Output is correct |
4 |
Correct |
11 ms |
37340 KB |
Output is correct |
5 |
Correct |
54 ms |
40092 KB |
Output is correct |
6 |
Correct |
66 ms |
47496 KB |
Output is correct |
7 |
Correct |
27 ms |
39376 KB |
Output is correct |
8 |
Correct |
68 ms |
46132 KB |
Output is correct |
9 |
Correct |
69 ms |
46384 KB |
Output is correct |
10 |
Correct |
66 ms |
46324 KB |
Output is correct |
11 |
Correct |
70 ms |
46228 KB |
Output is correct |
12 |
Correct |
66 ms |
46772 KB |
Output is correct |
13 |
Correct |
66 ms |
46524 KB |
Output is correct |
14 |
Correct |
68 ms |
46236 KB |
Output is correct |
15 |
Correct |
66 ms |
46644 KB |
Output is correct |
16 |
Correct |
70 ms |
46408 KB |
Output is correct |
17 |
Correct |
74 ms |
46408 KB |
Output is correct |
18 |
Correct |
13 ms |
37468 KB |
Output is correct |
19 |
Correct |
13 ms |
37380 KB |
Output is correct |
20 |
Correct |
14 ms |
37468 KB |
Output is correct |
21 |
Correct |
19 ms |
37680 KB |
Output is correct |
22 |
Correct |
47 ms |
43368 KB |
Output is correct |
23 |
Correct |
69 ms |
47412 KB |
Output is correct |
24 |
Correct |
67 ms |
47600 KB |
Output is correct |
25 |
Correct |
71 ms |
47604 KB |
Output is correct |
26 |
Correct |
55 ms |
43812 KB |
Output is correct |
27 |
Correct |
67 ms |
46392 KB |
Output is correct |
28 |
Correct |
92 ms |
46392 KB |
Output is correct |
29 |
Correct |
64 ms |
45624 KB |
Output is correct |
30 |
Correct |
62 ms |
45544 KB |
Output is correct |
31 |
Correct |
72 ms |
46252 KB |
Output is correct |
32 |
Correct |
59 ms |
45236 KB |
Output is correct |
33 |
Incorrect |
66 ms |
45980 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
40140 KB |
Output is correct |
2 |
Correct |
34 ms |
39884 KB |
Output is correct |
3 |
Correct |
50 ms |
48064 KB |
Output is correct |
4 |
Correct |
45 ms |
47952 KB |
Output is correct |
5 |
Correct |
66 ms |
50464 KB |
Output is correct |
6 |
Correct |
26 ms |
40256 KB |
Output is correct |
7 |
Correct |
19 ms |
37468 KB |
Output is correct |
8 |
Correct |
14 ms |
37468 KB |
Output is correct |
9 |
Correct |
14 ms |
37468 KB |
Output is correct |
10 |
Correct |
13 ms |
37468 KB |
Output is correct |
11 |
Correct |
12 ms |
37468 KB |
Output is correct |
12 |
Correct |
13 ms |
37480 KB |
Output is correct |
13 |
Correct |
13 ms |
37480 KB |
Output is correct |
14 |
Correct |
19 ms |
37468 KB |
Output is correct |
15 |
Correct |
14 ms |
37464 KB |
Output is correct |
16 |
Correct |
44 ms |
45024 KB |
Output is correct |
17 |
Correct |
46 ms |
45064 KB |
Output is correct |
18 |
Correct |
28 ms |
40024 KB |
Output is correct |
19 |
Correct |
28 ms |
40184 KB |
Output is correct |
20 |
Correct |
60 ms |
42716 KB |
Output is correct |
21 |
Correct |
28 ms |
41156 KB |
Output is correct |
22 |
Correct |
60 ms |
45980 KB |
Output is correct |
23 |
Correct |
39 ms |
46756 KB |
Output is correct |
24 |
Correct |
67 ms |
50272 KB |
Output is correct |
25 |
Correct |
53 ms |
45332 KB |
Output is correct |
26 |
Correct |
50 ms |
50860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
46132 KB |
Output is correct |
2 |
Correct |
69 ms |
46384 KB |
Output is correct |
3 |
Correct |
66 ms |
46324 KB |
Output is correct |
4 |
Correct |
70 ms |
46228 KB |
Output is correct |
5 |
Correct |
66 ms |
46772 KB |
Output is correct |
6 |
Correct |
66 ms |
46524 KB |
Output is correct |
7 |
Correct |
68 ms |
46236 KB |
Output is correct |
8 |
Correct |
66 ms |
46644 KB |
Output is correct |
9 |
Correct |
70 ms |
46408 KB |
Output is correct |
10 |
Correct |
74 ms |
46408 KB |
Output is correct |
11 |
Correct |
13 ms |
37468 KB |
Output is correct |
12 |
Correct |
13 ms |
37380 KB |
Output is correct |
13 |
Correct |
14 ms |
37468 KB |
Output is correct |
14 |
Correct |
19 ms |
37680 KB |
Output is correct |
15 |
Correct |
47 ms |
43368 KB |
Output is correct |
16 |
Correct |
69 ms |
47412 KB |
Output is correct |
17 |
Correct |
67 ms |
47600 KB |
Output is correct |
18 |
Correct |
71 ms |
47604 KB |
Output is correct |
19 |
Correct |
55 ms |
43812 KB |
Output is correct |
20 |
Correct |
51 ms |
40140 KB |
Output is correct |
21 |
Correct |
34 ms |
39884 KB |
Output is correct |
22 |
Correct |
50 ms |
48064 KB |
Output is correct |
23 |
Correct |
45 ms |
47952 KB |
Output is correct |
24 |
Correct |
66 ms |
50464 KB |
Output is correct |
25 |
Correct |
26 ms |
40256 KB |
Output is correct |
26 |
Correct |
17 ms |
37624 KB |
Output is correct |
27 |
Correct |
14 ms |
37464 KB |
Output is correct |
28 |
Correct |
15 ms |
37468 KB |
Output is correct |
29 |
Correct |
16 ms |
37464 KB |
Output is correct |
30 |
Correct |
16 ms |
37572 KB |
Output is correct |
31 |
Correct |
20 ms |
37684 KB |
Output is correct |
32 |
Correct |
17 ms |
37724 KB |
Output is correct |
33 |
Correct |
17 ms |
37720 KB |
Output is correct |
34 |
Correct |
28 ms |
40628 KB |
Output is correct |
35 |
Correct |
56 ms |
52076 KB |
Output is correct |
36 |
Correct |
78 ms |
51740 KB |
Output is correct |
37 |
Correct |
83 ms |
52516 KB |
Output is correct |
38 |
Correct |
58 ms |
52276 KB |
Output is correct |
39 |
Correct |
76 ms |
52284 KB |
Output is correct |
40 |
Correct |
65 ms |
48116 KB |
Output is correct |
41 |
Correct |
65 ms |
45196 KB |
Output is correct |
42 |
Correct |
40 ms |
42044 KB |
Output is correct |
43 |
Correct |
47 ms |
41908 KB |
Output is correct |
44 |
Correct |
36 ms |
41936 KB |
Output is correct |
45 |
Correct |
39 ms |
41980 KB |
Output is correct |
46 |
Correct |
63 ms |
44084 KB |
Output is correct |
47 |
Correct |
59 ms |
43648 KB |
Output is correct |
48 |
Correct |
63 ms |
43808 KB |
Output is correct |
49 |
Correct |
79 ms |
43532 KB |
Output is correct |
50 |
Correct |
64 ms |
43524 KB |
Output is correct |
51 |
Correct |
59 ms |
43360 KB |
Output is correct |
52 |
Correct |
59 ms |
43676 KB |
Output is correct |
53 |
Correct |
82 ms |
43428 KB |
Output is correct |
54 |
Correct |
60 ms |
43264 KB |
Output is correct |
55 |
Correct |
25 ms |
39380 KB |
Output is correct |
56 |
Correct |
76 ms |
50644 KB |
Output is correct |
57 |
Correct |
53 ms |
49128 KB |
Output is correct |
58 |
Correct |
57 ms |
50724 KB |
Output is correct |
59 |
Correct |
58 ms |
50208 KB |
Output is correct |
60 |
Correct |
63 ms |
47236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
37468 KB |
Output is correct |
2 |
Correct |
14 ms |
37468 KB |
Output is correct |
3 |
Correct |
14 ms |
37468 KB |
Output is correct |
4 |
Correct |
13 ms |
37468 KB |
Output is correct |
5 |
Correct |
12 ms |
37468 KB |
Output is correct |
6 |
Correct |
13 ms |
37480 KB |
Output is correct |
7 |
Correct |
13 ms |
37480 KB |
Output is correct |
8 |
Correct |
19 ms |
37468 KB |
Output is correct |
9 |
Correct |
14 ms |
37464 KB |
Output is correct |
10 |
Correct |
17 ms |
37624 KB |
Output is correct |
11 |
Correct |
14 ms |
37464 KB |
Output is correct |
12 |
Correct |
15 ms |
37468 KB |
Output is correct |
13 |
Correct |
16 ms |
37464 KB |
Output is correct |
14 |
Correct |
16 ms |
37572 KB |
Output is correct |
15 |
Correct |
20 ms |
37684 KB |
Output is correct |
16 |
Correct |
17 ms |
37724 KB |
Output is correct |
17 |
Correct |
17 ms |
37720 KB |
Output is correct |
18 |
Correct |
16 ms |
37720 KB |
Output is correct |
19 |
Correct |
14 ms |
37800 KB |
Output is correct |
20 |
Correct |
14 ms |
37580 KB |
Output is correct |
21 |
Correct |
15 ms |
37724 KB |
Output is correct |
22 |
Correct |
15 ms |
37468 KB |
Output is correct |
23 |
Correct |
14 ms |
37468 KB |
Output is correct |
24 |
Incorrect |
21 ms |
37732 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
37480 KB |
Output is correct |
2 |
Correct |
12 ms |
37468 KB |
Output is correct |
3 |
Correct |
13 ms |
37484 KB |
Output is correct |
4 |
Correct |
13 ms |
37464 KB |
Output is correct |
5 |
Correct |
13 ms |
37468 KB |
Output is correct |
6 |
Correct |
13 ms |
37500 KB |
Output is correct |
7 |
Correct |
11 ms |
37340 KB |
Output is correct |
8 |
Correct |
54 ms |
40092 KB |
Output is correct |
9 |
Correct |
66 ms |
47496 KB |
Output is correct |
10 |
Correct |
27 ms |
39376 KB |
Output is correct |
11 |
Correct |
68 ms |
46132 KB |
Output is correct |
12 |
Correct |
69 ms |
46384 KB |
Output is correct |
13 |
Correct |
66 ms |
46324 KB |
Output is correct |
14 |
Correct |
70 ms |
46228 KB |
Output is correct |
15 |
Correct |
66 ms |
46772 KB |
Output is correct |
16 |
Correct |
66 ms |
46524 KB |
Output is correct |
17 |
Correct |
68 ms |
46236 KB |
Output is correct |
18 |
Correct |
66 ms |
46644 KB |
Output is correct |
19 |
Correct |
70 ms |
46408 KB |
Output is correct |
20 |
Correct |
74 ms |
46408 KB |
Output is correct |
21 |
Correct |
13 ms |
37468 KB |
Output is correct |
22 |
Correct |
13 ms |
37380 KB |
Output is correct |
23 |
Correct |
14 ms |
37468 KB |
Output is correct |
24 |
Correct |
19 ms |
37680 KB |
Output is correct |
25 |
Correct |
47 ms |
43368 KB |
Output is correct |
26 |
Correct |
69 ms |
47412 KB |
Output is correct |
27 |
Correct |
67 ms |
47600 KB |
Output is correct |
28 |
Correct |
71 ms |
47604 KB |
Output is correct |
29 |
Correct |
55 ms |
43812 KB |
Output is correct |
30 |
Correct |
67 ms |
46392 KB |
Output is correct |
31 |
Correct |
92 ms |
46392 KB |
Output is correct |
32 |
Correct |
64 ms |
45624 KB |
Output is correct |
33 |
Correct |
62 ms |
45544 KB |
Output is correct |
34 |
Correct |
72 ms |
46252 KB |
Output is correct |
35 |
Correct |
59 ms |
45236 KB |
Output is correct |
36 |
Incorrect |
66 ms |
45980 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |