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:
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <chrono>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <cassert>
#include <type_traits>

using namespace std;

// typedef
//------------------------------------------
typedef long long LL;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef vector<VI> VVI;
typedef vector<VB> VVB;
typedef vector<VS> VVS;
typedef vector<VLL> VVLL;
typedef vector<VVI> VVVI;
typedef vector<VVLL> VVVLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;
typedef pair<string, string> PSS;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<VPII> VVPII;
typedef vector<VPLL> VVPLL;
typedef vector<VS> VVS;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<string, int> MSI;
typedef map<int, string> MIS;

// container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define EACH(i, arr) for(typeof((arr).begin()) i=(arr).begin(); i!=(arr).end(); ++i)
#define EXIST(str, e) ((str).find(e)!=(str).end())
#define COUNT(arr, v) count((arr).begin(), (arr).end(), v)
#define SEARCH(v, w) search((v).begin(), (v).end(), (w).begin(), (w).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define ROTATE_LEFT(arr, c) rotate((arr).begin(), (arr).begin()+(c), (arr).end())
#define ROTATE_RIGHT(arr, c) rotate((arr).rbegin(), (arr).rbegin() + (c), (arr).rend())
#define SUMI(arr) accumulate((arr).begin(), (arr).end(), 0)
#define SUMD(arr) accumulate((arr).begin(), (arr).end(), 0.)
#define SUMLL(arr) accumulate((arr).begin(), (arr).end(), 0LL)
#define MULD(arr) accumulate((arr).begin(), (arr).end(), 1., multiplies<double>())
#define UB(arr, n) upper_bound((arr).begin(), (arr).end(), n)
#define LB(arr, n) lower_bound((arr).begin(), (arr).end(), n)
#define PB push_back
#define MP make_pair
#define ft first
#define sd second


// input output
//------------------------------------------
#define GL(s) getline(cin, (s))
#define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0)
#define OUT(d) std::cout<<(d)
#define OUT_L(d) std::cout<<(d)<<endl
#define FOUT(n, data) std::cout<<std::fixed<<std::setprecision(n)<<(data)
#define FOUT_L(n, data) std::cout<<std::fixed<<std::setprecision(n)<<(data)<<"\n"
#define EL() printf("\n")
#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v) {std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yay() printf("Yay!\n")
#define Nnn() printf(":(\n")
#define CE(x, y) ((x + y - 1) / (y))


template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
    in >> p.first >> p.second;
    return in;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
    for (auto &x: v)
        in >> x;
    return in;
}


template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
    out << "[" << p.first << ", " << p.second << "]" << "\n";
    return out;
}


//repetition
//------------------------------------------
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i)
#define REP(i, n)  FOR(i,0,n)
#define RREP(i, n) for(int i = n-1;i >= 0;i--)
#define FORLL(i, a, b) for(LL i=LL(a);i<LL(b);++i)
#define RFORLL(i, a, b) for(LL i=LL(b)-1;i>=LL(a);--i)
#define REPLL(i, n) for(LL i=0;i<LL(n);++i)
#define RREPLL(i, n) for(LL i=LL(n)-1;i>=0;--i)
#define FOREACH(x, arr) for(auto &(x) : (arr))

//------------------------------------------
//------------------------------------------



int main() {

    string S;
    cin >> S;

    int n = S.size();

    int ok = 0;
    int ng = n + 1;

    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;
        bool isGood = [&] {
            if (mid * 2 <= n) return true;
            int r = mid;
            int l = n - mid;
            for (int i = l; i < r; i++) {
                if (S[l] != S[i]) {
                    return false;
                }
            }
            return true;
        }();
        if (isGood) ok = mid;
        else ng = mid;
    }

    cout << ok << endl;

}

Submission status: Runtime Error

Solution code:
s = input()
prev = s[0]
cnt = 0

now_len = 1
len_sec = []
for i in range(1,len(s)):
  if prev == s[i]:
    now_len += 1
  else:
    len_sec.append(now_len)
    now_len = 1
  prev = s[i]
if len(len_sec)%2==0:
  mx = len_sec[0]+len_sec[1]
  for i in range(1,len(len_sec)-1):
    mx = max(mx, len_sec[i]+len_sec[i+1])
else:
  mx = max(len_sec)
print(mx)

Submission status: Runtime Error

Solution code:
#include "bits/stdc++.h"
using namespace std;


int main(){

  string s;
  cin >> s;
  int ans = INT_MAX;
  for(int i = 0; i + 1 < s.size(); i++){
    if(s[i] != s[i + 1])
      ans = min(ans, max(i + 1, (int)s.size() - i - 1));
  }

  cout << ans << '\n';


}

Submission status: Runtime Error

Solution code:
#include "bits/stdc++.h"
using namespace std;


int main(){

  string s;
  cin >> s;
  int ans = INT_MAX;
  for(int i = 0; i < s.size(); i++){
    if(s[i] != s[i + 1])
      ans = min(ans, max(i + 1, (int)s.size() - i - 1));
  }

  cout << ans << '\n';


}

Submission status: Runtime Error

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)

s = input()

K = []
for k in range(1, len(s)):
    if s[k] != s[k-1]:
        K.append(max(len(s)-k, k))
    else:
        continue

ans = 0
if min(K) == 0:
    if all(v == "1" for v in s):
        ans = len(s)
else:
    ans = min(K)
print(ans)

Submission status: Runtime Error

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)

s = input()

K = []
for k in range(1, len(s)):
    if s[k] != s[k-1]:
        K.append(max(len(s)-k, k))
    else:
        continue

ans = 0
if min(K) == 0:
    if all(v == "1" for v in s):
        ans = len(K)
else:
    ans = min(K)
print(ans)

Submission status: Runtime Error

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

p=s[0]
seq=1

for c in s[1:]:
  if c==p:
    seq+=1
  else:
    l.append(seq)
    seq=1
    p=c
ans=float("inf")

d1=min([a+b for a,b in zip(l[0::2],l[1::2])])
d2=min([a+b for a, b in zip(l[1::2],l[2::2])])
print(min(d1,d2))

Submission status: Runtime Error

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

p=s[0]
seq=1

for c in s[1:]
  if c==p:
    seq+=1
  else:
    l.append(seq)
    seq=1
    p=c
ans=float("inf")

d1=min([a+b for a,b in zip(l[0::2],l[1::2])])
d2=min([a+b for a, b in zip(l[1::2],l[2::2])])
print(min(d1,d2))

Submission status: Runtime Error

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

p=s[0]
seq=1

for c in s[1:]
  if c==p:
    seq+=1
  else:
    l.append(seq)
    seq=1
    prev=c
ans=float("inf")

d1=min([a+b for a,b in zip(l[0::2],l[1::2])])
d2=min([a+b for a, b in zip(l[1::2],l[2::2])])
print(min(d1,d2))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:

S = input()
l=len(S)
if l%2==0:
    s=S[int(l/2)]
    for i in range(int(l/2)):
        if S[int(l/2)-1-i]!=s or S[int(l/2)+i]!=s:
            break
    print(int(l/2)+i)
else:
    s=S[int(l/2)]
    for i in range(int(l/2)):
        if S[int(l/2)-1-i]!=s or S[int(l/2)+1+i]!=s:
            break
    print(int(l/2)+1+i)

Submission status: Runtime Error

Solution code:
S=input()+'2'
prev=S[0]
cnt=1
li=[]
for i in range(1,len(S)):
    if S[i]!=prev:
        li.append(cnt)
        prev=S[i]
        cnt=1
    else:
        cnt+=1
if len(li)==1:
    print(li[0])
elif len(li)==2:
    print(max(li))
elif len(li)%2==1:
    ans=li[0]+li[1]
    for i in range(len(li)-1):
        ans=min(ans,li[i]+li[i+1])
    print(ans)
else:
    ans1=li[0]+li[1]
    ans2=li[1]+li[2]
    for i in range(len(li)-2):
        ans1=min(ans1,li[i]+li[i+1])
    for i in range(1,len(li)-1):
        ans2=min(ans2,li[i]+li[i+1])
    print(max(a1,a2))

Submission status: Runtime Error

Solution code:
s=input()[1:-1]
c=s[0]
res=0
cnt=1
for i in range(1,len(s)):
    if s[i]==c:
        cnt+=1
    else:
        c=s[i]
        res=max(res,cnt)
        cnt=1
res=max(res,cnt)
print(res+1)

Submission status: Runtime Error

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

ans = N//2
l = r = N//2
if N%2 == 0: r += 1

