-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinary_String_MEX.cpp
More file actions
96 lines (90 loc) · 2.16 KB
/
Binary_String_MEX.cpp
File metadata and controls
96 lines (90 loc) · 2.16 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
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;cin>>t;
while(t--)
{
string s;
cin>>s;
int n=s.size();
int l=-1;
int dp[1000000],dl[1000000];
int com1[1000000],com0[1000000];
memset(dp,0,sizeof(dp));
memset(dl,0,sizeof(dl));
memset(com0,0,sizeof(com0));
memset(com1,0,sizeof(com1));
for(int i=0;i<n;i++)
{
if(s[i]=='0')
{
for(int j=l+1;j<=i;j++)
com0[j]=i;l=i;
}
}
for(int i=l+1;i<n;i++)com0[i]=n;
if(com0[0]==n)
{
cout<<0<<endl;
continue;
}
l=-1;
dp[n]=0;
dl[n]=0;
for(int i=0;i<n;i++)
{
if(s[i]=='1')
{
for(int j=l+1;j<=i;j++)
com1[j]=i;
l=i;
}
}
for(int i=l+1;i<n;i++)
{
com1[i]=n;
}
dp[n+1]=0;
dl[n+1]=0;
for(int i=n-1;i>=0;--i)
{
dp[i]=dp[i+1];
if(s[i]=='0' && com1[i]<n )
dp[i]=max( dp[i] , dp[com1[i]+1]+1 );
if(s[i]=='1' && com0[i]<n )
dp[i]=max( dp[i] , dp[com0[i]+1]+1 );
dl[i]=dl[i+1];
if(com1[i]<n)
dl[i] = max( dl[i] , dp[com1[i]+1]+1 );
}
int r=dl[0]+1;
int cur=com1[0]+1;
string ans="1";
for(int i=1;i<r;++i)
{
if(cur>=n)
{
ans.push_back('0');
continue;
}
if(com0[cur]>=n)
{
ans.push_back('0');
cur=com0[cur]+1;
continue;
}
if(dp[com0[cur]+1] < r-i-1 )
{
ans.push_back('0');
cur=com0[cur]+1;
}
else{
ans.push_back('1');
cur=com1[cur]+1;
}
}
cout<<ans<<endl;
}
return 0;
}