Problem p03069 (python)

Fixed submission:
_ = input()
S = input()
numc = S.count('.')
numcmin = numc
for s in S:
    if s == '#':
        numc = numc + 1
    else:
        numc = numc -1
        if numc < numcmin:
            numcmin = numc
print(numcmin)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
sint = [0]
prev = S[0]
DEBUG = False
def dbg(*something):
    if DEBUG:
        print(*something)

for s in S:
    if prev == s:
        sint[-1] = sint[-1] + 1
    else:
        prev = s
        sint.append(1)
if len(sint) % 2 == 1:
    if S[0] == '#':
        sint = [0] + sint
    else:
        sint.append(0)
elif S[0] == '#':
    sint = [0] + sint + [0]
dbg(sint)

def count2(numSB,numSW):
    dbg('len:',len(sint))
    if len(sint) < 2:
        return 0
    sint.pop(0)
    sint.pop()
    
    if len(sint) < 2:
        return 0
    b = sint.pop(0)
    w = sint.pop()
    
    numSR =  b + w
    dbg(numSB,numSW,numSR)
    if numSB <= numSW and numSB <= numSR:
        return numSB
    elif numSW <= numSB and numSW <= numSR:
        return numSW
    else:
        return numSR + count2(numSB+b,numSW+w)
    # if numSB <= numSW :
    #     return numSB
    # else:
    #     return numSW
    
    

# def count(_S,_numSW,_numSB):
#     _S = _S.lstrip('.').rstrip('#')
#     if len(_S) == 0 :
#         return 0
#     numSW = _S.count('.')
#     numSB = _S.count('#')
#     SR = _S.lstrip('#').rstrip('.')
#     numSR = len(_S) - len(SR)
#     if numSW <= numSB and numSW <= numSR:
#         return numSW
#     elif numSB <= numSW and numSB <= numSR:
#         return numSB
#     else:
#         return numSR + count(SR)


numSB = 0
numSW = 0
for i,v in enumerate(sint):
    if i % 2 == 1:
        numSB = numSB + v
    else:
        numSW = numSW + v
#print(sint)
#print(sint,numSB,numSW,count2(numSB,numSW))
numSR = count2(numSB,numSW)
dbg('numSW:',numSW)
dbg('numSB',numSB)
dbg('numSR',numSR)
print(min(numSB,numSW,numSR))
'''
22
#####.....####..###...####....#.#.#
'''
# #intS = []
# def xor(A,B):
#     #print(A)
#     #print(B)
#     num = 0
#     for i in range(len(A)):
        
#         if A[i]!=B[i] :
#             num = num + 1
#     return num
# # for s in S :
# #     intS.append(0 if s=='.' else 1)
# minC = N

# mask = ['#' for j in range(len(S) + 1)]
# for i in range(len(S) + 1):
#     mask[i] = '.'
#     c = xor(mask[1::],S)
#     if c < minC:
#         minC = c

# print(minC)
# # def dl(_S):


# #     l = '.'
# #     r = '#'
    
# #     _S = _S.lstrip(l).rstrip(r)
# #     print(_S)
# #     if len(_S) == 0:
# #         return 0
# #     _S2W = _S.lstrip(r)
# #     _S2B = _S.rstrip(l)
# #     _S2 =  _S2W if len(_S2W) <= len(_S2B) else _S2B
# #     return len(_S) - len(_S2) + dl(_S2)
# # print(dl(S))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
sint = [0]
prev = S[0]
DEBUG = False
def dbg(*something):
    if DEBUG:
        print(*something)
for s in S:
    if prev == s:
        sint[-1] = sint[-1] + 1
    else:
        prev = s
        sint.append(1)
if len(sint) % 2 == 1:
    if S[0] == '#':
        sint = [0] + sint
    else:
        sint.append(0)
elif S[0] == '#':
    sint = [0] + sint + [0]
dbg(sint)

