sacgajcvs

Untitled

Oct 13th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. /*
  2. _____ _ _ _ _
  3. |_ _| |__ ___ / \ _ __ ___| |__ _ _| |
  4. | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
  5. | | | | | | __// ___ \| | | \__ \ | | | |_| | |
  6. |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
  7.  
  8. */
  9. #include<bits/stdc++.h>
  10. #include <ext/pb_ds/assoc_container.hpp>
  11. #include <ext/pb_ds/tree_policy.hpp>
  12. #define ll long long
  13. #define pb push_back
  14. #define ppb pop_back
  15. #define endl '\n'
  16. #define mii map<ll,ll>
  17. #define msi map<string,ll>
  18. #define mis map<ll, string>
  19. #define rep(i,a,b) for(ll i=a;i<b;i++)
  20. #define repr(i,a,b) for(ll i=b-1;i>=a;i--)
  21. #define trav(a, x) for(auto& a : x)
  22. #define pii pair<ll,ll>
  23. #define vi vector<ll>
  24. #define vii vector<pair<ll, ll>>
  25. #define vs vector<string>
  26. #define all(a) (a).begin(),(a).end()
  27. #define F first
  28. #define S second
  29. #define sz(x) (ll)x.size()
  30. #define hell 1000000007
  31. #define lbnd lower_bound
  32. #define ubnd upper_bound
  33. #define max(a,b) (a>b?a:b)
  34. #define min(a,b) (a<b?a:b)
  35.  
  36. /* For Debugging */
  37. #define DEBUG cerr<<"\n>>>I'm Here<<<\n"<<endl;
  38. #define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
  39. #define what_iss(x) cerr << #x << " = " << x << endl;
  40. #define what_is(x) cerr << #x << " = " << x << " ";
  41.  
  42. std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  43. #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
  44. #define TIME cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
  45. #define DECIMAL(n) cout << fixed ; cout << setprecision(n);
  46. #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  47. using namespace __gnu_pbds;
  48. using namespace std;
  49. #define PI 3.141592653589793
  50. #define N 100005
  51.  
  52. class Solution {
  53. public:
  54.  
  55. void fun(string& s, int l,int mid,int r) {
  56. string p1 = "";
  57. string p2 = "";
  58. for(int i = l; i<=mid; i++) {
  59. p1+=s[i];
  60. }
  61. for(int i = mid+1; i<=r; i++) {
  62. p2+=s[i];
  63. }
  64. for(int i=0;i<p2.length();i++) {
  65. s[l+i] = p2[i];
  66. }
  67. for(int i=0;i<p1.length();i++) {
  68. s[l+p2.length()+i] = p1[i];
  69. }
  70. }
  71.  
  72. int pref(string s,int l) {
  73. int cnt = 0;
  74. for(int i = l;i >= 0;i --) {
  75. cnt += (s[i]=='0'?1:-1);
  76. if(cnt == 0)
  77. return i;
  78. }
  79. return 0;
  80. }
  81.  
  82. int suf(string s,int l) {
  83. int cnt = 0;
  84. for(int i = l;i < s.length();i ++) {
  85. cnt += (s[i]=='0'?1:-1);
  86. if(cnt == 0)
  87. return i;
  88. }
  89. return s.length() - 1;
  90. }
  91.  
  92. int calcOne(string s,int l) {
  93. int cnt = 0;
  94. while(l<s.length() && s[l]=='1') {
  95. l++;
  96. cnt++;
  97. }
  98. return cnt;
  99. }
  100.  
  101. bool check(string s,int l,int mid,int r) {
  102. for(int i = l,j = mid + 1; i <= mid && j <= r; i++,j++) {
  103. if(s[i]!=s[j]) {
  104. return (s[j]=='1');
  105. }
  106. }
  107. return 0;
  108. }
  109.  
  110. string makeLargestSpecial(string s) {
  111. int n = s.length();
  112. int j,i=0;
  113. int num1,num2;
  114. while(i<n) {
  115. // what_is(i);
  116. // what_iss(s);
  117. if(s[i] == '1') {
  118. if(i>0 && s[i-1]=='1') {
  119. i--;
  120. // what_iss(i);
  121. continue;
  122. }
  123. num1 = calcOne(s,i);
  124. if(i == 0) {
  125. i+=num1;
  126. continue;
  127. }
  128. j = pref(s,i-1);
  129. num2 = calcOne(s,j);
  130. if(check(s,j,i-1,suf(s,i))) {
  131. // what_is(i);
  132. // what_is(j);
  133. // what_is(num1);
  134. // what_is(num2);
  135. // what_iss(suf(s,i));
  136. fun(s,j,i-1,suf(s,i));
  137. i = j;
  138. continue;
  139. } else {
  140. i += num1;
  141. }
  142. } else {
  143. i++;
  144. continue;
  145. }
  146. }
  147. return s;
  148. }
  149. };
  150.  
  151. void solve()
  152. {
  153. string s;
  154. cin >> s;
  155. Solution tmp;
  156. cout << tmp.makeLargestSpecial(s) << endl;
  157. return;
  158. }
  159. int main()
  160. {
  161. FAST
  162. int TESTS=1;
  163. // cin>>TESTS;
  164. rep(i,0,TESTS)
  165. {
  166. // cout<<"Case #"<<i+1<<": ";
  167. solve();
  168. }
  169. // TIME
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment