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: Runtime Error

Solution code:
if __name__=="__main__":
    l = list(input())
    N = len(l)
    results = []
    for i in range(0,N-1):
        if not l[i]==l[i+1]:
            results.append(max(i+1,N-i-1))
    print(min(results))

Submission status: Runtime Error

Solution code:
if __name__=="__main__":
    l = list(input())
    N = len(l)
    pre = l[0]
    results = []
    for i in range(1,N):
        if not l[i]==pre:
            pre = l[i]
            results.append(max(i+1,N-i-1))
    print(min(results))

Submission status: Runtime Error

Solution code:
s = raw_input()
n = len(s)
if not n & 1:
    l = n / 2 - 1
    r = n / 2
    res = n / 2
    while r < n and l >= 0 and s[l] == s[r]:
        l -= 1
        r += 1
        res += 1
    print res
else:
    l = n / 2 - 1
    r = n / 2 + 1
    res = n / 2 + 1
    if s[n / 2] != s[l] or s[n / 2] != s[r]:
        print res
    else:
        while r < n and l >= 0 and s[l] == s[r]:
            l -= 1
            r += 1
            res += 1
        print res

Submission status: Runtime Error

Solution code:
s = input()

lst = []
add_lst = []
n = 0
for i in range(len(s)):
    if i == 0 or s[i] == s[i-1]:
        n +=1
    else:
        lst.append(n)
        n = 1
lst.append(n)

for i in range(len(lst) - 1):
    add_lst.append(lst[i] + lst[i + 1])

f=0
if len(lst) > 2:
    f =min(add_lst)
elif len(lst) == 2:
    f=max(lst)
else:
    f=0

s = 0
fir = lst[0]
last = lst[len(lst)-1]
del lst[0]
del lst[len(lst)-1]
for i in range(len(lst)):
    s += lst[i]
print(min(f, s + min(fir, last)))

Submission status: Runtime Error

Solution code:
s = input()
table = []
n = len(s)
i = 0
k = 0

counter = 1
while i <= n-1:
	if s[i] == s[i+1]:
		counter += 1
		i += 1
	else:
		break

counter1 = 1
while k <= n-1:
	if s[n-k-1] == s[n-k-2]:
		counter1 += 1
		k += 1
	else:
		break

while i < n-k-2:
	t  = 1
	counter2 = 1
	while t <= n:
		if s[i] != s[i+t]:
			counter2 += 1
			t += 1
		else:
			table.append(counter2)
			break
	i = i + counter2 - 1

a1 = counter
if counter1 >= counter:
	a1 = counter1
if len(table) != 0:
	a2 = min(table)
	if a1 >= a2:
		a2 = a1
else:
	a2 = a1

print(a2)

Submission status: Runtime Error

Solution code:
# スペース区切りの整数の入力
bit = map(str, input().split())
length = len(bit)

num0 = 0
num1 = 0

for i in range(length):
    if bit[i] == '0':
        num0 += 1
    else:
        num1 += 1

K = max(num0, num1)
        
print(K)

Submission status: Runtime Error

Solution code:
s = raw_input()
max0, max1 = -1, -1
cnt = 0
L = []
t = ""
for i in range(len(s)):
    if i == 0:
        t += s[i]
    
    elif s[i] == "0" and s[i-1] == "0":
        t += s[i]
    elif s[i] == "0" and s[i-1] == "1":
        L.append(len(t))
        max1 = max(max1, len(t))
        t = "0" 
        cnt += 1
    elif s[i] == "1" and s[i-1] == "1":
        t += s[i]
    elif s[i] == "1" and s[i-1] == "0":
        max0 = max(max0, len(t))
        t = "1"
        cnt += 1
        L.append(max0)

if t.count("0") == 0:
    max1 = max(len(t))
    L.append(max1)
else:
    max0 = max(len(t))
    L.append(max1)

ans = 0
for i in range(len(L)-1):
    ans = max(ans, (L[i] + L[i+1]))

for i in L:
    print i,

print 
if s.count("0") == len(s) or s.count("1") == len(s):
    print "0"
elif len(s) == 2:
    print "1"
elif cnt == 1:
    print max(ans, 2)
else:
    print ans

Submission status: Runtime Error

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

