-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySuffixArray.cc
100 lines (94 loc) · 2.03 KB
/
BinarySuffixArray.cc
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef LOCAL
#pragma GCC optimize(2)
#endif
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
#define pb push_back
#define fst first
#define snd second
#define ALL(cont) cont.begin(), cont.end()
#define mset(a,b) memset(a,b,sizeof(a));
#define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i)
#define SZ(x) ((int)x.size())
#define FIN ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define RB(x) (x<n?r[x]:0)
void csort(vector<int>& sa, vector<int>& r, int k){
int n=sa.size();
vector<int> f(max(255,n),0),t(n);
fore(i,0,n)f[RB(i+k)]++;
int sum=0;
fore(i,0,max(255,n))f[i]=(sum+=f[i])-f[i];
fore(i,0,n)t[f[RB(sa[i]+k)]++]=sa[i];
sa=t;
}
vector<int> constructSA(string& s){ // O(n logn)
int n=s.size(),rank;
vector<int> sa(n),r(n),t(n);
fore(i,0,n)sa[i]=i,r[i]=s[i];
for(int k=1;k<n;k*=2){
csort(sa,r,k);csort(sa,r,0);
t[sa[0]]=rank=0;
fore(i,1,n){
if(r[sa[i]]!=r[sa[i-1]]||RB(sa[i]+k)!=RB(sa[i-1]+k))rank++;
t[sa[i]]=rank;
}
r=t;
if(r[sa[n-1]]==n-1)break;
}
return sa;
}
int main()
{
FIN;
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
freopen("binary.in", "r", stdin);
freopen("binary.out", "w", stdout);
#define endl '\n'
#endif
int n;cin>>n;
vector<int> w(n);
vector<int> w2(n);
vector<int> ans(n);
// int pos = 0;
fore(i,0,n){
cin>>w[i];
w[i]--;
w2[w[i]] = i;
}
int pos = -1;
fore(i,0,n){
if(w2[n-1-i] != i){
pos = w2[n-1-i];
break;
}
}
if(pos != -1){
fore(i,0,n){
ans[w[i]] = i>=pos;
}
}
string sans = "";
fore(i,0,n){
sans+=(ans[i]+'0');
}
sans+='$';
// sans = "000$";
vector<int> sufa = constructSA(sans);
vector<int> aProbar;
fore(i,1,SZ(sufa)){
aProbar.pb(sufa[i]);
}
if(w==aProbar){
cout<<sans.substr(0,n)<<endl;
} else
{
cout<<"Error"<<endl;
}
return 0;
}