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 Kotlin (1.0.0) (judge verdict: Wrong Answer)

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*

class D

fun main( args: Array<String> ) {
    val br = BufferedReader( InputStreamReader( System.`in` ) )
//    while ( true ) {
        solve( br )
//    }
}

fun solve( br: BufferedReader ) {
    val s = ( "0" + br.readLine() + "0" ).toCharArray()
    val change = ArrayList<Int>()
    for ( i in 0 until s.lastIndex ) {
        if ( s[i] != s[i + 1] ) {
            change.add( i )
        }
    }
    val change0 = ArrayList<Int>()
    change0.add( 0 )
    change0.add( 0 )
    change0.addAll( change )
    val change1 = ArrayList<Int>()
    change1.add( 0 )
    change1.addAll( change )
    change1.add( s.size - 2 )
    var best = Math.max( getBest( s, change ), getBest( s, change0 ) )
    change.add( s.size - 2 )
    change.add( s.size - 2 )
    change0.add( s.size - 2 )
    change0.add( s.size - 2 )
    best = Math.max( Math.max( best, getBest( s, change1 ) ), Math.max( getBest( s, change ), getBest( s, change0 ) ) )
    println( best )
}

private fun getBest( s: CharArray, change: ArrayList<Int>): Int {
    var l = 1
    var r = s.size - 2
    while ( l < r ) {
        val m = ( l + r + 1 ) / 2
        var good = true
        for ( i in 0 until change.size / 2 ) {
            good = good && ( ( change[i + change.size / 2] - change[i] ) >= m )
        }
        if ( good ) {
            l = m
        } else {
            r = m - 1
        }
    }
    return l
}