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



Sample input:
010


Sample output:
2

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1             # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    

    s = input()
    n = len(s)
    ans = n
    for i in range(1, n):
        if s[i] != s[i-1]:
            ans = min(ans, max(i, n - i))
    print(ans)

    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s=input()
cs=[]
cnt=1
pre=s[0]
for si in s[1:]:
  if si==pre:
    cnt+=1
  else:
    cs.append(cnt)
    pre=si
    cnt=1
cs.append(cnt)
n=len(s)
ans1=n
merge=0
no_merge=n
for i in range(len(cs)):
  merge+=cs[i]
  no_merge-=cs[i]
  tmp=max(merge,no_merge)
  ans1=min(ans1,tmp)
cs.reverse()
ans2=n
merge=0
no_merge=n
for i in range(len(cs)):
  merge+=cs[i]
  no_merge-=cs[i]
  tmp=max(merge,no_merge)
  ans2=min(ans2,tmp)
print(max(ans1,ans2))

Submission status: Accepted

Solution code:
S = input()

n = len(S)
a = n
for i in range(n-1):
    if S[i] != S[i+1]:
        a = min(a, max(i+1, n-i-1))

print(a)

Submission status: Accepted

Solution code:
S = input()
n = len(S)    
K = n
for k in range(n-1):
    if S[k] != S[k+1]:
        K = min(K, max(k+1, n-k-1))
print(K)

Submission status: Accepted

Solution code:


def check(s, n, k):
    l = n - k
    r = k

    for c in s[l + 1:r]:
        if c != s[l]:
            return False
    
    return True


def submit():
    s = input()
    n = len(s)

    if check(s, n, n):
        print(n)
        return

    l = 1
    r = n
    while l + 1 < r:
        m = (l + r) // 2
        if check(s, n, m):
            l = m
        else:
            r = m
    print(l)
    


if __name__ == "__main__":
    submit()

Submission status: Accepted

Solution code:
s = input()

sl = list(s)
sl.append("2")

n = len(sl)
res=[]

temp =1
for i  in range(n-1):
    if sl[i+1] == sl[i]:
        temp +=1
    else:
        res.append(temp)
        temp = 1

resu = n-1
m = n-1

while len(res) >1:
    resu = min(resu,max(res[0],m-res[0]))
    x = res.pop(1)
    res[0]  = res[0]+x    
    
print(resu)

Submission status: Accepted

Solution code:
S = list(input())

def check(x):
    #print(S[len(S)-x:x])
    for y in S[len(S)-x:x]:
        if y != S[len(S)-x]: break
    else:
        return True
    return False

def bisect(l,r):
    if r - l == 1: return l
    mid = (l+r) // 2
    if check(mid) == False:
        return bisect(l,mid)
    else:
        return bisect(mid,r)
print(bisect(0,len(S)+1))

Submission status: Accepted

Solution code:
import sys
import math
input = sys.stdin.readline

s = input().strip()
n = len(s)
a = [ord(s[0]) - ord('0')]
for i in range(n - 1):
    if s[i] == s[i + 1]:
        a.append(0)
    else:
        a.append(1)
a.append(ord(s[n - 1]) - ord('0'))
ans = n
for i in range(len(a)):
    if a[i] == 1:
        ans = min(ans, max(n - i, i))
if sum(a) % 2 == 0:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
S = [0] + list(map(int, list(input())))
N = len(S) - 1

def change(s, c):
  if c == 0:
    return s
  else:
    return 1 - s

def check(k):
  count = [0] * (N+1)
  T = S.copy()
  for i in range(1, N+1):
    c = count[i-1]
    s = change(S[i], c)
    if i < N - k + 2:
      T[i] = 0
      if s == 1:
        count[i] = (c + 1) % 2
      else:
        count[i] = c        
    else:
      T[i] = s
      count[i] = c      
  n = 0
  for t in T[1:]:
    if t == 1:
      break
    n += 1
  return n >= k


left = 1 # True
right = N
while left + 1 < right:
  mid = (left + right) // 2
  if check(mid):
    left = mid
  else:
    right = mid