def count2(numSB,numSW):
    dbg('len:',len(sint))
    if len(sint) < 2:
        return 0
    sint.pop(0)
    sint.pop()
    
    #この状態で偶数番号は黒、奇数番号は白(ゼロスタート)
    # numSB = 0
    # numSW = 0
    # for i,v in enumerate(sint):
        
    #     if i % 2 == 0:
    #         numSB = numSB + v
    #     else:
    #         numSW = numSW + v
    if len(sint) < 2:
        return 0
    b = sint.pop(0)
    w = sint.pop()
    
    numSR =  b + w
    dbg(numSB,numSW,numSR)
    if numSB <= numSW and numSB <= numSR:
        return numSB
    elif numSW <= numSB and numSW <= numSR:
        return numSW
    else:
        return numSR + count2(numSB-b,numSW-w)
    # if numSB <= numSW :
    #     return numSB
    # else:
    #     return numSW
    
    

# def count(_S,_numSW,_numSB):
#     _S = _S.lstrip('.').rstrip('#')
#     if len(_S) == 0 :
#         return 0
#     numSW = _S.count('.')
#     numSB = _S.count('#')
#     SR = _S.lstrip('#').rstrip('.')
#     numSR = len(_S) - len(SR)
#     if numSW <= numSB and numSW <= numSR:
#         return numSW
#     elif numSB <= numSW and numSB <= numSR:
#         return numSB
#     else:
#         return numSR + count(SR)


numSB = 0
numSW = 0
for i,v in enumerate(sint):
    if i % 2 == 1:
        numSB = numSB + v
    else:
        numSW = numSW + v
#print(sint)
#print(sint,numSB,numSW,count2(numSB,numSW))
print(min(numSB,numSW,count2(numSB,numSW)))
'''
22
#####.....####..###...
'''
# #intS = []
# def xor(A,B):
#     #print(A)
#     #print(B)
#     num = 0
#     for i in range(len(A)):
        
#         if A[i]!=B[i] :
#             num = num + 1
#     return num
# # for s in S :
# #     intS.append(0 if s=='.' else 1)
# minC = N

# mask = ['#' for j in range(len(S) + 1)]
# for i in range(len(S) + 1):
#     mask[i] = '.'
#     c = xor(mask[1::],S)
#     if c < minC:
#         minC = c

# print(minC)
# # def dl(_S):


# #     l = '.'
# #     r = '#'
    
# #     _S = _S.lstrip(l).rstrip(r)
# #     print(_S)
# #     if len(_S) == 0:
# #         return 0
# #     _S2W = _S.lstrip(r)
# #     _S2B = _S.rstrip(l)
# #     _S2 =  _S2W if len(_S2W) <= len(_S2B) else _S2B
# #     return len(_S) - len(_S2) + dl(_S2)
# # print(dl(S))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
sint = [0]
si = 0
prev = S[0]

for s in S:
    if prev == s:
        sint[si] = sint[si] + 1
    else:
        prev = s
        si = si + 1
        sint.append(1)
if si % 2 == 0:
    if S[0] == '#':
        sint = [0] + sint
    else:
        sint.append(0)
    si = si + 1
elif S[0] == '#':
    sint = [0] + sint + [0]
    si = si + 2
#print(sint)

def count2(numSB,numSW):
    if len(sint) <= 2:
        return 0
    sint.pop(0)
    sint.pop()
    
    #この状態で偶数番号は黒、奇数番号は白(ゼロスタート)
    # numSB = 0
    # numSW = 0
    # for i,v in enumerate(sint):
        
    #     if i % 2 == 0:
    #         numSB = numSB + v
    #     else:
    #         numSW = numSW + v
    if len(sint) <= 2:
        return 0
    b = sint.pop(0)
    w = sint.pop()
    
    numSR =  b + w
    #print(numSB,numSW,numSR)
    if numSB <= numSW and numSB <= numSR:
        return numSB
    elif numSW <= numSB and numSW <= numSR:
        return numSW
    else:
        return numSR + count2(numSB-b,numSW-w)
    # if numSB <= numSW :
    #     return numSB
    # else:
    #     return numSW
    
    

def count(_S,_numSW,_numSB):
    _S = _S.lstrip('.').rstrip('#')
    if len(_S) == 0 :
        return 0
    numSW = _S.count('.')
    numSB = _S.count('#')
    SR = _S.lstrip('#').rstrip('.')
    numSR = len(_S) - len(SR)
    if numSW <= numSB and numSW <= numSR:
        return numSW
    elif numSB <= numSW and numSB <= numSR:
        return numSB
    else:
        return numSR + count(SR)


numSB = 0
numSW = 0
for i,v in enumerate(sint):
    if i % 2 == 1:
        numSB = numSB + v
    else:
        numSW = numSW + v
#print(sint,numSB,numSW)
print(min(numSB,numSW,count2(numSB,numSW)))
'''
22
#####.....####..###...
'''
# #intS = []
# def xor(A,B):
#     #print(A)
#     #print(B)
#     num = 0
#     for i in range(len(A)):
        
#         if A[i]!=B[i] :
#             num = num + 1
#     return num
# # for s in S :
# #     intS.append(0 if s=='.' else 1)
# minC = N

# mask = ['#' for j in range(len(S) + 1)]
# for i in range(len(S) + 1):
#     mask[i] = '.'
#     c = xor(mask[1::],S)
#     if c < minC:
#         minC = c

# print(minC)
# # def dl(_S):


# #     l = '.'
# #     r = '#'
    
# #     _S = _S.lstrip(l).rstrip(r)
# #     print(_S)
# #     if len(_S) == 0:
# #         return 0
# #     _S2W = _S.lstrip(r)
# #     _S2B = _S.rstrip(l)
# #     _S2 =  _S2W if len(_S2W) <= len(_S2B) else _S2B
# #     return len(_S) - len(_S2) + dl(_S2)
# # print(dl(S))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
sint = [0]
si = 0
prev = S[0]

for s in S:
    if prev == s:
        sint[si] = sint[si] + 1
    else:
        prev = s
        si = si + 1
        sint.append(1)
if si % 2 == 0:
    if S[0] == '#':
        sint = [0] + sint
    else:
        sint.append(0)
    si = si + 1
elif S[0] == '#':
    sint = [0] + sint + [0]
    si = si + 2
#print(sint)

def count2(numSB,numSW):
    if len(sint) <= 4:
        return 0
    sint.pop(0)
    sint.pop()
    
    #この状態で偶数番号は黒、奇数番号は白(ゼロスタート)
    # numSB = 0
    # numSW = 0
    # for i,v in enumerate(sint):
        
    #     if i % 2 == 0:
    #         numSB = numSB + v
    #     else:
    #         numSW = numSW + v
    b = sint.pop(0)
    w = sint.pop()
    
    numSR =  b + w
    #print(numSB,numSW,numSR)
    if numSB <= numSW and numSB <= numSR:
        return numSB
    elif numSW <= numSB and numSW <= numSR:
        return numSW
    else:
        return numSR + count2(numSB-b,numSW-w)
    # if numSB <= numSW :
    #     return numSB
    # else:
    #     return numSW
    
    

def count(_S,_numSW,_numSB):
    _S = _S.lstrip('.').rstrip('#')
    if len(_S) == 0 :
        return 0
    numSW = _S.count('.')
    numSB = _S.count('#')
    SR = _S.lstrip('#').rstrip('.')
    numSR = len(_S) - len(SR)
    if numSW <= numSB and numSW <= numSR:
        return numSW
    elif numSB <= numSW and numSB <= numSR:
        return numSB
    else:
        return numSR + count(SR)


numSB = 0
numSW = 0
for i,v in enumerate(sint):
    if i % 2 == 1:
        numSB = numSB + v
    else:
        numSW = numSW + v
#print(sint,numSB,numSW)
print(min(numSB,numSW,count2(numSB,numSW)))
'''
22
#####.....####..###...
'''
# #intS = []
# def xor(A,B):
#     #print(A)
#     #print(B)
#     num = 0
#     for i in range(len(A)):
        
#         if A[i]!=B[i] :
#             num = num + 1
#     return num
# # for s in S :
# #     intS.append(0 if s=='.' else 1)
# minC = N

# mask = ['#' for j in range(len(S) + 1)]
# for i in range(len(S) + 1):
#     mask[i] = '.'
#     c = xor(mask[1::],S)
#     if c < minC:
#         minC = c

