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:
from collections import Counter

item = str(input())
item = item + "*"
longest = [0,0]
shortest = [len(item),len(item)]
current = 1
for i in range(1, len(item)):
    if item[i-1] == item[i]:
        current = current + 1
    else:
        if longest[int(item[i-1])] < current:
            longest[int(item[i-1])] = current
        if shortest[int(item[i-1])] > current:
            shortest[int(item[i-1])] = current
        current = 1



if longest[0] + longest[1] == len(item)-1:
    print(longest[0] if longest[0] > longest[1] else longest[1])
else:
    print(shortest[0] + shortest[1])

Submission status: Wrong Answer

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

wk=length
bk=length
for i, s in enumerate(S):
    cost=max(i, length-(i+1))
    if s == "0":
        wk = min(wk, cost)
    else:
        bk = min(wk, cost)
print(max(wk, bk))

Submission status: Wrong Answer

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

wk=10**9
bk=10**9
for i, s in enumerate(S):
    cost=max(i, length-(i+1))
    if s == "0":
        wk = min(wk, cost)
    else:
        bk = min(wk, cost)
print(max(wk, bk))

Submission status: Wrong Answer

Solution code:
s, o = raw_input(), float('inf')
for k in xrange(len(s) - 1):
    if s[k] != s[k + 1]:
        o = min(o, max(k, len(s) - k - 1))
print o if o != float('inf') else len(s)

Submission status: Wrong Answer

Solution code:
s, o = ' ' + raw_input(), float('inf')
for k in xrange(1, len(s) - 1):
    if s[k] != s[k + 1]:
        o = min(o, max(k, len(s) - k - 1))
print o

Submission status: Wrong Answer

Solution code:
inp = input()
nStr = len(inp)
ret = nStr//2