while l >= 0:
    if S[l] == '1' or S[r] == '1':
        break
    l -= 1
    r += 1
    ans += 1
print(max(ans, N//2 + N%2))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
S = input()
n = len(S)
L = list(map(int, list(S)))
D = []
res = 0
for k in range(n-1):
    if L[k] != L[k+1]:
        D.append(max(k+1, n-k-1))

print(min(D))

Submission status: Runtime Error

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)
print(min(T))

Submission status: Runtime Error

Solution code:
a,b = map(int,input().split())
s = list(input())
a_lis = s[:(a-1)]
b_lis = s[b:]
if s[a] == "-":
  if "-" not in a_lis:
    if "-" not in b_lis:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
else:
  print("No")

Submission status: Runtime Error

Solution code:
def main():
    s = map(int,input().split())

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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
    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 = -1, 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: Runtime Error

Solution code:
import numpy as np 
s = input()
fis = s[0]
n = len(s)
k = float('inf')
temp = s[0]
for i in range(n):
    if temp!=s[i]:
        temp = s[i]
        k = np.min((k, np.max( (i, n-i) ) ))
print("{}".format(int(k)))

Submission status: Runtime Error

Solution code:
import numpy as np 
fis = s[0]
n = len(s)
k = float('inf')
temp = s[0]
for i in range(n):
    if temp!=s[i]:
        temp = s[i]
        k = np.min((k, np.max( (i, n-i) ) ))
print("{}".format(int(k)))

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
#define INT(x) int x; scanf("%d",&x)
#define LL(x) ll x; scanf("%lld",&x)
#define INPUT(x) cin >> x
#define REP1(x,n) for(int x = 0; x < n; x++)
#define REP2(x,s,e) for(int x = s; x <= e; x++)
#define RREP1(x,n) for(int x = n-1; x >= 0; x--)
#define RREP2(x,s,e) for(int x = s; x >= e; x--)
#define BR printf("\n")
#define INF_INT 2147483647
#define INF_LL 9223372036854775807

typedef long long ll;

int main(){
    string s;
    cin >> s;
    
    int mini = 1000000;
    
    REP1(i, s.size()-1){
        if (s[i] != s[i+1]) {
            mini = min(mini, max(i+1, int(s.size()-1-i) ));
        }
    }
    
    cout << mini << endl;
    
    return 0;
}

Submission status: Runtime Error

Solution code:
s = input()

l = len(s)

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

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

flips = []
for i in range(len(cnts) - 1):
    flips.append((cnts[i] + cnts[i + 1], i))
flips.sort()

from bisect import bisect_left
left, right = (len(S) + 1) // 2, len(S)
while left + 1 < right:
    mid = (left + right) // 2
    _, mid_idx = flips[bisect_left(flips, (mid, -1))]
    ret = [False] * (len(cnts))
    for _, idx in flips[mid_idx:]:
        ret[idx] = ret[idx + 1] = True
    if all(x for x in ret):
        left = mid
    else:
        right = mid
ans = max(left, ans)
print(ans)

Submission status: Runtime Error

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

flips = []
for i in range(len(cnts) - 1):
    flips.append((cnts[i] + cnts[i + 1], i))
flips.sort()

from bisect import bisect_left
left, right = (len(S) + 1) // 2, len(S)
while left + 1 < right:
    mid = (left + right) // 2
    _, mid_idx = bisect_left(flips, (mid, -1))
    ret = [False] * (len(cnts))
    for _, idx in flips[mid_idx:]:
        ret[idx] = ret[idx + 1] = True
    if all(x for x in ret):
        left = mid
    else:
        right = mid
ans = max(left, ans)
print(ans)

Submission status: Runtime Error

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 = l[:m + 1]
        s2 = l[:m]
        s3 = l[m:]
        s4 = 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: Runtime Error

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()
  
0/0
t=[]
for i in range(len(s)):
  if s[i]==0:t+=[1]
  else: t+=[0]
while True:
  firs=s.index(1)
  last=len(s)-1-s[::-1].index(1)
  print(firs,last)
  break

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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(

Submission status: Runtime Error

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

ans = 0
for i, c in enumerate(s):
  ld, rd = i, n - i - 1
  if c == '1':
  	ans = min(ans, max(lr, rd))
print(ans)