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: Wrong Answer

Solution code:
s = list(input())

ans = min(1, len(s) // 2)
for i in range(ans, len(s)):
    m_b = len(s) - i
    m_e = i
    is_ok = True
    if m_b <= m_e:
        prev = -1
        for j in range(m_b, m_e):
            if prev == -1:
                prev = s[j]
            else:
                if prev != s[j]:
                    is_ok = False
                    break
    if is_ok:
        ans = i
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
s=input()
j=0
x=[]
for i in range(len(s)):
 if s[i]=="1":
    if len(x)>0:
        x[-1].append(j)
    x.append([j])
    j=0
    continue
    
 j+=1
if len(x)>0 and len(x[-1])==1:
        x[-1].append(j)
        x.append([j,0])
b=float("inf")
for i in range(len(x)):
 if x[i][0]>0:
    if i==0 or i==len(x)-1:b=min(b,x[i][0])
    b=min(b,x[i][0]+1)
      
 if x[i][0]>0 and x[i][1]>0:
    b=max(b,min(x[i][0],x[i][1])+1)
 
print(b)

Submission status: Wrong Answer

Solution code:
S = input()
if S[0] == '1':
    S = ''.join('0' if S[i] == '1' else '1' for i in range(len(S)))

K = float('inf')
LENL, LENONE, LENR = 0, 0, 0
L, ONE, R = 'left', 'one', 'right'
POS = L
for i in range(len(S)):
    if POS == L:
        if S[i] == '0':
            LENL += 1
        else:
            LENONE = 1
            POS = ONE

    elif POS == ONE:
        if S[i] == '1':
            LENONE += 1
        else:
            LENR = 1
            POS = R

    elif POS == R:
        if S[i] == '0':
            LENR += 1
        else:
            K = min(K, max(LENL + LENONE, LENONE + LENR))
            LENONE = 1
            LENL = i  # LENR
            POS = ONE
if POS == R:
    K = min(K, LENONE + LENR)
else:
    K = min(K, max(LENL, LENONE))
print(K)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
s=input()
j=0
x=[]
for i in range(len(s)):
 if s[i]=="1":
    if len(x)>0:
        x[-1].append(j)
    x.append([j])
    j=0
    continue
    
 j+=1
if len(x)>0 and len(x[-1])==1:
        x[-1].append(j)
        x.append([j,0])
b=float("inf")
for i in range(len(x)):
 if x[i][0]>0:
    if i==0:b=min(b,x[i][0])
    b=min(b,x[i][0]+1)
      
 if x[i][0]>0 and x[i][1]>0:
    b=max(b,min(x[i][0],x[i][1])+1)
 
print(b)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = []
pr = s[0]
cnt = 0
for i in s:
    if i==pr:
        cnt+=1
    else:
        a.append(cnt)
        pr = i
        cnt = 1
if cnt:
    a.append(cnt)

if len(a)<=2:
    print max(a)
else:
    a.append(0)
    b = []
    l = len(a)
    for i in xrange(l-1):
        b.append(a[i] + a[i+1])
    b.sort()
    print max(b)

Submission status: Wrong Answer

Solution code:
def solve(x):
    current = 0
    l = []
    for i, c in enumerate(x):
        if c != current:
            l.append(i)
            current = c

    if len(l) == 1:
        print(len(x))
        return
    if len(l) == 2:
        print(max(len(x) - l[1], l[1] - l[0]))
        return
    
    l.append(len(x))
    min_ = len(x)
    for start, end in zip(l, l[2:]):
        min_ = min(min_, end - start)

    print(min_)


if __name__ == '__main__':
    x = input()
    solve(x)
    # solve('010')
    # solve('100000000')
    # solve('001100')
    # solve('111')
    # solve('11001')

Submission status: Wrong Answer

Solution code:
S = list(map(int, list(input())))
lenS = len(S)
count0 = S.count(0)
if count0 == 0 or count0 == len(S):
	print(count0)
	exit()

def reverse(A, l, n):
	for x in range(n):
		A[l+x] = 1 - A[l+x]

L,R = 2,lenS-1
while L < R:
	K = int((L+R+1)/2)
	test = [i for i in S]
	for i in range(lenS - K + 1):
		if test[i] == 1:
			reverse(test, i, K)
	flag = True
	for i in range(K):
		if test[i] == 1:
			flag = False
			break
	if flag:
		L = K
	else:
		R = K - 1
		S = test
print(L)

Submission status: Wrong Answer

Solution code:
import sys
line = sys.stdin.readline().strip()

l = [0]

st = line[0]
for s in line:
    if not st is s:
        st = s
        l.append(0)
    l[-1] += 1
if len(l) <= 2:
    print(max(l))
else:
    print(max( l[i] + l[i+1] for i in range(len(l)-1) ))

Submission status: Wrong Answer

Solution code:
s = list(input())

ans = len(s) // 2
for i in range(len(s) // 2, len(s)):
    m_b = len(s) - i
    m_e = i
    if m_b > m_e:
        continue
    prev = -1
    is_ok = True
    for j in range(m_b, m_e):
        if prev == -1:
            prev = s[j]
        else:
            if prev != s[j]:
                is_ok = False
    if is_ok:
        ans = i
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = []
pr = s[0]
cnt = 0
for i in s:
    if i==pr:
        cnt+=1
    else:
        a.append(cnt)
        pr = i
        cnt = 1
if cnt:
    a.append(cnt)

if len(a)<=2:
    print max(a)
else:
    a.append(0)
    b = []
    l = len(a)
    for i in xrange(l-1):
        b.append(a[i] + a[i+1])
    b.sort()
    if b[-1]==b[-2]:
        print b[-2]
    else:
        for i in xrange(l-1):
            if a[i] + a[i+1] == b[-1]:
                break
        ma = b[-1]
        ans = b[-2]
        if sum(a[:i+1]) > a[i+1] or sum(a[i+1:]) > a[i]:
            ans = b[-1]
        print ans

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = []
pr = s[0]
cnt = 0
for i in s:
    if i==pr:
        cnt+=1
    else:
        a.append(cnt)
        pr = i
        cnt = 1
if cnt:
    a.append(cnt)

if len(a)<=2:
    print max(a)
else:
    a.append(0)
    b = []
    l = len(a)
    for i in xrange(l-1):
        b.append(a[i] + a[i+1])
    b.sort()
    if b[-1]==b[-2]:
        print b[-2]
    else:
        for i in xrange(l-1):
            if a[i] + a[i+1] == b[-1]:
                break
        ma = b[-1]
        ans = b[-2]
        if sum(a[:i+1]) > ma or sum(a[i+1:]) > ma:
            ans = b[-1]
        print ans

Submission status: Wrong Answer

Solution code:
import sys
line = sys.stdin.readline().strip()

l = [0]

st = line[0]
for s in line:
    if not st is s:
        st = s
        l.append(0)
    l[-1] += 1
if len(l) <= 2:
    print(max(l))
else:
    print(min( l[i] + l[i+1] for i in range(len(l)-1) ))

Submission status: Wrong Answer

Solution code:
s = list(input())
for i in range(0, len(s)//2):
    if len(set(s[i:-i])) == 1:
        print(len(s) - i)
        quit()
print(len(s)//2)

Submission status: Wrong Answer

Solution code:
S = raw_input()
A = [0]
for i in range(len(S)-1):
  if S[i]!=S[i+1]:
    A += [i+1]
A += [len(S)]
B = [A[i+1]-A[i] for i in range(len(A)-1)]


def solve(B):
  x = sum(B[:len(B)/2+1])
  i = 0
  ans = x
  while len(B)/2+1+i<len(B):
    x -= B[i]
    x += B[len(B)/2+1+i]
    ans = min(ans, x)
    i += 1
  return ans

if len(B)==1:
  print B[0]
elif len(B)==2:
  print max(B)
elif len(B)%2==0:
  print max(solve(B[1:]), solve(B[:-1]))
else:
  print solve(B)

Submission status: Wrong Answer

Solution code:
arr = []
prev = None
num = 0
for c in input():
  if prev is not None and c != prev:
    arr.append(num)
    num = 0
  prev = c
  num += 1
arr.append(num)

if len(arr) <= 2:
  print(max(arr))
else:
  print(max(max(arr[0], arr[-1]), max(arr[1:-1])+1))

Submission status: Wrong Answer

Solution code:
S = raw_input()
A = [0]
for i in range(len(S)-1):
  if S[i]!=S[i+1]:
    A += [i+1]
A += [len(S)]
B = [A[i+1]-A[i] for i in range(len(A)-1)]

def solve(B):
  l = B[0]+B[1]
  r = sum(B)-B[0]
  ans = min(l,r)
  for i in range(2,len(B)-1):
    l += B[i]
    r -= B[i]
    ans = max(ans, min(l,r))
  return ans

if len(B)==1:
  print len(S)
elif len(B)==2:
  print max(B)
elif len(B)%2==0:
  print max(solve(B[1:]), solve(B[:-1]))
else:
  print solve(B)

Submission status: Wrong Answer

Solution code:
Ss = input()

Ns = []
prev = Ss[0]
num = 0
for S in Ss:
    if S != prev:
        Ns += [num]
        num = 1
    else:
        num += 1
    prev = S

Ns += [num]

L = (len(Ns) + 1) // 2

if L == 1:
    print(max(Ns))
    exit()

ans = float('inf')
for i in range(len(Ns) - L + 1):
    ans = min(ans, sum(Ns[i:i + L]))

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = []
pr = s[0]
cnt = 0
for i in s:
    if i==pr:
        cnt+=1
    else:
        a.append(cnt)
        pr = i
        cnt = 1
if cnt:
    a.append(cnt)

if len(a)<=2:
    print max(a)
else:
    a.append(0)
    b = []
    l = len(a)
    for i in xrange(l-1):
        b.append(a[i] + a[i+1])
    b.sort()
    print max(b[-2],max(a))

Submission status: Wrong Answer

Solution code:
S = input()
idx = 0
cnts = []
while idx < len(S):
    char = S[idx]
    cnt = 0
    while idx < len(S) and S[idx] == char:
        idx += 1
        cnt += 1
    cnts.append(cnt)
if len(cnts) <= 2:
    print(max(cnts))
    quit()
ans = (len(S) + 1) // 2
for i in range(len(cnts) - 2):
    ans = max(ans, min(cnts[i] + cnts[i + 1], cnts[i + 1] + cnts[i + 2]))
print(ans)

Submission status: Wrong Answer

Solution code:
S=raw_input()
flip=0
maxsize=1
tmp=1
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        flip+=1
        maxsize=max(maxsize, tmp)
        tmp=1
    else:
        tmp+=1
ans=max(maxsize, tmp)
if flip==0:
    ans=len(S)
if flip>1:
    ans+=1
print ans

Submission status: Wrong Answer

Solution code:
import sys
s = list(str(input().rstrip()))
s = list(map(int,s))

change = 0
for i in range(len(s)-1):
  if s[i]!=s[i+1]: change+=1
if change==0:
  print(len(s))
  sys.exit()
elif change ==1:
  ones = s.count(1)
  zeros = s.count(0)
  print(max(ones,zeros))
  sys.exit()
  
renban=len(s)
flag=s[0]
conti=1
for i in range(0,len(s)-1):
  if s[i+1]==flag:
    conti+=1
  else:
    flag=s[i+1]
    renban=min(renban,conti)
    conti=1
print(renban+1)

Submission status: Wrong Answer

Solution code:

def main(S):
    con = []
    cnt = 1
    now = S[0]

    for c in S[1:]:
        if now != c:
            con.append(cnt)
            cnt = 0
        now = c
        cnt += 1

    con.append(cnt)

    # for i in range(len(con) - 2):
    #     # print(i, con[i:i+3])
    #     if con[i:i + 3] == [1, 1, 1]:
    #         print('2')
    #         return

    # print(con)
    ans = [x for x in con if x > 2]
    if ans:
        print(max(ans))
    else:
        print('2')


S = input()
main(S)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = []
pr = s[0]
cnt = 0
for i in s:
    if i==pr:
        cnt+=1
    else:
        a.append(cnt)
        pr = i
        cnt = 1
if cnt:
    a.append(cnt)

if len(a)<=2:
    print max(a)
else:
    a.append(0)
    b = []
    l = len(a)
    for i in xrange(l-1):
        b.append(a[i] + a[i+1])
    b.sort()
    print b[-2]

Submission status: Wrong Answer

Solution code:

def main(S):
    con = []
    cnt = 1
    now = S[0]

    for c in S[1:]:
        if now != c:
            con.append(cnt)
            cnt = 0
        now = c
        cnt += 1

    con.append(cnt)

    # for i in range(len(con) - 2):
    #     # print(i, con[i:i+3])
    #     if con[i:i + 3] == [1, 1, 1]:
    #         print('2')
    #         return

    # print(con)
    ans = [x for x in con if x > 2]
    if ans:
        print(min(ans))
    else:
        print('2')


S = input()
main(S)

Submission status: Wrong Answer

Solution code:
s = input()

zero = s.count('0')
one = s.count('1')

print(max(zero, one))

Submission status: Wrong Answer

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

import re

def main():
    S = raw_input()
    pat = [re.compile(ch+'+') for ch in '01']
    X = []
    i = 0 if S[0] == '0' else 1
    m = pat[i].search(S)
    while m is not None:
        X.append(m.end()-m.start())
        i ^= 1
        m = pat[i].search(S, pos=m.end())

    if len(X) <= 2:
        print max(X[0], X[-1])
        return

    a = X[1:]
    b = X[:-1]

    res = 0
    for i, j in zip(a, b):
        res = max(res, i+j)

    print res

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
idx = 0
cnts = []
while idx < len(S):
    char = S[idx]
    cnt = 0
    while idx < len(S) and S[idx] == char:
        idx += 1
        cnt += 1
    cnts.append(cnt)
if len(cnts) <= 2:
    print(max(cnts))
    quit()
ans = 2
for i in range(len(cnts) - 2):
    ans = max(ans, min(cnts[i] + cnts[i + 1], cnts[i + 1] + cnts[i + 2]))
print(ans)

Submission status: Wrong Answer

Solution code:
'''input
010
'''

def list_input():
    return list(map(int,input().split()))
def map_input():
    return map(int,input().split())
def map_string():
    return input().split()
    
s = input()
n = len(s)
cnt = 0
for i in s:
	if i == '0': cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
s = list(str(input().rstrip()))
s = list(map(int,s))

change = 0
for i in range(len(s)-1):
  if s[i]!=s[i+1]: change+=1
if change==0:
  print(len(s))
  sys.exit()
elif change ==1:
  ones = s.count(1)
  zeros = s.count(0)
  print(max(ones,zeros))
  sys.exit()

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = []
pr = s[0]
cnt = 0
for i in s:
    if i==pr:
        cnt+=1
    else:
        a.append(cnt)
        pr = i
        cnt = 1
if cnt:
    a.append(cnt)

if len(a)<=2:
    print max(a)
else:
    a.sort()
    print a[-2]

Submission status: Wrong Answer

Solution code:
def solve(s):
    n = len(s)

    if n == 1:
        return 1
    if n == 2:
        return 2 if s[0] == s[1] else 1

    ci = n // 2
    c = s[ci]
    if n & 1:
        for di in range(1, ci + 1):
            if c == s[ci - di] == s[ci + di]:
                pass
            else:
                return ci + di
        return n
    if c != s[ci - 1]:
        return ci
    for di in range(1, ci):
        if c == s[ci - di - 1] == s[ci + di]:
            pass
        else:
            return ci + di
        return n


print(solve(input()))

Submission status: Wrong Answer

Solution code:
S = input()
if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2 if S[0] == S[1] else 1)
else:
    ans = 0
    idx = 0
    while idx < len(S):
        char = S[idx]
        cnt = 0
        while idx < len(S) and S[idx] == char:
            idx += 1
            cnt += 1
        ans = max(ans, cnt)
    print(max(2, ans))

Submission status: Wrong Answer

Solution code:
S = input()
print(max(S.count('0'), S.count('1')))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

line = sys.stdin.readline()

st = ''
ans = 100000
cnt = 0
ii = False
for i,s in enumerate(line):
    cnt += 1
    if not ii:
        ii = True
        st = s
        continue
    if not s is st:
        ans = cnt
        cnt = 0
        ii = False

print(ans)

Submission status: Wrong Answer

Solution code:
def solve(s):
    n = len(s)

    if n == 1:
        return 1
    if n == 2:
        return 2 if s[0] == s[1] else 1

    ci = n // 2
    c = s[ci]
    if n & 1:
        for di in range(1, ci + 1):
            if c == s[ci - di] == s[ci + di]:
                pass
            else:
                return ci + di
    if c != s[ci - 1]:
        return ci
    for di in range(1, ci):
        if c == s[ci - di - 1] == s[ci + di]:
            pass
        else:
            return ci + di


print(solve(input()))

Submission status: Wrong Answer

Solution code:
s = input().strip()
n = len(s)
 
ans = n
for i, c in enumerate(s):
  ld, rd = i, n - i - 1
  if c == '1':
  	ans = min(ans, max(ld, rd))
print(ans)

Submission status: Wrong Answer

Solution code:
s = str(input().rstrip())
lens = len(s)
zeros = s.count("0")
ones = s.count("1")
print(lens - min(zeros,ones))

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = []
pr = s[0]
cnt = 0
for i in s:
    if i==pr:
        cnt+=1
    else:
        a.append(cnt)
        pr = i
        cnt = 1
if cnt:
    a.append(cnt)

if len(a)<=2:
    print max(a)
else:
    cur = len(s) + 5
    l = len(a)
    for i in xrange(l-1):
        cur = min(cur,a[i] + a[i+1])
    print cur

Submission status: Wrong Answer

Solution code:
S = input()

one = [len(x) for x in S.split('0') if x]
zero = [len(x) for x in S.split('1') if x]

one = [x for x in one if x > 1]
zero = [x for x in zero if x > 1]
if len(one + zero) == 0:
    print('2')
else:
    print(max(one + zero))

Submission status: Wrong Answer

Solution code:
s = str(input().rstrip())
zeros = s.count("0")
ones = s.count("1")
print(max(zeros,ones))

Submission status: Wrong Answer

Solution code:
'''input
100000000
'''

def list_input():
    return list(map(int,input().split()))
def map_input():
    return map(int,input().split())
def map_string():
    return input().split()
    
s = input()
n = len(s)
cnt = 0
for i in s:
	if i == '0': cnt += 1
print(max(cnt,n-cnt))

Submission status: Time Limit Exceeded

Solution code:
S = list(map(int,list(input())))
l = len(S)
maxnum = l
while (1 in S) and (0 in S):
  get=[]
  for lis in S,S[::-1]:
    a = lis[0]
    count = 0
    for item in lis:
      if item == a:
        count += 1
      else:
        get.append(l - count)
        break
  target = max(get)
  i = 0
  if get.index(target) == 1:
    while i < target:
      if S[i] == 0:
        S[i] = 1
      else:
        S[i] = 0
      i += 1
  else:
    while i < target:
      if S[l-i-1] == 1:
        S[l-i-1] = 0
      else:
        S[l-i-1] = 1
      i += 1
  maxnum = min(maxnum,target)
print (maxnum)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from itertools import product
from operator import xor
from functools import reduce
 
rS = input()
S = int(rS, 2)
 
L = len(rS)
k = len(rS)
 
dp = set([0])
while True:
    A = [sum([2**i for i in range(I, I+k)]) for I in range(L-k+1)]
    B = set([reduce(xor, A_) for i in range(0, L-k+1) for A_ in product(A, repeat=i+1)])
    dp.update([d^b for d, b in product(dp, B)])
    if S in dp:
        break
    else:
        #print(dp)
        k -= 1
print(k)

Submission status: Time Limit Exceeded

Solution code:
S=raw_input()
flip=0
maxsize=1
tmp=1
A=[]
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        A.append(tmp)
        tmp=1
    else:
        tmp+=1
A.append(tmp)
ans=len(S)
if len(A)==1:
    print A[0]
elif len(A)==2:
    print max(A)
else:
    while(len(A)>2):
        sA=sum(A)
        mid=sA-A[0]-A[-1]
        if mid==max(mid,A[0],A[-1]):
            ans=min(ans,mid+min(A[0],A[-1]))
            if A[0]>A[-1]:
                A=[A[0]+A[1]]+A[2:]
            else:
                A=A[:-2]+[A[-1]+A[-2]]
        else:
            ans=min(ans,mid+max(A[0],A[-1]))
            if A[0]>A[-1]:
                A=A[:-2]+[A[-1]+A[-2]]
            else:
                A=[A[0]+A[1]]+A[2:]
    print min(max(A),ans)

Submission status: Time Limit Exceeded

Solution code:
# 無限回試行できる時、Kの最小値を最大化する問題
s = input()
now = s[0]
cnt = 1
use = []
for i in range(1,len(s)):
    if now == s[i]:
        cnt += 1
    else:
        use.append(cnt)
        cnt = 1
        now = s[i]
use.append(cnt)

min_prob = len(s)
sum_s = sum(use)
def ketsugou(l):
    global min_prob
    global sum_s
    #一番小さいやつをくっつけていく
    m = l.index(min(l))
    if m == 0 or m == len(l) - 1:
        s1 = l[m]
        s2 = sum_s - l[m]
        min_prob = min(min_prob, max(s1, s2))
        if m == 0:
            l.pop(0)
            l[0] += s1
        else:
            l.pop()
            l[-1] += s1
    else:
        s1 = sum(l[:m])
        s2 = s1 + l[m]
        s3 = sum_s - s1
        s4 = sum_s - s2
        min_prob = min(min_prob, max(s1, s2,s3,s4))
        if l[m - 1] < l[m + 1]:
            l[m - 1] += l[m]
        else:
            l[m + 1] += l[m]
        l.pop(m)
while(len(use) > 1):
    ketsugou(use)
print(min_prob)

Submission status: Time Limit Exceeded

Solution code:
# 無限回試行できる時、Kの最小値を最大化する問題
s = input()
now = s[0]
cnt = 1
use = []
for i in range(1,len(s)):
    if now == s[i]:
        cnt += 1
    else:
        use.append(cnt)
        cnt = 1
        now = s[i]
use.append(cnt)

min_prob = len(s)
def ketsugou(l):
    global min_prob
    #一番小さいやつをくっつけていく
    m = l.index(min(l))
    if m == 0 or m == len(l) - 1:
        s1 = l[m]
        s2 = sum(l) - l[m]
        min_prob = min(min_prob, max(s1, s2))
        if m == 0:
            l.pop(0)
            l[0] += s1
        else:
            l.pop()
            l[-1] += s1
    else:
        s1 = sum(l[:m + 1])
        s2 = sum(l[:m])
        s3 = sum(l[m:])
        s4 = sum(l[m+1:])
        min_prob = min(min_prob, max(s1, s2,s3,s4))
        if l[m - 1] < l[m + 1]:
            l[m - 1] += l[m]
        else:
            l[m + 1] += l[m]
        l.pop(m)
while(len(use) > 1):
    ketsugou(use)
print(min_prob)

Submission status: Time Limit Exceeded

Solution code:
arr = []
prev = None
num = 0
for c in input():
  if prev is not None and c != prev:
    arr.append(num)
    num = 0
  prev = c
  num += 1
arr.append(num)

while len(arr) > 2:
  if arr[0] > arr[-1]:
    arr = arr[:-2] + [arr[-2]+arr[-1]]
  else:
    arr = [arr[0]+arr[1]] + arr[2:]

print(max(arr))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from itertools import product
from operator import xor
from functools import reduce

rS = input()
S = int(rS, 2)

L = len(rS)
k = len(rS)

dp = set([0])
while True:
    A = [sum([2**i for i in range(I, I+k)]) for I in range(L-k+1)]
    B = set([reduce(xor, A_) for i in range(0, L-k+1) for A_ in product(A, repeat=i+1)])
    dp.update([d^b for d, b in product(dp, B)])
    if S in dp:
        break
    else:
        #print(dp)
        k -= 1
print(k)

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
sys.setrecursionlimit(2147483647)
import math
from heapq import heappush, heappop, heapify

def solve(s):
  n = len(s)
  k = n
  for i in range(n-1):
    if s[i] != s[i+1]:
      k = min(k, max(i+1, n-i-1))
  return k

def main():
  s = input()
  ans = solve(s)
  print(ans)
  

if __name__=='__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = [int(x) for x in input()]
s = len(S)
ans = float('inf')
for i in range(s-1):
    if S[i] != S[i+1]:
        ans = min(ans, max(i+1, s-i-1))

if ans == float('inf'):
    ans = s
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque, defaultdict
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations,product#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
from functools import reduce,lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal
from argparse import ArgumentParser

args=ArgumentParser()
args.add_argument('-loc', '--local', type=bool,default=False)
LOCAL=args.parse_args().local

def input(): 
    x=sys.stdin.readline(); 
    if x[-1]=="\n": 
        return x.rstrip() 
    return x
    
def printe(*x):_=print("## ",*x,file=sys.stderr) if LOCAL else 0
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): #xは64bit整数
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    #D - Wide Flip
    mod = 1000000007
    #w.sort(key=itemgetter(1),reverse=True)  #二個目の要素で降順並び替え

    #N = int(input())
    #N, K = map(int, input().split())
    #A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    s=input()
    ans=len(s)
    N=len(s)
    for i in range(len(s)-1):
        if s[i]!=s[i+1]:
            x=max(N-i-1,i+1)
            ans=min(ans,x)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = str(input())

n = len(s)

if len(set(list(s))) == 1:
    print(n)
    exit()

def is_ok(x):
    for i in range(n-x, x-1):
        if s[i] != s[i+1]:
            return False
    else:
        return True

l = 1
r = n
while l+1 < r:
    c = (l+r)//2
    if is_ok(c):
        l = c
    else:
        r = c
print(l)

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-1-i))
print(ans)

