Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- _____ _ _ _ _
- |_ _| |__ ___ / \ _ __ ___| |__ _ _| |
- | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
- | | | | | | __// ___ \| | | \__ \ | | | |_| | |
- |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
- */
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define ll long long
- #define pb push_back
- #define ppb pop_back
- #define endl '\n'
- #define mii map<ll,ll>
- #define msi map<string,ll>
- #define mis map<ll, string>
- #define rep(i,a,b) for(ll i=a;i<b;i++)
- #define repr(i,a,b) for(ll i=b-1;i>=a;i--)
- #define trav(a, x) for(auto& a : x)
- #define pii pair<ll,ll>
- #define vi vector<ll>
- #define vii vector<pair<ll, ll>>
- #define vs vector<string>
- #define all(a) (a).begin(),(a).end()
- #define F first
- #define S second
- #define sz(x) (ll)x.size()
- #define hell 1000000007
- #define lbnd lower_bound
- #define ubnd upper_bound
- #define max(a,b) (a>b?a:b)
- #define min(a,b) (a<b?a:b)
- /* For Debugging */
- #define DEBUG cerr<<"\n>>>I'm Here<<<\n"<<endl;
- #define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
- #define what_iss(x) cerr << #x << " = " << x << endl;
- #define what_is(x) cerr << #x << " = " << x << " ";
- std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
- #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
- #define TIME cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
- #define DECIMAL(n) cout << fixed ; cout << setprecision(n);
- #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- using namespace __gnu_pbds;
- using namespace std;
- #define PI 3.141592653589793
- #define N 100005
- class Solution {
- public:
- void fun(string& s, int l,int mid,int r) {
- string p1 = "";
- string p2 = "";
- for(int i = l; i<=mid; i++) {
- p1+=s[i];
- }
- for(int i = mid+1; i<=r; i++) {
- p2+=s[i];
- }
- for(int i=0;i<p2.length();i++) {
- s[l+i] = p2[i];
- }
- for(int i=0;i<p1.length();i++) {
- s[l+p2.length()+i] = p1[i];
- }
- }
- int pref(string s,int l) {
- int cnt = 0;
- for(int i = l;i >= 0;i --) {
- cnt += (s[i]=='0'?1:-1);
- if(cnt == 0)
- return i;
- }
- return 0;
- }
- int suf(string s,int l) {
- int cnt = 0;
- for(int i = l;i < s.length();i ++) {
- cnt += (s[i]=='0'?1:-1);
- if(cnt == 0)
- return i;
- }
- return s.length() - 1;
- }
- int calcOne(string s,int l) {
- int cnt = 0;
- while(l<s.length() && s[l]=='1') {
- l++;
- cnt++;
- }
- return cnt;
- }
- bool check(string s,int l,int mid,int r) {
- for(int i = l,j = mid + 1; i <= mid && j <= r; i++,j++) {
- if(s[i]!=s[j]) {
- return (s[j]=='1');
- }
- }
- return 0;
- }
- string makeLargestSpecial(string s) {
- int n = s.length();
- int j,i=0;
- int num1,num2;
- while(i<n) {
- // what_is(i);
- // what_iss(s);
- if(s[i] == '1') {
- if(i>0 && s[i-1]=='1') {
- i--;
- // what_iss(i);
- continue;
- }
- num1 = calcOne(s,i);
- if(i == 0) {
- i+=num1;
- continue;
- }
- j = pref(s,i-1);
- num2 = calcOne(s,j);
- if(check(s,j,i-1,suf(s,i))) {
- // what_is(i);
- // what_is(j);
- // what_is(num1);
- // what_is(num2);
- // what_iss(suf(s,i));
- fun(s,j,i-1,suf(s,i));
- i = j;
- continue;
- } else {
- i += num1;
- }
- } else {
- i++;
- continue;
- }
- }
- return s;
- }
- };
- void solve()
- {
- string s;
- cin >> s;
- Solution tmp;
- cout << tmp.makeLargestSpecial(s) << endl;
- return;
- }
- int main()
- {
- FAST
- int TESTS=1;
- // cin>>TESTS;
- rep(i,0,TESTS)
- {
- // cout<<"Case #"<<i+1<<": ";
- solve();
- }
- // TIME
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment