Problem:
Score : 500 points

Problem Statement

You are given a string S consisting of 0 and 1.
Find the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times.

Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\geq K must be satisfied). For each integer i such that l\leq i\leq r, do the following: if S_i is 0, replace it with 1; if S_i is 1, replace it with 0.

Constraints

1\leq |S|\leq 10^5

S_i(1\leq i\leq N) is either 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times.

Sample Input 1

010

Sample Output 1

2

We can turn all the characters of S into 0 by the following operations:

Perform the operation on the segment S[1,3] with length 3. S is now 101.

Perform the operation on the segment S[1,2] with length 2. S is now 011.

Perform the operation on the segment S[2,3] with length 2. S is now 000.

Sample Input 2

100000000

Sample Output 2

8

Sample Input 3

00001111

Sample Output 3

4

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

using i64=int64_t;

#define rep(i,x,y) for(i64 i=i64(x),i##_max_for_repmacro=i64(y); i<i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)
 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif
 
const int inf=1.01e9;
const i64 inf64=4.01e18;
const double eps=1e-9;
 
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
        os << v << ",";
    }
    os << "]";
    return os;
}

void solve(){
    string S;
    cin >> S;

    if(S.find('0')==string::npos or S.find('1')==string::npos){
        cout << S.size() << endl;
        return;
    }

    auto ok=[&](int k){
        vector<int> tmp(S.size());
        rep(i,0,S.size()){
            if(S[i]=='1') tmp[i]=1;
        }

        vector<int> f(S.size());
        int sum=0;
        rep(i,0,S.size()-k+1){
            if((tmp[i]+sum)%2){
                f[i]=1;
            }
            sum+=f[i];
            if(i-k+1>=0) sum-=f[i-k+1];
        }

        vector<int> tmp2(S.size()-(S.size()-k+1));
        rep(i,S.size()-k+1,S.size()){
            if((tmp[i]+sum)%2) tmp2[i-(S.size()-k+1)]=1;
            if(i-k+1>=0) sum-=f[i-k+1];
        }
        
        int cnt=0;
        bool flag=false;
        rep(i,0,tmp2.size()){
            if(flag and tmp2[i]==0) return false;
            if(tmp2[i]==1) flag=true;
            if(flag) ++cnt;
        }

        return max(cnt,int(S.size())-cnt)>=k;
    };

    auto ok2=[&](int k){
        vector<int> tmp(S.size());
        reverse(begin(S),end(S));
        rep(i,0,S.size()){
            if(S[i]=='1') tmp[i]=1;
        }
        reverse(begin(S),end(S));

        vector<int> f(S.size());
        int sum=0;
        rep(i,0,S.size()-k+1){
            if((tmp[i]+sum)%2){
                f[i]=1;
            }
            sum+=f[i];
            if(i-k+1>=0) sum-=f[i-k+1];
        }

        vector<int> tmp2(S.size()-(S.size()-k+1));
        rep(i,S.size()-k+1,S.size()){
            if((tmp[i]+sum)%2) tmp2[i-(S.size()-k+1)]=1;
            if(i-k+1>=0) sum-=f[i-k+1];
        }
        
        bool flag=false;
        int cnt=0;
        rep(i,0,tmp2.size()){
            if(flag and tmp2[i]==0) return false;
            if(tmp2[i]==1) flag=true;
            if(flag) ++cnt;
        }

        return max(cnt,int(S.size())-cnt)>=k;
    };

    auto ok3=[&](int k){
        vector<int> tmp(S.size());
        rep(i,0,S.size()){
            if(S[i]=='0') tmp[i]=1;
        }

        vector<int> f(S.size());
        int sum=0;
        rep(i,0,S.size()-k+1){
            if((tmp[i]+sum)%2){
                f[i]=1;
            }
            sum+=f[i];
            if(i-k+1>=0) sum-=f[i-k+1];
        }

        vector<int> tmp2(S.size()-(S.size()-k+1));
        rep(i,S.size()-k+1,S.size()){
            if((tmp[i]+sum)%2) tmp2[i-(S.size()-k+1)]=1;
            if(i-k+1>=0) sum-=f[i-k+1];
        }
        
        int cnt=0;
        bool flag=false;
        rep(i,0,tmp2.size()){
            if(flag and tmp2[i]==0) return false;
            if(tmp2[i]==1) flag=true;
            if(flag) ++cnt;
        }

        return max(cnt,int(S.size())-cnt)>=k;
    };

    auto ok4=[&](int k){
        vector<int> tmp(S.size());
        reverse(begin(S),end(S));
        rep(i,0,S.size()){
            if(S[i]=='0') tmp[i]=1;
        }
        reverse(begin(S),end(S));

        vector<int> f(S.size());
        int sum=0;
        rep(i,0,S.size()-k+1){
            if((tmp[i]+sum)%2){
                f[i]=1;
            }
            sum+=f[i];
            if(i-k+1>=0) sum-=f[i-k+1];
        }

        vector<int> tmp2(S.size()-(S.size()-k+1));
        rep(i,S.size()-k+1,S.size()){
            if((tmp[i]+sum)%2) tmp2[i-(S.size()-k+1)]=1;
            if(i-k+1>=0) sum-=f[i-k+1];
        }
        
        bool flag=false;
        int cnt=0;
        rep(i,0,tmp2.size()){
            if(flag and tmp2[i]==0) return false;
            if(tmp2[i]==1) flag=true;
            if(flag) ++cnt;
        }

        return max(cnt,int(S.size())-cnt)>=k;
    };

    int lb=1,ub=S.size();
    while(ub-lb>1){
        int mid=(lb+ub)/2;
        if(ok(mid) or ok2(mid) or ok3(mid) or ok4(mid)) lb=mid;
        else ub=mid;
    }

    cout << lb << endl;
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}