# print(minC)
# # def dl(_S):


# #     l = '.'
# #     r = '#'
    
# #     _S = _S.lstrip(l).rstrip(r)
# #     print(_S)
# #     if len(_S) == 0:
# #         return 0
# #     _S2W = _S.lstrip(r)
# #     _S2B = _S.rstrip(l)
# #     _S2 =  _S2W if len(_S2W) <= len(_S2B) else _S2B
# #     return len(_S) - len(_S2) + dl(_S2)
# # print(dl(S))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
sint = [0]
si = 0
prev = S[0]

for s in S:
    if prev == s:
        sint[si] = sint[si] + 1
    else:
        prev = s
        si = si + 1
        sint.append(1)
if si % 2 == 0:
    if S[0] == '#':
        sint = [0] + sint
    else:
        sint.append(0)
    si = si + 1
elif S[0] == '#':
    sint = [0] + sint + [0]
    si = si + 2
#print(sint)

def count2(numSB,numSW):
    if len(sint) <= 2:
        return 0
    sint.pop(0)
    sint.pop()
    
    #この状態で偶数番号は黒、奇数番号は白(ゼロスタート)
    # numSB = 0
    # numSW = 0
    # for i,v in enumerate(sint):
        
    #     if i % 2 == 0:
    #         numSB = numSB + v
    #     else:
    #         numSW = numSW + v
    b = sint.pop(0)
    w = sint.pop()
    
    numSR =  b + w
    #print(numSB,numSW,numSR)
    if numSB <= numSW and numSB <= numSR:
        return numSB
    elif numSW <= numSB and numSW <= numSR:
        return numSW
    else:
        return numSR + count2(numSB-b,numSW-w)
    # if numSB <= numSW :
    #     return numSB
    # else:
    #     return numSW
    
    

def count(_S,_numSW,_numSB):
    _S = _S.lstrip('.').rstrip('#')
    if len(_S) == 0 :
        return 0
    numSW = _S.count('.')
    numSB = _S.count('#')
    SR = _S.lstrip('#').rstrip('.')
    numSR = len(_S) - len(SR)
    if numSW <= numSB and numSW <= numSR:
        return numSW
    elif numSB <= numSW and numSB <= numSR:
        return numSB
    else:
        return numSR + count(SR)


numSB = 0
numSW = 0
for i,v in enumerate(sint):
    if i % 2 == 1:
        numSB = numSB + v
    else:
        numSW = numSW + v
#print(sint,numSB,numSW)
print(min(numSB,numSW,count2(numSB,numSW)))
'''
22
#####.....####..###...
'''
# #intS = []
# def xor(A,B):
#     #print(A)
#     #print(B)
#     num = 0
#     for i in range(len(A)):
        
#         if A[i]!=B[i] :
#             num = num + 1
#     return num
# # for s in S :
# #     intS.append(0 if s=='.' else 1)
# minC = N

# mask = ['#' for j in range(len(S) + 1)]
# for i in range(len(S) + 1):
#     mask[i] = '.'
#     c = xor(mask[1::],S)
#     if c < minC:
#         minC = c

# print(minC)
# # def dl(_S):


# #     l = '.'
# #     r = '#'
    
# #     _S = _S.lstrip(l).rstrip(r)
# #     print(_S)
# #     if len(_S) == 0:
# #         return 0
# #     _S2W = _S.lstrip(r)
# #     _S2B = _S.rstrip(l)
# #     _S2 =  _S2W if len(_S2W) <= len(_S2B) else _S2B
# #     return len(_S) - len(_S2) + dl(_S2)
# # print(dl(S))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
sint = [0]
si = 0
prev = S[0]

for s in S:
    if prev == s:
        sint[si] = sint[si] + 1
    else:
        prev = s
        si = si + 1
        sint.append(1)
if si % 2 == 0:
    if S[0] == '#':
        sint = [0] + sint
    else:
        sint.append(0)
    si = si + 1
elif S[0] == '#':
    sint = [0] + sint + [0]
    si = si + 2
#print(sint)