def inverse(x):
    if x == 1:
        return 0
    return 1

def calc(string):
    ptn = [1]
    flag = string[0]
    for i in range(1, len(string)):
        if flag != string[i]:
            flag = string[i]
            ptn.append(1)
        else:
            ptn[-1] += 1
    return ptn

def func(ptn, min_change):
    if len(ptn) == 1:
        return min(min_change, len(ptn))
    elif len(ptn) == 2:
        return min(min_change, max(ptn[0], ptn[1]))
    elif len(ptn) == 3:
        return min(sum(ptn[:-1]), sum(ptn[1:]), min_change)
    ptn, change = update(ptn)
    return func(ptn, min(min_change, change))

def update(ptn):
    tmp = sum(ptn[0:3])
    ptn[3] += ptn[2]
    return ptn[:2] + ptn[3:], tmp

patern = calc(s)
print (func(patern, n))

Submission status: Runtime Error

Solution code:
s = input()
table = []
n = len(s)
i = 0
k = 0

counter = 1
while i <= n-1:
	if s[i] == s[i+1]:
		counter += 1
		i += 1
	else:
		break

counter1 = 1
while k <= n-1:
	if s[n-k-1] == s[n-k-2]:
		counter1 += 1
		k += 1
	else:
		break

while i < n-k-2:
	t  = 1
	counter2 = 1
	while t <= n:
		if s[i] != s[i+t]:
			counter2 += 1
			t += 1
		else:
			table.append(counter2)
			break
	i = i + counter2 - 1

a1 = counter
if counter1 >= counter:
	a1 = counter
if len(table) != 0:
	a2 = min(table)
	if a1 >= a2:
		a2 = a1
else:
	a2 = a1

print(a2)

Submission status: Runtime Error

Solution code:
S=input()
minSeq=100000
seq=[]
tmpSeq=1
for i in range(1,len(S)):
    if(S[i]==S[i-1]):
        tmpSeq+=1
        if not(i==len(S)-1):
            continue
    if tmpSeq not in seq:
        seq.append(tmpSeq)
    tmpSeq=1
if max(seq)==1:
    print(seq[0]*2)
elif len(seq)==2:
    print(max(seq))
else:
    print(min(seq))

Submission status: Runtime Error

Solution code:
s = list(input())
one = list.count("1")
zero = list.count("0")
ans = []
ans.append(one)
ans.append(zero)
ans = sorted(ans)
print(ans[0])

Submission status: Runtime Error

Solution code:
mx = max(mx, min(i, len(l)-x))

Submission status: Runtime Error

Solution code:
s = raw_input()
max0, max1 = -1, -1
cnt = 0
t = ""
for i in range(len(s)):
    if i == 0:
        t += s[i]
    
    elif s[i] == "0" and s[i-1] == "0":
        t += s[i]
    elif s[i] == "0" and s[i-1] == "1":
        max1 = max(max1, len(t))
        t = "0"
        cnt += 1
    elif s[i] == "1" and s[i-1] == "1":
        t += s[i]
    elif s[i] == "1" and s[i-1] == "0":
        max0 = max(max0, len(t))
        t = "1"
        cnt += 1

if t.count("0") == 0:
    max1 = max(max1, len(t))
else:
    max0 = max(max0, len(t))

if s.count("0") == len(s) or s.count("1") == len(s):
    print "0"
elif len(s) == 2:
    print "1"
elif cnt == 1:
    print max(max(max1, max0), 2)
else:
    print max(max1 + max0)

Submission status: Runtime Error

Solution code:
def d_WideFlip(S):
    import re
    from collections import Counter

    d = Counter(S)
    zero = d['0']
    one_continue = re.findall(r'1+', S)
    one_continue = len(max(one_continue))
    return max(zero, one_continue)

S = input()
print(d_WideFlip(S))

Submission status: Runtime Error

Solution code:
s = input()

import numpy as np
lst = []
add_lst = []
n = 0
for i in range(len(s)):
    if i == 0 or s[i] == s[i-1]:
        n +=1
    else:
        lst.append(n)
        n = 1
lst.append(n)

for i in range(len(lst) - 1):
    add_lst.append(lst[i] + lst[i + 1])
