Advertisement
fireLUFFY

Divide3

Oct 11th, 2021 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define inf 2e18
  6. int MOD=1000000007;//998244353;
  7. const int nn=1000050;
  8. bool prime[nn]; //array to store precalculated primes till 10^6
  9. void cal_primes(){memset(prime,true,sizeof(prime)); for(int i=2;i<=sqrt(nn);++i){ if(prime[i]==true){ for(int j=i*i;j<=nn;j+=i){prime[j]=false;}}}}
  10.  
  11. void solve(int t)
  12. {
  13.     int testcases=t;
  14.     while(t--)
  15.     {
  16.         string s;cin>>s;
  17.         int n=s.length(),ans=0,len=0,c0=0,c1=0;
  18.         vector<pair<char,int>>v;
  19.         for(int i=0;i<n;i++)
  20.         {
  21.             if(s[i]=='0')
  22.             {
  23.                 if(c1>0)
  24.                     v.push_back(make_pair('1',c1)),c1=0;
  25.                 c0++;
  26.             }
  27.             else
  28.             {
  29.                 if(c0>0)
  30.                     v.push_back(make_pair('0',c0)),c0=0;
  31.                 c1++;
  32.             }
  33.         }
  34.         if(c1>0)
  35.             v.push_back(make_pair('1',c1)),c1=0;
  36.         else
  37.             v.push_back(make_pair('0',c0)),c0=0;
  38.  
  39.         // for(auto it:v)
  40.         //  cout<<it.first<<" "<<it.second<<endl;
  41.  
  42.         for(int i=0;i<v.size();i++)
  43.         {
  44.             ans=0;
  45.             if(v[i].first=='1')
  46.             {
  47.                 ans+=v[i].second;
  48.                 if(i!=0)
  49.                     ans+=v[i-1].second;
  50.                 if(i!=(v.size()-1))
  51.                     ans+=v[i+1].second;
  52.             }
  53.             else
  54.                 ans+=v[i].second;
  55.  
  56.             len=max(len,ans);
  57.         }
  58.         cout<<len<<endl;
  59.     }
  60. }
  61.  
  62. main()
  63. {
  64.     auto start=chrono::system_clock::now();
  65.     {
  66.         #ifndef ONLINE_JUDGE
  67.             freopen("input.txt","r",stdin);
  68.             freopen("output.txt","w",stdout);
  69.             freopen("error.txt","w",stderr);
  70.         #endif 
  71.         ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  72.         int t=1;
  73.     //  cin>>t;
  74.         solve(t);
  75.     }
  76.     auto end=chrono::system_clock::now();
  77.     chrono::duration<double> elapsed=end-start;
  78. //  cout<<endl<<"Time taken: "<<elapsed.count()<<" sec";
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement