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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::io::*;
use std::str::FromStr;
use std::collections::HashSet;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char) 
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}


fn main() {
    let s: String = read();  // e.g. 01100111000100
    let chars: Vec<char> = s.chars().collect();

    let mut first_c = chars[0];
    let mut before_c = chars[0];
    let mut cnt = 1;
    let mut nums_vec: Vec<u32> = Vec::new();
    for c in chars.into_iter().skip(1) {
        if before_c == c {
            cnt += 1;
        } else {
            // println!("{} {}", before_c, cnt);
            nums_vec.push(cnt);
            cnt = 1;
            before_c = c;
        }
    }
    nums_vec.push(cnt);

    // println!("{:?}", nums_vec);

    let mut scores: HashSet<u32> = HashSet::new();
    while nums_vec.len() > 2 {
        let (min_idx, _) = nums_vec.iter().enumerate().min_by_key(|&(_, &a)| a).unwrap();

        let base_idx;
        if min_idx == 0 {
            let k1 = nums_vec[min_idx] + nums_vec[min_idx + 1];
            let k2 = nums_vec[min_idx + 1] + nums_vec[min_idx + 2];
            if k1 >= k2 {
                base_idx = min_idx;
            } else {
                base_idx = min_idx + 1;
            }
        } else if min_idx == nums_vec.len() - 1 {
            let k1 = nums_vec[min_idx - 2] + nums_vec[min_idx - 1];
            let k2 = nums_vec[min_idx - 1] + nums_vec[min_idx];
            if k1 >= k2 {
                base_idx = min_idx - 2;
            } else {
                base_idx = min_idx - 1;
            }
        } else {
            let k1 = nums_vec[min_idx - 1] + nums_vec[min_idx];
            let k2 = nums_vec[min_idx] + nums_vec[min_idx + 1];
            if k1 >= k2 {
                base_idx = min_idx - 1;
            } else {
                base_idx = min_idx;
            }
        }

        let k = nums_vec[base_idx] + nums_vec[base_idx + 1];
        scores.insert(k);
        // println!("{:?}", nums_vec);
        // println!("{}", k);

        if base_idx == 0 {
            let n1 = nums_vec[base_idx + 1];
            let n2 = nums_vec[base_idx] + nums_vec[base_idx + 2];
            nums_vec[base_idx] = n1;
            nums_vec[base_idx + 1] = n2;
            nums_vec.remove(base_idx + 2);
            if first_c == '0' {
                first_c = '1';
            } else {
                first_c = '0';
            }
        } else if base_idx == nums_vec.len() - 2 {
            let n1 = nums_vec[base_idx - 1] + nums_vec[base_idx + 1];
            let n2 = nums_vec[base_idx];
            nums_vec[base_idx - 1] = n1;
            nums_vec[base_idx] = n2;
            nums_vec.remove(base_idx + 1);
        } else {
            let n1 = nums_vec[base_idx - 1] + nums_vec[base_idx + 1];
            let n2 = nums_vec[base_idx] + nums_vec[base_idx + 2];
            nums_vec[base_idx - 1] = n1;
            nums_vec[base_idx] = n2;
            nums_vec.remove(base_idx + 2);
            nums_vec.remove(base_idx + 1);
        }
    }

    // println!("first_c: {}", first_c);
    // println!("{:?}", nums_vec);
    // println!("{:?}", scores);

    if nums_vec.len() == 2 {
        if nums_vec[0] > nums_vec[1] {
            if first_c == '0' {
                first_c = '1';
            }
        } else if nums_vec[0] < nums_vec[1] {
            if first_c == '1' {
                first_c = '0';
            }
        }
        if first_c == '0' {
            scores.insert(nums_vec[1]);
        } else {
            scores.insert(nums_vec[0]);
        }
    } else if nums_vec.len() == 1 {
        let n = nums_vec.remove(0);
        if first_c == '1' {
            scores.insert(n);
        } else {
            scores.insert(n);
        }
    }

    // println!("first_c: {}", first_c);
    // println!("{:?}", nums_vec);
    // println!("{:?}", scores);
    
    let ans = scores.iter().min().unwrap();
    println!("{}", ans);
}