def count2(_numSB,_numSW):
    if len(sint) == 0:
        return 0
    sint.pop(0)
    sint.pop()
    if len(sint) == 0:
        return 0
    #この状態で偶数番号は黒、奇数番号は白(ゼロスタート)
    # numSB = 0
    # numSW = 0
    # for i,v in enumerate(sint):
        
    #     if i % 2 == 0:
    #         numSB = numSB + v
    #     else:
    #         numSW = numSW + v
    b = sint.pop(0)
    w = sint.pop()
    
    numSB = _numSB 
    numSW = _numSW 
    numSR =  b + w
    #print(numSB,numSW,numSR)
    if numSB <= numSW and numSB <= numSR:
        return numSB
    elif numSW <= numSB and numSW <= numSR:
        return numSW
    else:
        return numSR + count2(numSB-b,numSW-w)
    
    

def count(_S,_numSW,_numSB):
    _S = _S.lstrip('.').rstrip('#')
    if len(_S) == 0 :
        return 0
    numSW = _S.count('.')
    numSB = _S.count('#')
    SR = _S.lstrip('#').rstrip('.')
    numSR = len(_S) - len(SR)
    if numSW <= numSB and numSW <= numSR:
        return numSW
    elif numSB <= numSW and numSB <= numSR:
        return numSB
    else:
        return numSR + count(SR)


numSB = 0
numSW = 0
for i,v in enumerate(sint):
    if i % 2 == 1:
        numSB = numSB + v
    else:
        numSW = numSW + v
#print(sint,numSB,numSW)
print(count2(numSB,numSW))
'''
#####.....####..###...
'''
# #intS = []
# def xor(A,B):
#     #print(A)
#     #print(B)
#     num = 0
#     for i in range(len(A)):
        
#         if A[i]!=B[i] :
#             num = num + 1
#     return num
# # for s in S :
# #     intS.append(0 if s=='.' else 1)
# minC = N

# mask = ['#' for j in range(len(S) + 1)]
# for i in range(len(S) + 1):
#     mask[i] = '.'
#     c = xor(mask[1::],S)
#     if c < minC:
#         minC = c

# print(minC)
# # def dl(_S):


# #     l = '.'
# #     r = '#'
    
# #     _S = _S.lstrip(l).rstrip(r)
# #     print(_S)
# #     if len(_S) == 0:
# #         return 0
# #     _S2W = _S.lstrip(r)
# #     _S2B = _S.rstrip(l)
# #     _S2 =  _S2W if len(_S2W) <= len(_S2B) else _S2B
# #     return len(_S) - len(_S2) + dl(_S2)
# # print(dl(S))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
def count(_S):
    _S = _S.rstrip('#').lstrip('.')
    if len(_S) == 0 :
        return 0
    numSW = _S.count('.')
    numSB = _S.count('#')
    SR = _S.lstrip('#').rstrip('.')
    numSR = len(_S) - len(SR)
    if numSW <= numSB and numSW <= numSR:
        return numSW
    elif numSB <= numSW and numSB <= numSR:
        return numSB
    else:
        return numSR + count(SR)
print(count(S))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
def count(_S):
    _S = input().rstrip('#').lstrip('.')
    if len(_S) == 0 :
        return 0
    numSW = _S.count('.')
    numSB = _S.count('#')
    SR = _S.lstrip('#').rstrip('.')
    numSR = len(_S) - len(SR)
    if numSW <= numSB and numSW <= numSR:
        return numSW
    elif numSB <= numSW and numSB <= numSR:
        return numSB
    else:
        return numSR + count(SR)
print(count(S))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input().rstrip('#').lstrip('.')
def count(_S):
    # _S = input().rstrip('#').lstrip('.')
    if len(_S) == 0 :
        return 0
    numSW = _S.count('.')
    numSB = _S.count('#')
    SR = _S.lstrip('#').rstrip('.')
    numSR = len(_S) - len(SR)
    if numSW <= numSB and numSW <= numSR:
        return numSW
    elif numSB <= numSW and numSB <= numSR:
        return numSB
    else:
        return numSR + count(SR)
print(count(S))