add_lst.append(lst[len(lst) - 1])

max0 = 100000
while True:
    if len(lst) == 2:
        print(np.amax(lst))
        break
    argmax = np.argmax(add_lst)
    max0 = min(max0, np.amax(add_lst))
    if argmax != 0:
        lst[argmax - 1] += lst[argmax]
    if argmax != len(lst) - 1:
        lst[argmax + 1] += lst[argmax]
    if argmax != 0 or argmax != 1:
        add_lst[argmax -1] += lst[argmax - 2]
    if argmax != len(lst) -2 or argmax != len(lst) -1:
        add_lst[argmax +1] += lst[argmax +2]
    del add_lst[argmax]
    del lst[argmax]

Submission status: Runtime Error

Solution code:
S =str(input())
if len(S) % 2 == 1:
    a = 1
    b = int((len(S) - 1) / 2)
    while True:
        if b - a < 0:
            break
        if S[b] == S[b + a] and S[b] == S[b - a]:
            a = a + 1
        else:
            break
    c = a + b
    print(c)
else:
    d = 0
    e = int(len(S) / 2 - 1)
    f = S[e]
    while True:
        if e - d < 0:
            break
        if S[e + 1] == f:
            d = d + 1
            if S[e - d] == f and S[e + d + 1] == f:
                d = d + 1
            else:
                break
        else:
            break
    g = d + e + 1
    print(g)

Submission status: Runtime Error

Solution code:
s = input()
ss = []

key = s[0]
cnt = 1
for i in range(1,len(s)):
    if key == s[i]:
        cnt += 1
    else:
        ss.append(cnt)
        key = s[i]
        cnt = 1

ss.append(cnt)

ans = 0
maxs = max(ss)
#print(ss)
# 端かどうかの判定。端ならそのまま。
if len(ss)==1 or len(ss)==2:
    ans = maxs
elif max(ss[1:len(ss)-2]) < maxs and ss[0] != ss[len(ss)-1]:
    ans = maxs
# 端じゃないなら、隣の長いほうも入る。
# 但し、とろうとしている長いのが端なら、短いほうしか入れられない。
else:
    tmpmax = 0
    print(ss)
    for i in range(1,len(ss)-1 ):
        if ss[i] == maxs:
            if i == 1:
                if ss[0] > ss[2]:
                    tmp = maxs + ss[2] 
                else:
                    tmp = maxs + ss[0]
            elif i == len(ss)-2:
                if ss[len(ss)-1] > ss[len(ss)-3]:
                    tmp = maxs + ss[len(ss)-3]
                else:
                    tmp = maxs + ss[len(ss)-1]
            else:
                tmp= maxs+max(ss[i-1],ss[i+1])

            if tmp > tmpmax:
                tmpmax = tmp
        else:
            pass

    ans = tmpmax

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

S = [int(x) for x in list(input())]

if len(S)==1:
    print(1)

elif len(S)==2:
    if S[0] == S[1]:
        print(2)
    else:
        print(1)

else:    
    for i in range(len(S)):
        if S[i] == S[i+1]:
            pass
        else:
            f = i + 1
            break

    for j in range(1, len(S)):
        if S[-j-1] == S[-j]:
            pass
        else:
            b = j
            break

    if f <= 2 and b <= 2:
        print(2)
    else:
        print(max(f,b))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

S = [int(x) for x in list(input())]

if len(S)==1:
    print(1)

elif len(S)==2:
    if S[0] == S[1]:
        print(2)
    else:
        print(1)

else:    
    for i in range(len(S)):
        if S[i] == S[i+1]:
            pass
        else:
            f = i + 1
            break

    for j in range(1, len(S)):
        if S[-j-1] == S[-j]:
            pass
        else:
            b = j

    if f <= 2 and b <= 2:
        print(2)
    else:
        print(max(f,b))

Submission status: Runtime Error

Solution code:
S =str(input())
if len(S) % 2 == 1:
    a = 1
    b = int((len(S) - 1) / 2)
    while True:
        if S[b] == S[b + a] and S[b] == S[b - a]:
            a = a + 1
        else:
            break
    c = a + b
    print(c)