if right == N and check(N):
  print(N)
else:
  print(left)

Submission status: Accepted

Solution code:
def main():
    S = input()
    N = len(S)
    ans = N
    for i in range(N - 1):
        if S[i] != S[i + 1]:
            ans = min([ans, max([i + 1, N - (i + 1)])])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def rle(s):
    # ex. '01100011110' -> [1 2 3 4 1]
    before_c = s[0]
    ret = list()
    cnt = 0
    for c in s:
        if before_c == c:
            cnt += 1
        else:
            ret.append(cnt)
            before_c = c
            cnt = 1
    if cnt > 0:
        ret.append(cnt)
    return ret


def main():
    S = input()
    N = len(S)
    seq_cnt = rle(S)
    if len(seq_cnt) == 1:
        print(seq_cnt[0])
        return
    if len(seq_cnt) == 2:
        print(max(seq_cnt))
        return
    # cumsum
    cumsum_seq_cnt = [0 for _ in range(len(seq_cnt))]
    rev_cumsum_seq_cnt = [0 for _ in range(len(seq_cnt))]
    cumsum_seq_cnt[0] = seq_cnt[0]
    rev_cumsum_seq_cnt[0] = seq_cnt[-1]
    for i in range(1, len(seq_cnt)):
        cumsum_seq_cnt[i] = cumsum_seq_cnt[i-1] + seq_cnt[i]
        rev_cumsum_seq_cnt[i] = rev_cumsum_seq_cnt[i-1] + seq_cnt[-1-i]
    # search
    max_k = cumsum_seq_cnt[0]
    rev_max_k= rev_cumsum_seq_cnt[0]
    for i in range(1, len(seq_cnt)):
        k = cumsum_seq_cnt[i]
        if k <= N - cumsum_seq_cnt[i-1]:
            max_k = k
        rev_k = rev_cumsum_seq_cnt[i]
        if rev_k <= N - rev_cumsum_seq_cnt[i-1]:
            rev_max_k = rev_k
    print(max([max_k, rev_max_k]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    S=list(map(int,list(input())))
    N=len(S)
    if N%2==0:
        left=N//2-1
        right=N//2
        ans=N//2
        if S[left]==S[right]:
            x=S[left]
            while left-1>=0 and S[left-1]==x:
                left-=1
            while right+1<N and S[right+1]==x:
                right+=1
            ans+=min(N//2-left,right-N//2+1)
    else:
        left=N//2
        right=N//2
        x=S[N//2]
        ans=N//2+1
        if left-1>=0 and S[left-1]==x:
            while left-1>=0 and S[left-1]==x:
                left-=1
        if right+1<N and S[right+1]==x:
            while right+1<N and S[right+1]==x:
                right+=1
        ans+=min(N//2-left,right-N//2)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = list(input())

ans = float("INF")
if len(s) == 1:
    print(1)
    exit()
for i in range(len(s)-1):
    if s[i] != s[i+1]:
        c = max(i+1,len(s)-i-1)
    
        ans = min(ans,c)
if ans == float("INF"):
    print(len(s))
else:

    print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000000)

def main():
    S = input()
    N = len(S)
    ans = 0
    before = 0
    for i in range(N - 1):
        if S[i] != S[i + 1]:
            ans = max(ans,min(N - before,i + 1))
            before = i + 1
    ans = max(ans,N - before)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
n = len(s)
ans = n
s1 = s.count('1')
i = 0
while s1 != 0 and s1 != n:
  s1 -= int(s[i]) + int(s[i + n - 1])
  if n == 1:
    s1 += int(s[i])
  n -= 2
  ans -= 1
  i += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    s = input()
    ans = len(s)
    for i in range(len(s)-1):
        if s[i] != s[i+1]:
            tmp = max(i+1, len(s)-i-1)
            ans = min(ans, tmp)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
len_s = len(s)
ans = len_s // 2
center = ans
b = s[ans]
if len_s % 2:
  for i in range(center+1):
    if s[center-i] == s[center+i] == b:
      ans += 1
    else:
      break
else:
  for i in range(center):
    if s[center-1-i] == s[center+i] == b:
      ans += 1
    else:
      break
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
n = len(s)
if n % 2 == 0:
    for i in range(n//2):
        a = s[n // 2]
        if s[n // 2 - 1 - i] != a or s[n // 2 + i] != a:
            print(n // 2 + i)
            exit()
else:
    for i in range(1, (n - 1) // 2 + 1):
        b = s[(n-1)//2]
        if s[(n - 1) // 2 - i] != b or s[(n - 1) // 2 + i] != b:
            print((n-1)//2+i)
            exit()
print(n)

Submission status: Accepted

Solution code:
s=input()
ans=len(s)
for i in range(len(s)-1):
  if s[i]!=s[i+1]:
    ans=min(ans,max(i+1,len(s)-i-1))
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)

l = 0
r = n//2 + 1

while r - l > 1:
    m = l + (r-l)//2
    d = n//2 - m
    if s[d:n-d].count("0") == 0 or s[d:n-d].count("1") == 0:
        l = m
    else:
        r = m

if n % 2 == 0:
    print(n//2+l)
else:
    print(n//2+l+1)

Submission status: Accepted

Solution code:
slist=list(map(int,list(input())))
len_s=len(slist)

min_answer=len_s
for i in range(1,len_s):
  if slist[i-1]!=slist[i]:
    min_answer=min(min_answer,max(i,len_s-i))
    #print(i,len(slist)-i)

print(min_answer)

Submission status: Accepted

Solution code:
"""
互いに異なる隣り合う要素を揃えることに着目する

_________10___
↓
_________00___
(左端から1のところまでを反転)

_________01___
↓
_________11___
(左端から0のところまでを反転)
↓
_________00___
(全体を反転)

K>=max(k, n-k)+1だとどのような区間を取っても
01, 10を揃えることができない
"""

import sys
input = sys.stdin.readline

S = input()[:-1]

if S in ['0'*len(S), '1'*len(S)]:
    print(len(S))
    exit()

T = 10**10

for i in range(len(S)-1):
    if S[i]!=S[i+1]:
        T = min(T, max(i+1, len(S)-i-1))

print(T)

Submission status: Accepted

Solution code:
S=input()
N=len(S)

b=[int(S[i]) for i in range(N)]
for i in range(1,N):
    b[i]+=b[i-1]
black=[0 for i in range(0,N+1)]
for i in range(0,N):
    black[i+1]=b[i]

def condition(K):
    if 2*K-1<N:
        return True
    else:
        if black[K]-black[N-K]==0 or black[K]-black[N-K]==2*K-N:
            return True
        else:
            return False

start=1
end=N
while end-start>1:
    test=(end+start)//2
    if condition(test):
        start=test
    else:
        end=test

if condition(end):
    print(end)
else:
    print(start)

Submission status: Accepted

Solution code:
#dame 
def main():
    S = input()
    r = len(S)
    if S.count('0') == r:
        return r
    if S.count('1') == r:
        return r
    N = len(S)
    one = S[0] == '1'
    c = 0
    for i in range(N):
        if (S[i] == '1' and one) or (S[i] == '0' and not one):
            continue
        r = min(r, max(i, N-i))
        one = S[i] == '1'
    return r
print(main())

Submission status: Accepted

Solution code:
def main():
    S = input()
    A = []
    i, t = 1, S[0]
    for s in S[1:]:
        if s != t:
            A.append(i)
            i = 1
            t = s
        else:
            i += 1
    A.append(i)

    m, l  = len(S), 0
    for a in A:
        t = max(l + a, len(S) - l - a)
        m = min(m, t)
        l += a
    return m

print(main())

Submission status: Accepted

Solution code:
S = input()
N = len(S)
res = N
for i in range(N - 1):
    if S[i] != S[i + 1]:
        res = min(res, max(i + 1, N - (i + 1)))
print(res)

Submission status: Accepted

Solution code:
# solution

import io
import sys
import math

data = input()
n = len(data)
ans = n
for i in range(n - 1):
    if data[i] != data[i + 1]:
        ans = min(ans, max(i + 1, n - i - 1))
print(ans)

Submission status: Accepted

Solution code:
s = input()

half = (len(s)-1)//2

ans = half+1
c = s[half]
for x,y in zip(s[half+1:],s[half-(len(s)%2==1)::-1]):
    if x==c and y==c:
        ans += 1
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
# from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

s=input()
ls=len(s)
kijun=s[ls//2]
if ls%2==0:
    tmp=0
    for i in range(ls//2):
        if kijun==s[ls//2+i]==s[ls//2-1-i]:
            tmp+=1
        else:
            break
    print(ls//2+tmp)
else:
    tmp=1
    for i in range(ls//2):
        if kijun==s[ls//2+1+i]==s[ls//2-1-i]:
            tmp+=1
        else:
            break
    print(ls//2+tmp)

Submission status: Accepted

Solution code:
S = input()
MIN = 10 ** 8
flag = 1
for i in range(len(S)-1):
    if S[i] != S[i+1]:
        flag = 0
        MIN = min(MIN, max(i + 1, len(S) - i - 1))
if flag:
    MIN = len(S)
print(MIN)

Submission status: Accepted

Solution code:
s = input()

n = len(s)
ans = n
for i in range(1, n):
    if s[i - 1] != s[i]:
        # left:  [0, i) -> length = i
        # right: [i, n) -> length = n - i
        ans = min(ans, max(i, n - i))

print(ans)

Submission status: Accepted

Solution code:
s = [int(i) for i in input()]
n = len(s)
ans = n
for i in range(n-1):
  if s[i] != s[i+1]:
    ans = min(ans, max(i+1, n-i-1))
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
l = len(s)
if l % 2 == 0:
    for i in range(l//2):
        a = s[l//2]
        if s[l//2 - 1 - i] != a or s[l//2 + i] != a:
            print(l//2 + i)
            exit()
else:
    for i in range(1, (l-1)//2 + 1):
        a = s[(l-1)//2]
        if s[(l-1)//2 - i] != a or s[(l-1)//2 + i] != a:
            print((l-1)//2 + i)
            exit()
print(l)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(S: str):
    N = len(S)
    m = N
    for i in range(N - 1):
        if S[i] != S[i + 1]:
            m = min(m, max(N - (i + 1), i + 1))
    print(m)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  # type: str
    solve(S)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def find_cont(S):
    N=len(S)
    res=N
    for i in range(N-1):
        if S[i]!=S[i+1]:
            res=min([res,max(i+1,N-1-i)])
    return res
S=input()
print(find_cont(S))

Submission status: Accepted

Solution code:
s=input()
n=len(s)
ans=n
for i in range(n-1):
  if s[i+1]!=s[i]:   
    ans=min(ans,max(i+1,n-i-1))
print(ans)

Submission status: Accepted

Solution code:


#!/usr/bin/env python

# -*- coding: utf-8 -*-



s = input()

if len(s) % 2 == 0:
    number = s[len(s) // 2]
    other = s[len(s) // 2 - 1]
    p = len(s) // 2
    cnt = 0
    if number == other:
        cnt += 1
        for i in range(1, p):
            if s[p + i] == s[p - 1 - i] == number:
                cnt += 1
            else:
                break
    print(p + cnt)


else:
    number = s[len(s) // 2]
    cnt = 0
    p = len(s) // 2

    for i in range(1, len(s) // 2 + 1):
        if s[p - i] == s[p + i] == number:
            cnt += 1
        else:
            break
    print(p + 1 + cnt)

Submission status: Accepted

Solution code:
from collections import deque

x, y = deque(), deque()
s = deque(list(input()))
n = len(s)
m = s[n // 2]
ans = (n + 1) // 2
for _ in range((n + 1) // 2):
    x.appendleft(s.popleft())
for _ in range(n // 2):
    y.appendleft(s.pop())
if n % 2 == 1:
    x.popleft()
for i in range(n // 2):
    if x[i] == y[i] == m:
        ans += 1
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
#初めは0と1で多い方だと思った
#正確にやってはないけど連続する0or1より長くないとだめ
#上限が何かを考える
s=input()
l=len(s)

ans=1000000000000
for i in range(l-1):
    if s[i]!=s[i+1]:
        ne=max(i+1,l-i-1)
        ans=min(ans,ne)
if ans==1000000000000:
    print(l)
else:
    print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
ans = N
for i in range(N - 1):
    if S[i] != S[i + 1]:
        ans = min(ans, max(i + 1, N - (i + 1)))
print(ans)

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.buffer.readline
 
def main():
    s = input()
    l = len(s)
    if "0" not in s or "1" not in s:
        print(l)
    else:
        ans = 10**6
        for i in range(l-1):
            if s[i] != s[i+1]:
                ans = min(ans,max(i+1,l-i-1))
        print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S = input()
l = len(S)
ans = l
"""
if len(set(list(S))) == 1 and S[0] == "0":
    print(0)
    exit()
"""
for i in range(l-1):
    if S[i] != S[i+1]:
        ans = min(ans,max(l-i-1,i+1))
print(ans)

Submission status: Accepted

Solution code:
import math
def f(s):
    if len(s)%2==0: p=len(s)//2-1
    else: p=len(s)//2
    
    q=p
    while  q<len(s) and s[p]==s[q]:
        q+=1
    return q



s=input()
l=len(s)
ans=l
for i in range(len(s)-1):
    if s[i]!=s[i+1]:
        ans=min(ans, max(i+1,l-i-1) )
print(ans)

Submission status: Accepted

Solution code:
import math
def f(s):
    if len(s)%2==0: p=len(s)//2-1
    else: p=len(s)//2
    
    q=p
    while  q<len(s) and s[p]==s[q]:
        q+=1
    return q



s=input()

print(min(f(s),f(s[::-1])))

Submission status: Accepted

Solution code:
s = input()

l = len(s)
center = s[l // 2]
cnt = l % 2
for e1, e2 in zip(s[:l//2][::-1], s[l//2+l%2:]):
    if center == e1 == e2:
        cnt += 2
    else:
        break

ans = (l + cnt) // 2
print(ans)

Submission status: Accepted

Solution code:
s = input()

l = len(s)
ans = l
for i, (e1, e2) in enumerate(zip(s, s[1:]), 1):
    if e1 != e2:
        ans = min(ans, max(i, l - i))

print(ans)

Submission status: Accepted

Solution code:
S=input()
n=len(S)
if n%2:
    m=n//2
    t=S[m]
    for i in range(m):
        if S[m+i+1]!=t or S[m-i-1]!=t:
            a=m+i+1
            break
    else:
        a=n
else:
    m=n//2
    t=S[m]
    for i in range(m):
        if S[m+i]!=t or S[m-i-1]!=t:
            a=m+i
            break
    else:
        a=n
print(a)

Submission status: Accepted

Solution code:
s=list(input())
l=[len(s)]
for i in range(len(s)-1):
  if s[i]!=s[i+1]:
    l.append(max(i+1,len(s)-i-1))
print(min(l))

Submission status: Accepted

Solution code:
s = str(input())
n = len(s)

if n == 1:
    print(1)
    exit()

maxs = []
for i in range(n-1):
    if s[i] != s[i+1]:
        maxs.append(max(i+1, n-i-1))

if maxs != []:
    print(min(maxs))
else:
    print(n)

Submission status: Accepted

Solution code:
from collections import deque

S=input()
N=len(S)

prev=S[0]
ans=N
temp=0

con=deque()

for i in range(N):
    if S[i]==prev:
        temp+=1
    else:
        con.append(temp)
        prev=S[i]
        temp=1
con.append(temp)       
        
        
    
while len(con)>2:
    a=con[0]
    b=con[-1]
    if b>=a:
        c=con.popleft()
        con[0]+=c
        ans=min(ans,N-a)
    else:
        c=con.pop()
        con[-1]+=c
        ans=min(ans,N-b)
    
    
    
if len(con)==2:
    ans=min(ans,max(con))
else:
    ans=N
    
    
print(ans)

Submission status: Accepted

Solution code:
def main():
  S = input()
  j = 0
  m = S[0]
  ls = []
  for i in range(len(S)):
    if m!=S[i]:
      ls.append(i-j)
      j = i
      m = S[i]
  ls.append(len(S)-j)
  N = len(ls)
  if N<3:
    print(max(ls))
  else:
    ans = float('inf')
    sls = [ls[0]]
    for i in range(1,N):
      sls += [sls[-1]+ls[i]]
    ans = min(max(sls[i], sls[-1]-sls[i]) for i in range(N-1))
    print(ans)

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
S = input()
j = 0
m = S[0]
ls = []
for i in range(len(S)):
  if m!=S[i]:
    ls += [i-j]
    j = i
    m = S[i]
ls += [len(S)-j]
N = len(ls)
if N<3:
  print(max(ls))
else:
  ans = float('inf')
  sls = [ls[0]]
  for i in range(1,N):
    sls += [sls[-1]+ls[i]]
  for i in range(N-1):
    ans = min(ans,max(sls[i], sls[-1]-sls[i]))
  print(ans)

Submission status: Accepted

Solution code:
s=list(str(input()))
n=len(s)
ans=10**10
for i in range(n-1):
    if s[i]!=s[i+1]:
        ans=min(ans,max(i+1,n-i-1))

if ans==10**10:
    print(n)
else:
    print(ans)

Submission status: Accepted

Solution code:
S = input() + '#'

pressed = []

prev = S[0]
count = 0
for s in S:
    if s == prev:
        count += 1
    else:
        prev = s
        pressed.append(count)
        count = 1

left_cumsum = []
right_cumsum = []

cumsum = 0
for count in pressed:
    cumsum += count
    left_cumsum.append(cumsum)

cumsum = 0
for count in pressed[::-1]:
    cumsum += count
    right_cumsum.append(cumsum)

length = len(left_cumsum)

if length == 1:
    print(len(S)-1)
    exit()

change_k = [0] * length

for idx in range(1, length):
    k = right_cumsum[-idx-1]
    change_k[idx-1] = k

for idx in range(1, length):
    k = left_cumsum[-idx-1]
    change_k[-idx] = max(change_k[-idx], k)

ans1 = min(change_k[::2])
ans2 = min(change_k[1::2])
print(max(ans1, ans2))

Submission status: Accepted

Solution code:
# D - Wide Flip

S = input()

N = len(S)
ans = N

if N == 1:
    print(1)
else:
    for i in range(N-1):
        if S[i] != S[i+1]:
            ans = min(ans, max(i+1, N-(i+1)))
    print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
k = 10**9
if not "1" in s:
    print(n)
elif not "0" in s:
    print(n)
else:
    for i in range(n-1):
        if s[i] != s[i+1]:
            k = min(k,max(i+1,n-(i+1)))
    if k > n:
        print(n)
    else:
        print(k)

Submission status: Accepted

Solution code:
from itertools import groupby
S = input()
N = len(S)
G = [len(list(l)) for _, l in groupby(S)]
A = 0
for g in G:
    A += g
    if A >= (N+1) // 2:
        ans = min(A, N-A+g)
        print(ans)
        break

Submission status: Accepted

Solution code:
S = input()
N = len(S)

if len(set(S))==1:
  print(N)
  exit()

K = (N+1)//2
X = set()
if N&1:
  X = {S[K-1]}
  l = K-2
  r = K
else:
  X = set()
  l = K-1
  r = K
  
while l:
  X.add(S[l])
  X.add(S[r])
  if len(X)>1: break
  K += 1
  l -= 1
  r += 1

print(K)

Submission status: Accepted

Solution code:
s = input()
ans = len(s)
for i in range(len(s)-1):
  if s[i] != s[i+1]:
    ans = min(ans, max(i+1, len(s)-i-1))
print(ans)

Submission status: Accepted

Solution code:
s=input()
n=len(s)
ans=n
for i in range(n-1):
    if s[i]!=s[i+1]:
        t=max(i+1,n-i-1)
        ans=min(ans,t)

print(ans)

Submission status: Accepted

Solution code:
"""import sys
input = sys.stdin.readline"""
S=input()
t=10**6
for i in range(1,len(S)):
    if S[i-1]!=S[i]:
        t=min(t,max(i,len(S)-i))
#print(t)
if t==10**6:
    print(len(S))
else:
    print(t)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
ans = (n+1)//2
if n%2:
    x = s[n//2]
    for i in range(1,n//2+1):
        if s[n//2-i] == s[n//2+i] == x:
            ans += 1
        else:
            break
if n%2==0:
    if s[n//2] == s[n//2-1]:
        ans += 1
        x = s[n//2]
        for i in range(1,n//2):
            if s[n//2-1-i] == s[n//2+i] == x:
                ans += 1
            else:
                break
print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby, accumulate

S = list(input())
n = len(S)
s = list(accumulate([len(list(j)) for i, j in groupby(S)]))
s = [max(i, n - i) for i in s]

print(min(s))

Submission status: Accepted

Solution code:

from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


# show_flg = False
show_flg = True


def main():
    S = input()
    center = len(S) // 2

    if len(S) % 2 == 0:
        c = 1
        while True:
            if (center - 1 + c) < len(S) and S[center - c] == S[center - 1 + c] and S[center - c] == S[center]:
                # if S[center - c] == S[center]:
                # print(c, center - c, center - 1 + c, S[center - c], S[center])
                c += 1
            else:
                # print(c, S[center - c], S[center - 1 + c])
                # c -= 1
                break
        print(c + center - 1)
    else:
        c = 0
        while True:
            if (center + c) < len(S) and S[center - c] == S[center + c] and S[center] == S[center - c]:
                c += 1
            else:
                break
        print(c + center)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

s = sys.stdin.readline().rstrip()
n = len(s)

def main():
    m = n // 2
    if n & 1:
        l = s[m::-1]
        r = s[m:]
    else:
        l = s[m-1::-1]
        r = s[m:]
    base = l[0]

    cnt = m
    i = 0
    if n & 1:
        m += 1
    while i < m:
        if l[i] == r[i] == base:
            i += 1
            cnt += 1
        else:
            break
    return cnt
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
s = input()
lns = len(s)
r = lns
for i, (b, c) in enumerate(zip(s, s[1:])):
    if b != c:
        r = min(r, max(i+1, lns-i-1))
print(r)

Submission status: Accepted

Solution code:

S = input()

z = 0
o = 0

for i in S:
    if i == "0":
        z += 1
    else:
        o += 1

for i in range(len(S)):

    if o == 0 or z == 0:
        print (len(S) - i)
        break

    if S[i] == "0":
        z -= 1
    else:
        o -= 1

    if S[-1-i] == "0":
        z -= 1
    else:
        o -= 1

Submission status: Accepted

Solution code:
import numpy as np
S=str(input())
X=[]
c=1
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    c +=1
  elif S[i]!=S[i-1]:
    X +=[c]
    c=1
  if i==len(S)-1:
    X +=[c]
if len(S)==1:
  X=[1]
if len(X)>=3:
  X=np.cumsum(X)
  t=float('inf')
  for i in range(len(X)-1):
    u=max(X[i],len(S)-X[i])
    t=min(u,t)
  answer=t
elif len(X)==1:
  answer=X[0]
elif len(X)==2:
  answer=max(X)
print(answer)

Submission status: Accepted

Solution code:
s=input()
n=len(s)
if n % 2 == 1:
    hn = n//2
    if s[hn] =="1":
        s=s.replace("1","p").replace("0","1").replace("p","0")
    res = 0
    while res <= hn and s[hn+res] == "0" and s[hn-res] == "0":
        res += 1
    print(res+hn if res != 0 else hn+1)
else:
    hn = n//2
    res = 0
    if s[hn] =="1" and s[hn-1]=="1":
        s=s.replace("1","p").replace("0","1").replace("p","0")
    while res < hn and s[hn+res] == "0" and s[hn-res-1] == "0":
        res += 1
    print(res+hn)