if(inp[nStr//2] == "1"):
    for i in reversed(range((nStr-1)//2 + 1)):
        if((inp[i] == inp[nStr-i-1]) and (inp[i] == "1")):
            ret += 1
        else:
            print(ret)
            break
else:            
    for i in reversed(range((nStr-1)//2 + 1)):
        if((inp[i] == inp[nStr-i-1]) and (inp[i] == "0")):
            ret += 1
        else:
            print(ret)
            break

Submission status: Wrong Answer

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

S = input()
n = len(S)
T = []

for i in range(len(S)-1):
    if S[i] != S[i+1]:
        t = max(i+1, n-i-1)
        T.append(t)
if len(T) == 0:
    if S[0] == '1':
        print(1)
    else:
        print(0)
else:
    print(min(T))

Submission status: Wrong Answer

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

S = input()
n = len(S)
T = []

for i in range(len(S)-1):
    if S[i] != S[i+1]:
        t = max(i+1, n-i-1)
        T.append(t)
if len(T) == 0:
    print(0)
else:
    print(min(T))

Submission status: Wrong Answer

Solution code:
s=raw_input()
k=len(s)/2+1
lef=(len(s)-1)/2
ch=s[lef]
rig=(len(s))/2
while 0<=lef and rig<len(s):
    k+=1
    if s[lef]==s[rig] and ch==s[lef]:
        lef-=1
        rig+=1
    else:
        k-=1
        break
print(k)

Submission status: Wrong Answer

Solution code:
s=raw_input()
k=len(s)/2+1
lef=(len(s)-1)/2
ch=s[lef]
rig=(len(s))/2
while 0<=lef and rig<len(s):
    if s[lef]==s[rig] and ch==s[lef]:
        lef-=1
        rig+=1
        k+=1
    else:
        k-=1
        break
print(k)

Submission status: Wrong Answer

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

S = input()

V = []
flg = S[0]
v = 0
change_points = []

for i,s in enumerate(S):
    if flg == s:
        v += 1
    else:
        flg = s
        V.append(v)
        change_points.append(i)
        v = 1
V.append(v)

inc_V = []

if len(V) == 1:
    print(V[0])
else:
    for i in range(len(V) - 1):
        inc_V.append(V[i+1] + V[i])
    max_v = max(inc_V)
    max_index = change_points[inc_V.index(max_v)]
    from_left_length = max_index
    from_right_length = len(S) - max_index
    max_length = max(from_left_length, from_right_length)
    ans = min(max_length, max_v)
    # print(change_points)
    # print(from_left_length, from_right_length, max_v)
    # print(V)
    print(ans)

Submission status: Wrong Answer

Solution code:
s = raw_input()
ones, left, right = 0, 0, 0

for c in s:
    if c == '1':
        ones += 1
        right = 0
    if c == '0':
        if ones == 0:
            left += 1
        right += 1

print min(len(s), left + right)

Submission status: Wrong Answer

Solution code:
def min_flip(s):
    n = len(s)       
    ones = []
    i = 0
    while i < n:
        if s[i] == '1':
            ones.append(i)
        i += 1

    ans = len(s)
    for i in range(0, len(ones)):
        if i == 0 and ones[i] > 0:
            ans = min(ans, ones[i])
        if i == len(ones) - 1 and ones[i] < n - 1:
            ans = min(ans, n - 1 - ones[i])
        if ones[i] > ones[i-1] + 1:
            ans = min(ans, ones[i] - ones[i-1])
    
    return ans

s = input()

t = ''
for c in s:
    if c == '0':
        t = t + '1'
    else:
        t = t + '0'

print(max(min_flip(s), min_flip(t)))

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
from heapq import heappush, heappop
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy4, dx4 = [0, -1, 0, 1], [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def ceil(a, b): return (a + b - 1) // b


def can(n, s):
    if n > len(s):
        return False
    if n == 1:
        return True
    s = list(s)
    for i in range(len(s) - n + 1):
        if s[i] == "1":
            for j in range(i, i + n):
                s[j] = "1" if s[j] == "0" else "0"
    num = 0
    for i in range(len(s)):
        if s[i] != "0":
            break
        else:
            num += 1
    return num >= n



def main():
    S = input()

    # [low, high)
    low, high, ans = 0, len(S), 1
    while high - low > 1:
        middle = (low + high) // 2
        if can(middle, S):
            ans = middle
            low = middle
        else:
            high = middle

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

s = input()
f = True
if s[0] == '0':
    f = True
else:
    f = False
c = [0]
for ss in s:
    if f and ss == '0':
        c[-1] = c[-1] + 1
    elif not f and ss == '1':
        c[-1] = c[-1] + 1
    else:
        f = not f
        c.append(1)
if len(c) == 1:
    print(len(c))
    sys.exit()

d = []
for i in range(len(c)-1):
    d.append(c[i] + c[i+1])
print(max(d))

Submission status: Wrong Answer

Solution code:
import sys

s = input()
f = True
if s[0] == '0':
    f = True
else:
    f = False
c = [0]
for ss in s:
    if f and ss == '0':
        c[-1] = c[-1] + 1
    elif not f and ss == '1':
        c[-1] = c[-1] + 1
    else:
        f = not f
        c.append(1)
if len(c) == 1:
    print(len(c))
    sys.exit()

d = []
for i in range(len(c)-1):
    d.append(c[i] + c[i+1])
print(min(d))

Submission status: Wrong Answer

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

m = N//2
if N%2 == 0:
    if S[m] != S[m-1]:
        print(m)
        sys.exit()
    ans = m
    for i in range(m-1):
        if S[m] == S[m+i] or S[m] == S[m-1-i]:
            ans += 1
        else:
            break
else:
    ans = m
    for i in range(m):
        if S[m] == S[m+i] or S[m] == S[m-1-i]:
            ans += 1
        else:
            break
print(ans)

Submission status: Wrong Answer

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

m = N//2
if N%2 == 0:
    if S[m] != S[m-1]:
        print(m)
        sys.exit()
    ans = m+1
    for i in range(m-1):
        if S[m] == S[m+i] == S[m-1-i]:
            ans += 1
        else:
            break
else:
    ans = m+1
    for i in range(m):
        if S[m] == S[m+i] == S[m-1-i]:
            ans += 1
        else:
            break
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
fis = s[0]
n = len(s)
for i in range(n):
    if s[i] !=s[0]:
        break;
a = max(i,n-i)
print("{}".format(a))

Submission status: Wrong Answer

Solution code:
s = input()

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

Submission status: Wrong Answer

Solution code:
s = input()

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

Submission status: Wrong Answer

Solution code:
s = input()

l = len(s)
count = 0
for i,x in enumerate(s):
    if i == 0:
        pass
    else:
        if x != s[i-1]:
            count += 1
        else:
            if count % 2 == 0:
                a = count + 1
                if a < l:
                    l = a
            else:
                a = count
                if a < l:
                    l = a
            count = 0

ans = (l + 1) / 2

print(int(ans))

Submission status: Wrong Answer

Solution code:
s = input()

l = len(s)
count = 0
for i,x in enumerate(s):
    if i == 0:
        pass
    else:
        if x != s[i-1]:
            count += 1
        else:
            if count % 2 == 0:
                a = count + 1
                if a < l:
                    l = a
            else:
                a = count
                if a < l:
                    l = a
            count = 0

print((count+1)//2)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = [0]
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:
    ans = max(a)
    ans = max(ans, sum(a)/2)
else:
    a.append(0)
    b = []
    l = len(a)
    for i in xrange(l-1):
        b.append(a[i] + a[i+1])
    b.sort()
    pre = 0
    ans = max(a)
    ans = max(ans,b[-2])
    ans = max(ans,b[-1]/2)
    tot = len(s)
    for i in xrange(1,l-1):
        
        if pre >=  a[i+1]:
            ans = max(ans,a[i] + a[i+1])
        suf = tot - pre - a[i] - a[i+1]
        if suf >= a[i]:
            ans = max(ans, a[i] + a[i+1])
        pre += a[i]
    print ans

Submission status: Wrong Answer

Solution code:
s = input()

l = len(s)
count = 0
for i,x in enumerate(s):
    if i == 0:
        pass
    else:
        if x != s[i-1]:
            count += 1
        else:
            if count % 2 == 0:
                a = count -1
                if a <= l:
                    l = a
            else:
                a = count
                if a <= l:
                    l = a
            count = 0

print((count+1)//2)

Submission status: Wrong Answer

Solution code:
n = list(input())
boo = []
bar = []
now = 1
bb = 0
kk = []
for i in range(len(n)):
    try:
        if n[i] == n[i+1]:
            now += 1
        else:
            boo.append([i - now + 1, i + 1, now])
            bar.append(now)
            now = 1
    except:
        boo.append([i-now+1, i+1, now])
        bar.append(now)
        now = 1
ms, mk = 0, 0
try:
    ms = max(bar) + bar[bar.index(max(bar)) - 1]
except:
    pass
try:
    mk = max(bar) + bar[bar.index(max(bar)) + 1]
except:
    pass
if len(bar) == 1 and n[0] == "0":
    print("0")
elif len(bar) == 1 and n[0] == "1":
    print(bar[0])
elif len(bar) == 2:
    print(max(bar))
else:
    print(min(ms, mk))
if n[0] == "1":
    bb = sum(list(map(int, n[0::2])))
else:
    bb = sum(list(map(int, n[1::2])))

Submission status: Wrong Answer

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

m = N//2
if N%2 == 0:
    if S[m-1] != S[m]:
        print(m)
        sys.exit()
    ans = 2
    for i in range(m-1):
        if S[m] == S[m+i]:
            ans += 1
        if S[m] == S[m-1-i]:
            ans += 1
        if S[m] != S[m+1] or S[m] != S[m-1-i]:
            break
    if ans >= m:
        print(ans)
        sys.exit()
    ans = 1
    for i in range(m-1):
        if S[0] == S[1+i] and S[-1] == S[-1-i]:
            ans += 1
    print(ans)
else:
    ans = 1
    for i in range(m):
        if S[m] == S[m+i]:
            ans += 1
        if S[m] == S[m-1-i]:
            ans += 1
        if S[m] != S[m+1] or S[m] != S[m-1-i]:
            break
    if ans >= m:
        print(ans)
        sys.exit()
    ans = 1
    for i in range(m):
            if S[0] == S[1+i] and S[-1] == S[-1-i]:
                ans += 1
    print(ans)

Submission status: Wrong Answer

Solution code:
n = list(input())
boo = []
bar = []
now = 1
bb = 0
kk = []
for i in range(len(n)):
    try:
        if n[i] == n[i+1]:
            now += 1
        else:
            boo.append([i - now + 1, i + 1, now])
            bar.append(now)
            now = 1
    except:
        boo.append([i-now+1, i+1, now])
        bar.append(now)
        now = 1
ms, mk = 0, 0
try:
    ms = max(bar) + bar[bar.index(max(bar)) - 1]
except:
    pass
try:
    mk = max(bar) + bar[bar.index(max(bar)) + 1]
except:
    pass
if len(bar) == 1 and n[0] == "0":
    print("0")
elif len(bar) == 1 and n[0] == "1":
    print(bar[0])
elif n[0] == "1":
    bb = sum(list(map(int, n[0::2])))
else:
    bb = sum(list(map(int, n[1::2])))
if len(bar) == 2:
    print(max(bar))
else:
    print(min(ms, mk))

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 = 1  # 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:
            k1 = max(LENL, LENR)
            k2 = min(LENL, LENR) + LENONE
            K = max(K, max(k1, k2))
            LENONE = 1
            LENL = LENR
            POS = ONE
if POS == L:
    K = LENL
elif POS == R:
    k1 = max(LENL, LENR)
    k2 = min(LENL, LENR) + LENONE
    K = max(K, max(k1, k2))
else:
    K = max(K, LENONE)
print(K)

Submission status: Wrong Answer

Solution code:
n = list(input())
boo = []
bar = []
now = 1
bb = 0
kk = []
for i in range(len(n)):
    try:
        if n[i] == n[i+1]:
            now += 1
        else:
            boo.append([i - now + 1, i + 1, now])
            bar.append(now)
            now = 1
    except:
        boo.append([i-now+1, i+1, now])
        bar.append(now)
        now = 1
ms, mk = 0, 0
try:
    ms = max(bar) + bar[bar.index(max(bar)) - 1]
except:
    pass
try:
    mk = max(bar) + bar[bar.index(max(bar)) + 1]
except:
    pass
if len(bar) == 1 and n[0] == "1":
    print("0")
elif len(bar) == 1 and n[0] == "0":
    print(bar[0])
elif n[0] == "1":
    bb = sum(list(map(int, n[0::2])))
else:
    bb = sum(list(map(int, n[1::2])))
if len(bar) == 2:
    print(max(bar))
else:
    print(min(ms, mk))

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)
    two = [sum(con[i:i + 2]) for i in range(len(con) - 1)]
    three = [sum(con[i:i + 3]) for i in range(len(con) - 2)]
    four = [sum(con[i:i + 4]) for i in range(len(con) - 3)]
    # print(con)
    # print(two)
    # print(three)

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

    ans = max(con)

    def calc(A, B, add=2):
        ret = -1
        for i in range(len(A)):
            t = A[i]
            f = False
            # print(i, t)
            if 0 <= i - 1 < len(B):
                f = True
                t = min(t, B[i - 1])
            if 0 <= i + add < len(B):
                f = True
                t = min(t, B[i + add])

            # print(i, t)
            if f:
                ret = max(ret, t)

        return ret

    ans = max(ans, calc(three, two))
    ans = max(ans, calc(four, two, add=3))
    ans = max(ans, calc(two, two, add=1))

    print(ans)


S = input()
main(S)

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 = 1  # 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:
            k1 = max(LENL, LENR)
            k2 = min(LENL + LENONE, LENONE + LENR)
            K = max(K, max(k1, k2))
            LENONE = 1
            LENL = LENR
            POS = ONE
if POS == L:
    K = LENL
elif POS == R:
    k1 = max(LENL, LENR)
    k2 = min(LENL + LENONE, LENONE + LENR)
    K = max(K, max(k1, k2))
else:
    K = max(K, LENONE)
print(K)

Submission status: Wrong Answer

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

change = 0
cflag=[]
for i in range(len(s)-1):
  if s[i]!=s[i+1]:
    change+=1
    cflag+=[i+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()
elif change==2:
  print(max(1+cflag[0],1+len(s)-cflag[1]))
  sys.exit()
  
conti0,conti1=[],[]
flag=s[0]
count=1
for i in range(1,len(s)):
  if i ==len(s)-1:
    if flag==0:conti0+=[count]
    else: conti1+=[count]
  elif s[i]==flag:
    count+=1
  else: 
    if flag==0:conti0+=[count]
    else: conti1+=[count]
    flag=s[i]
    count=1
print(max(max(conti0),max(conti1))+2)

Submission status: Wrong Answer

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[1]=A.pop(0)
            else:
                A[-2]=A.pop(-1)
        else:
            ans=min(ans,mid+max(A[0],A[-1]))
            if A[0]>A[-1]:
                A[-2]=A.pop(-1)
            else:
                A[1]=A.pop(0)
    print min(max(A),ans)

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 = 1  # 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:
            k1 = max(LENL, LENR)
            k2 = min(LENL + LENONE, LENONE + LENR)
            K = max(K, max(k1, k2))
            LENONE = 1
            LENL = i  # LENR
            POS = ONE
if POS == L:
    K = LENL
elif POS == R:
    k1 = max(LENL, LENR)
    k2 = min(LENL + LENONE, LENONE + LENR)
    K = max(K, max(k1, k2))
else:
    K = max(K, LENONE)
print(K)

Submission status: Wrong Answer

Solution code:
n = list(input())
boo = []
bar = []
now = 1
bb = 0
kk = []
for i in range(len(n)):
    try:
        if n[i] == n[i+1]:
            now += 1
        else:
            boo.append([i - now + 1, i + 1, now])
            bar.append(now)
            now = 1
    except:
        boo.append([i-now+1, i+1, now])
        bar.append(now)
        now = 1
ms, mk = 0, 0
try:
    ms = max(bar) + bar[bar.index(max(bar)) - 1]
except:
    pass
try:
    mk = max(bar) + bar[bar.index(max(bar)) + 1]
except:
    pass
if n[0] == "1":
    bb = sum(list(map(int, n[0::2])))
else:
    bb = sum(list(map(int, n[1::2])))
if len(bar) == 2:
    print(max(bar))
else:
    print(min(ms, mk))

Submission status: Wrong Answer

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

m = N//2
if N%2 == 0:
    if S[m-1] != S[m]:
        print(m)
        sys.exit()
    ans = 2
    for i in range(m-1):
        if S[m] == S[m+i]:
            ans += 1
        if S[m] == S[m-1-i]:
            ans += 1
        if S[m] != S[m+1] or S[m] != S[m-1-i]:
            break
else:
    ans = 1
    for i in range(m):
        if S[m] == S[m+i]:
            ans += 1
        if S[m] == S[m-1-i]:
            ans += 1
        if S[m] != S[m+1] or S[m] != S[m-1-i]:
            break
print(max(ans, m))

Submission status: Wrong Answer

Solution code:
S = input()
L = 0
i = 0
while i < len(S):
    j = 1
    while i + j + 1 <= len(S) and S[i + j] == S[i]:
        j += 1
    L = max(L, j)
    i += j
M = len(S) // 2 + 1
if len(S) == 2:
    M = 1
print(max(M, L))

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(max(cnt,n-cnt)-1)

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:
            k1 = max(LENL, LENR)
            k2 = min(LENL + LENONE, LENONE + LENR)
            K = min(K, max(k1, k2))
            LENONE = 1
            LENL = i  # LENR
            POS = ONE
if POS == R:
    k1 = max(LENL, LENR)
    k2 = min(LENL + LENONE, LENONE + LENR)
    K = min(K, max(k1, k2))
else:
    if K == float('inf'):
        K = 0
    K = max(K, max(LENL, LENONE))
print(K)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = [0]
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()
    pre = 0
    ans = max(a)
    tot = len(s)
    for i in xrange(1,l-1):
        
        if pre >=  a[i+1]:
            ans = max(ans,a[i] + a[i+1])
        suf = tot - pre - a[i] - a[i+1]
        if suf >= a[i]:
            ans = max(ans, a[i] + a[i+1])
        pre += a[i]
    print ans

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)
    two = [sum(con[i:i + 2]) for i in range(len(con) - 1)]
    three = [sum(con[i:i + 3]) for i in range(len(con) - 2)]
    # print(con)
    # print(two)
    # print(three)

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

    ans = max(con)

    def calc(A, B, add=2):
        ret = -1
        for i in range(len(A)):
            t = A[i]
            f = False
            # print(i, t)
            if 0 <= i - 1 < len(B):
                f = True
                t = min(t, B[i - 1])
            if 0 <= i + add < len(B):
                f = True
                t = min(t, B[i + add])

            # print(i, t)
            if f:
                ret = max(ret, t)

        return ret

    ans = max(ans, calc(three, two))
    ans = max(ans, calc(two, two, add=1))

    print(ans)


S = input()
main(S)

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()

if lenS % 2 == 1:
	center = int(lenS/2)
	K = 1
	while K < center:
		if S[center-K] == S[center] and S[center + K] == S[center]:
			K += 1
			continue
		else:
			break
	K = center + K
else:
	center = int(lenS/2)
	K = 1
	while K < center:
		if S[center-K] == S[center+K-1] and S[center-K] == S[center]:
			K += 1
			continue
		else:
			break
	K = center + K - 1

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

L = int(lenS/2)
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:
from sys import stdin
s = stdin.readline().strip()
a = [0]
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()
    pre = 0
    ans = max(a)
    tot = len(s)
    for i in xrange(1,l-1):
        pre += a[i]
        if pre >= a[i] + a[i+1]:
            ans = max(ans,a[i] + a[i+1])
        suf = tot - pre
        if suf >= a[i] + a[i+1]:
            ans = max(ans, a[i] + a[i+1])
        if i < l-2 and pre >= a[i] + a[i-1]:
            ans = max(ans,a[i] + a[i-1])
        if i < l-2 and suf >= a[i] + a[i-1]:
            ans = max(ans,a[i] + a[i-1])
    print ans

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:
        sub_s = s[m_b:m_e]
        sub_s.sort()
        if sub_s[0] != sub_s[len(sub_s) - 1]:
            is_ok = False
    if is_ok:
        ans = i
    else:
        break
print(ans)

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()

if lenS % 2 == 1:
	center = int(lenS/2)
	K = 1
	while K < center:
		if S[center-K] == S[center] and S[center + K] == S[center]:
			K += 1
			continue
		else:
			break
	R = center + K
else:
	center = int(lenS/2)
	K = 1
	while K < center:
		if S[center-K] == S[center+K-1] and S[center-K] == S[center]:
			K += 1
			continue
		else:
			break
	R = center + K - 1
def reverse(A, l, n):
	for x in range(n):
		A[l+x] = 1 - A[l+x]

L = int(lenS/2)
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:
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)]

s = sum(B)
ans = s
if len(B)%2==0:
  l = 0
  for b in B:
    ans = min(ans, max(l,s-l-b))
    l += b
else:
  l = 0
  for b in B:
    ans = min(ans, max(l+b, s-l))
    l += b
print ans

Submission status: Wrong Answer

Solution code:
S = input()
L = 0
i = 0
while i < len(S):
    j = 1
    while i + j + 1 <= len(S) and S[i + j] == S[i]:
        j += 1
    L = max(L, j)
    i += j
print(max((len(S) + 2 - 1) // 2, L))

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]))
ans = max(ans, max(cnts))
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().strip()
a = [0]
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()
    pre = 0
    ans = max(a)
    tot = len(s)
    for i in xrange(l-1):
        pre += a[i]
        if pre >= a[i] + a[i+1]:
            ans = max(ans,a[i] + a[i+1])
        suf = tot - pre
        if suf >= a[i] + a[i+1]:
            ans = max(ans, a[i] + a[i+1])
    print ans

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, min(LENL + LENONE, LENONE + LENR))
            LENONE = 1
            LENL = i  # LENR
            POS = ONE
if POS == R:
    K = min(K, min(LENL + LENONE, LENONE + LENR))
else:
    K = min(K, max(LENL, LENONE))
print(K)

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]))
cnt_sum = sum(cnts)
for _ in range(2):
    cand = 0
    for cnt in cnts:
        if cand > cnt_sum // 2:
            break
        cand += cnt
    ans = max(ans, cand)
    cnts = cnts[::-1]
print(ans)