else:
    d = 0
    e = int(len(S) / 2 - 1)
    f = S[e]
    while True:
        if S[e + 1] == f:
            d = d + 1
            if S[e - d] == f and S[e + d + 1] == f:
                d = d + 1
            else:
                break
        else:
            break
    g = d + e + 1
    print(g)

Submission status: Runtime Error

Solution code:
s = input()
ss = []

key = s[0]
cnt = 1
for i in range(1,len(s)):
    if key == s[i]:
        cnt += 1
    else:
        ss.append(cnt)
        key = s[i]
        cnt = 1

ss.append(cnt)

ans = 0
maxs = max(ss)
#print(ss)
# 端かどうかの判定。端ならそのまま。
if len(ss)==2:
    ans = maxs
elif max(ss[1:len(ss)-1]) < maxs and ss[0] != ss[len(ss)-1]:
    ans = maxs
# 端じゃないなら、隣の長いほうも入る。
else:
    tmpmax = 0
    for i in range(1,len(ss)-2 ):
        if ss[i] == maxs:
           tmp= maxs+max(ss[i-1],ss[i+1])
           if tmp > tmpmax:
               tmpmax = tmp
        else:
            pass

    ans = tmpmax

print(ans)

Submission status: Runtime Error

Solution code:
S =str(input())
if len(S) % 2 == 1:
    a = 1
    b = int((len(S) - 1) / 2)
    while True:
        if S[b] == S[b + a] and S[b] == S[b - a]:
            a = a + 1
        else:
            break
    c = a + b
    print(c)
else:
    d = 1
    e = int(len(S) / 2 - 1)
    f = S[e]
    while True:
        if S[e + 1] == f:
            d = d + 1
            if S[e - d] == f and S[e + d + 1] == f:
                d = d + 1
            else:
                break
        else:
            break
    g = d + e
    print(g)

Submission status: Runtime Error

Solution code:
s = input()

key = s[0]
count = 1
c_list = []
for i in range(1, len(s)):
    if key == s[i]:
        count += 1
    else:
        c_list.append(count)
        key = s[i]
        count = 1
if count > 1:
    c_list.append(count)
ans = max(c_list)
if max(c_list) < len(s)/2:
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
s = input()
ss = []

key = s[0]
cnt = 1
for i in range(1,len(s)):
    if key == s[i]:
        cnt += 1
    else:
        ss.append(cnt)
        key = s[i]
        cnt = 1

ss.append(cnt)

ans = 0
maxs = max(ss)
#print(ss)
# 端かどうかの判定。端ならそのまま。
if len(ss)==2:
    ans = maxs
elif max(ss[1:len(s)-1]) < maxs and ss[0] != ss[len(ss)-1]:
    ans = maxs
# 端じゃないなら、短いほうも入る
else:
    ans = maxs + min(ss[0],ss[len(ss)-1])

print(ans)

Submission status: Runtime Error

Solution code:
S = input()
p = S.count("0")

if p > len(S) - p:
    print(p)
else:
    print(S-p)

Submission status: Runtime Error

Solution code:
import sys

def solve(s, cache={}):
    if s in cache:
        return cache[s]
    n = len(s)
    if n == 1:
        return 1
    if s == "0" * n or s == "1" * n:
        return n
    r1 = solve("0" + s[1:-1])
    r2 = solve("1" + s[1:-1])
    r3 = solve(s[1:-1] + "0")
    r4 = solve(s[1:-1] + "1")
    r = max(r1, r2, r3, r4)
    cache[s] = r
    return r

line = sys.stdin.readline().strip()

print(solve(line))

Submission status: Runtime Error

Solution code:
S = input()
p = S.count("0")

if p > S - p:
    print(p)
else:
    print(S-p)

Submission status: Runtime Error

Solution code:
import sys

S = input()
center = len(S) // 2
b = S[center]

if len(S) % 2 == 1:
    for i in range(1, center + 1):
        if S[center + i] != b or S[center - i] != b:
            print(center + i)
            sys.exit()
else:
    for i in range(center + 1):
        if S[center - i] != b or S[center + 1 + i] != b:
            print(center)
            sys.exit()

Submission status: Runtime Error

Solution code:
S = input()
N = int(input())
ans = S.count("0")
if ans > N:
    ans = N
print(ans)