Submission status: Accepted

Solution code:
def solve():
  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))
  return ans
print(solve())

Submission status: Accepted

Solution code:
s=input()
n=len(s)
i=n//2
while i<n and s[i]==s[n-1-i] and s[i]==s[n//2]:
  i+=1
print(i)

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(n-i-1, i+1))

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

print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

S = readline().rstrip()
N = len(S)
ans = N
ans2 = N
for i in range(N):
    if S[i] == "1":
        ans = min(ans,max(N-i-1,i))
    else:
        ans2 = min(ans2,max(N-i-1,i))
print(max(ans,ans2))

Submission status: Accepted

Solution code:
S = str(input())
N = len(S)
ans = N//2

while ans < N:
  K = ans+1
  start = N-K
  end = K-1
  leng = end-start+1
  C = str(S[start:end+1])
  #print(start,end,C,"0"*leng,"1"*leng)
  if C == "0"*leng or C == "1"*leng:
    ans += 1
  else:
    #print("unko")
    break
print(ans)

Submission status: Accepted

Solution code:
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    s = input()
#    s = "1"*10**5
    n = len(s)
    if n&1:
        t = 1
        sf = n//2-1
        so = n//2+1
        ss = s[n//2]
        while sf >= 0:
            if s[so] == ss == s[sf]:
                t += 1
                sf -= 1
                so += 1
            else:
                break
        print(t+n//2)
    else:
        if s[n//2] == s[n//2-1]:
            ss = s[n//2]
            t = 1
            sf = n//2-2
            so = n//2+1
            while sf >= 0:
                if s[so] == ss == s[sf]:
                    t += 1
                    sf -= 1
                    so += 1
                else:
                    break
            print(n//2+t)
        else:
            print(n//2)


    return None

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()

if len(s) % 2 == 0:
    mid_l = len(s)//2 - 1
    mid_r = len(s)//2
    if s[mid_l] != s[mid_r]:
        print(len(s) // 2)
    else:
        mid_char = s[mid_l]
        for t in range(1, len(s)//2):
            if not (s[mid_l - t] == s[mid_r + t] == mid_char):
                print(mid_r+t)
                break
        else:
            print(len(s))
else:
    mid = len(s)//2
    mid_char = s[mid]
    for t in range(1, len(s)//2+1):
        if not (s[mid - t] == s[mid + t] == mid_char):
            print(mid+t)
            break
    else:
        print(len(s))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
# debug = lambda *a, **kw: print(*a, **kw, file=sys.stderr)

S = [int(x == "1") for x in ins()]
N = len(S)


def check(k):
    assert k < N
    if 2 * k - 1 <= N:
        return True
    a = S[k - 1]
    return all(S[i] == a for i in range(N - k, k))


def solve():
    if all(x == 0 for x in S):
        return N
    if all(x == 1 for x in S):
        return N

    ok, ng = 1, N
    while ng - ok > 1:
        m = (ok + ng) // 2
        if check(m):
            ok = m
        else:
            ng = m
    return ok


print(solve())