Problem p02918 (python)

Fixed submission:
#import numpy as np

N, K = map(int, input().split())
s = input()
if len(s) >= 2 and s[0] == "R" and s[-1] == "R":
    s = s.replace("L", "#").replace("R", "L").replace("#", "R")

s = list(s) 

done = 0

out = 0

#gid_list = []
#gnum_list = []
#gkukan_list = []
#
#cur_gid = 0
#cur_gnum = 0
#cur_gstart = 0

for i in range(N):
    cur_c = s[i]
    
    if s[i] == "L" and i >= 1 and s[i-1] == "L":
        out += 1
    elif s[i] == "R" and i <= N-2 and s[i+1] == "R":
        out += 1

#    if i >= 1:
#        if cur_c != before_c:
#            cur_gend =i-1
#            cur_gkukan = [cur_gstart, cur_gend]
#            gkukan_list.append(cur_gkukan)
#            cur_gstart = i
#
#            gnum_list.append(cur_gnum)
#            cur_gnum = 0
#
#            cur_gid += 1
#    
#    cur_gnum += 1
#    gid_list.append(cur_gid)
#
#    before_c = cur_c
#
#cur_gend =N-1
#cur_gkukan = [cur_gstart, cur_gend]
#gkukan_list.append(cur_gkukan)
#gnum_list.append(cur_gnum)
#len_g = len(gnum_list)



#for i in range(N):
#    if done < K:
#        if 1 <= i <= N-2:
#            # L(RL) -> LLL
#            if s[i-1] == "L" and s[i] == "R" and s[i+1] == "L":
#                s[i] = "L"
#                out += 2
#                done += 1
#    else:
#        break

#print("current", out, done, s)
i = 0
while i < N:
    if done < K:
        if 0 <= i <= N-2:
           # L(RL) -> LLL
            if s[i] == "L" and s[i+1] == "R":
                start_i = i+1
                i += 1

                found = True
                while s[i] == "R":
                    i += 1
                    if i >= N:
                        found = False
                        break
               
                # s[i] == "L" made

                if found:
                    end_i = i-1
                else:
                    #end_i = N-1
                    break
                
                for j in range(start_i, end_i+1):
                    s[j] = "L"

                #print("found", found, start_i, end_i)
                if found:
                    out += 2
                #else:
                #    out += 1
                #    break

                done += 1

                i -= 1 # s[i] == "R" ni modosu
        else:
            pass
            #print("false", i)

        i += 1
    else:
        break


#for i in range(N):
#    if done < K:
#        if 1 <= i <= N-2:
#            # R(LR) -> RRR
#            if s[i-1] == "R" and s[i] == "L" and s[i+1] == "R":
#                s[i] = "R"
#                out += 2
#                done += 1
#    else:
#        break

#for i in range(N-1):
#    if done < K:
#        if s[i] == "R" and s[i+1] == "L":
#            s[i] = "L"
#            out += 1
#            done += 1
#    else:
#        break



i = 0
if done < K:
    if s[0] == "R":
        start_i = 0

        found = True
        while s[i] == "R":
            end_i = i
            i += 1
            if i >= N:
                found = False
                break

        if found:
            for j in range(start_i, end_i+1):
                s[j] = "L"
            out += 1
            #print("test0")
            
        done += 1


i = N-1
if done < K:
    if s[i] == "R":
        #print("test3", out)
        end_i = i

        found = True
        while s[i] == "R":
            start_i = i
            i -= 1
            if i <= -1:
                found = False
                break

        if found:
            #start_i = i-1
            for j in range(start_i, end_i+1):
                s[j] = "L"
            out += 1
            #print("test0")
            
        done += 1

print(out)

Buggy submission (verdict: Runtime Error):
#import numpy as np

N, K = map(int, input().split())
s = list(input()) 

done = 0


out = 0

#gid_list = []
#gnum_list = []
#gkukan_list = []
#
#cur_gid = 0
#cur_gnum = 0
#cur_gstart = 0

for i in range(N):
    cur_c = s[i]
    
    if s[i] == "L" and i >= 1 and s[i-1] == "L":
        out += 1
    elif s[i] == "R" and i <= N-2 and s[i+1] == "R":
        out += 1

#    if i >= 1:
#        if cur_c != before_c:
#            cur_gend =i-1
#            cur_gkukan = [cur_gstart, cur_gend]
#            gkukan_list.append(cur_gkukan)
#            cur_gstart = i
#
#            gnum_list.append(cur_gnum)
#            cur_gnum = 0
#
#            cur_gid += 1
#    
#    cur_gnum += 1
#    gid_list.append(cur_gid)
#
#    before_c = cur_c
#
#cur_gend =N-1
#cur_gkukan = [cur_gstart, cur_gend]
#gkukan_list.append(cur_gkukan)
#gnum_list.append(cur_gnum)
#len_g = len(gnum_list)



#for i in range(N):
#    if done < K:
#        if 1 <= i <= N-2:
#            # L(RL) -> LLL
#            if s[i-1] == "L" and s[i] == "R" and s[i+1] == "L":
#                s[i] = "L"
#                out += 2
#                done += 1
#    else:
#        break

#print("current", out, done, s)
i = 0
while i < N:
    if done < K:
        if 0 <= i <= N-2:
           # L(RL) -> LLL
            if s[i] == "L" and s[i+1] == "R":
                start_i = i+1
                i += 1

                found = True
                while s[i] == "R":
                    i += 1
                    if i >= N:
                        found = False
                        break
               
                # s[i] == "L" made

                if found:
                    end_i = i-1
                else:
                    #end_i = N-1
                    break
                
                for j in range(start_i, end_i+1):
                    s[j] = "L"

                #print("found", found, start_i, end_i)
                if found:
                    out += 2
                #else:
                #    out += 1
                #    break

                done += 1

                i -= 1 # s[i] == "R" ni modosu
        else:
            pass
            #print("false", i)

        i += 1
    else:
        break


for i in range(N):
    if done < K:
        if 1 <= i <= N-2:
            # R(LR) -> RRR
            if s[i-1] == "R" and s[i] == "L" and s[i+1] == "R":
                s[i] = "R"
                out += 2
                done += 1
    else:
        break

for i in range(N-1):
    if done < K:
        if s[i] == "R" and s[i+1] == "L":
            s[i] = "L"
            out += 1
            done += 1
    else:
        break



i = 0
if done < K:
    if s[0] == "R":
        start_i = 0
        i += 1

        found = True
        while s[i] == "R":
            i += 1
            if i >= N:
                found = False
                break

        if found:
            end_i = i-1
            for j in range(start_i, end_i+1):
                s[j] = "L"
            out += 1
            #print("test0")
            
        done += 1


i = N-1
if done < K:
    if s[i] == "L":
        end_i = i
        i -= 1

        found = True
        while s[i] == "L":
            i -= 1
            if i >= -1:
                found = False
                break

        if found:
            start_i = i-1
            for j in range(start_i, end_i+1):
                s[j] = "R"
            out += 1
            #print("test0")
            
        done += 1

print(out)