Problem p02918 (python)

Fixed submission:

N, K = map(int, input().split())
S = input()

ans = 0
for x,y in zip(S, S[1:]):
    if x == y: #同じ方向を見ているとき
        ans += 1
#最大K回まで操作をして、そのたびに幸福な人が二人増える。全員同じ方向を向くとN-1
ans = N-1 if ans+2*K >= N-1 else ans+2*K
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())

S = input()

b = 0
p1 = 0
p2 = 0
for i in range(N):
    if i == 0:
        if S[i] == S[i+1]:
            b += 1
        if S[i] == "L":
            p1 += 1
        else:
            if S[i] == "R" and S[i+1] == "L":
                p2 += 1
    elif 0 < i < N-1:
        if S[i] == S[i+1]:
            b += 1
        if S[i] == "R" and S[i+1] == "L":
            p2 += 1
    elif i == N-1:
        if S[i] == "R":
            p1 += 1

#print(b)
if K <= p2:
    b += 2*K
else:
    b += 2*K
    K -= p2
    b += max(p1, K)

print(min(N-1,b))

Buggy submission (verdict: Runtime Error):

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

S = input()

if N == 1:
    print(0)
    exit()
#左端をLにする場合
now = ""
num_substr = 0
ex = ""
stack = []
for i in range(N):
    if (ex == "L" or ex == "") and S[i] == "R":
        start = i
    if ex == "R" and S[i] == "L":
        end = i-1
        stack.append((start, end))
        num_substr += 1
    ex = S[i]
#左からひっくり返す
if num_substr == 0:
    print(N-1)
    exit()
tmp = min(K,num_substr,N)
num = stack[tmp-1][1]
ss = "L"*(num+1)+S[num+1:]
ll = num

for i in range(num+1,N):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            ll += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            ll += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            ll += 1


#右からひっくり返す
if num_substr == 0:
    print(N-1)
    exit()
tmp = min(K,num_substr,N)
num = stack[len(stack)-tmp][0]
ss = S[:(N-1-num)]+"L"*(N-1-num)

lr = (N-1-num)

for i in range(num):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            lr += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            lr += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            lr += 1


######################################################################
#左端をRにする場合
now = ""
num_substr = 0
ex = ""
stack = []
for i in range(N):
    if (ex == "R" or ex == "") and S[i] == "L":
        start = i
    if ex == "L" and S[i] == "R":
        end = i-1
        stack.append((start, end))
        num_substr += 1
    ex = S[i]
#左からひっくり返す
if num_substr == 0:
    print(N-1)
    exit()
tmp = min(K,num_substr,N)
num = stack[tmp-1][1]
ss = "R"*(num+1)+S[num+1:]
rl = num
#print(ll, tmp)
for i in range(num+1,N):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            rl += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            rl += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            rl += 1


#右からひっくり返す
if num_substr == 0:
    print(N-1)
    exit()
tmp = min(K,num_substr,N)
num = stack[len(stack)-tmp][0]
ss = S[:(N-1-num)]+"R"*(N-1-num)

rr = (N-1-num)
#print(ll, tmp)
for i in range(num):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            rr += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            rr += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            rr += 1


print(max([ll,lr,rl,rr]))

Buggy submission (verdict: Runtime Error):

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

S = input()

if N == 1:
    print(0)
    exit()
#左端をLにする場合
now = ""
num_substr = 0
ex = ""
stack = []
for i in range(N):
    if (ex == "L" or ex == "") and S[i] == "R":
        start = i
    if ex == "R" and S[i] == "L":
        end = i-1
        stack.append((start, end))
        num_substr += 1
    ex = S[i]
#左からひっくり返す
tmp = min(K,num_substr,N)
num = stack[tmp-1][1]
ss = "L"*(num+1)+S[num+1:]
ll = num

for i in range(num+1,N):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            ll += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            ll += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            ll += 1


#右からひっくり返す
tmp = min(K,num_substr,N)
num = stack[len(stack)-tmp][0]
ss = S[:(N-1-num)]+"L"*(N-1-num)

lr = (N-1-num)

for i in range(num):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            lr += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            lr += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            lr += 1


######################################################################
#左端をRにする場合
now = ""
num_substr = 0
ex = ""
stack = []
for i in range(N):
    if (ex == "R" or ex == "") and S[i] == "L":
        start = i
    if ex == "L" and S[i] == "R":
        end = i-1
        stack.append((start, end))
        num_substr += 1
    ex = S[i]
#左からひっくり返す
tmp = min(K,num_substr,N)
num = stack[tmp-1][1]
ss = "R"*(num+1)+S[num+1:]
rl = num
#print(ll, tmp)
for i in range(num+1,N):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            rl += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            rl += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            rl += 1


#右からひっくり返す
tmp = min(K,num_substr,N)
num = stack[len(stack)-tmp][0]
ss = S[:(N-1-num)]+"R"*(N-1-num)

rr = (N-1-num)
#print(ll, tmp)
for i in range(num):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            rr += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            rr += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            rr += 1


print(max([ll,lr,rl,rr]))

Buggy submission (verdict: Runtime Error):

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

S = input()

if N == 1:
    print(0)
    exit()
#左端をLにする場合
now = ""
num_substr = 0
ex = ""
stack = []
for i in range(N):
    if (ex == "L" or ex == "") and S[i] == "R":
        start = i
    if ex == "R" and S[i] == "L":
        end = i-1
        stack.append((start, end))
        num_substr += 1
    ex = S[i]
#左からひっくり返す
tmp = min(K,num_substr)
num = stack[tmp-1][1]
ss = "L"*(num+1)+S[num+1:]
ll = num

for i in range(num+1,N):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            ll += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            ll += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            ll += 1


#右からひっくり返す
tmp = min(K,num_substr)
num = stack[len(stack)-tmp][0]
ss = S[:(N-1-num)]+"L"*(N-1-num)

lr = (N-1-num)

for i in range(num):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            lr += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            lr += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            lr += 1


######################################################################
#左端をRにする場合
now = ""
num_substr = 0
ex = ""
stack = []
for i in range(N):
    if (ex == "R" or ex == "") and S[i] == "L":
        start = i
    if ex == "L" and S[i] == "R":
        end = i-1
        stack.append((start, end))
        num_substr += 1
    ex = S[i]
#左からひっくり返す
tmp = min(K,num_substr)
num = stack[tmp-1][1]
ss = "R"*(num+1)+S[num+1:]
rl = num
#print(ll, tmp)
for i in range(num+1,N):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            rl += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            rl += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            rl += 1


#右からひっくり返す
tmp = min(K,num_substr)
num = stack[len(stack)-tmp][0]
ss = S[:(N-1-num)]+"R"*(N-1-num)

rr = (N-1-num)
#print(ll, tmp)
for i in range(num):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            rr += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            rr += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            rr += 1


print(max([ll,lr,rl,rr]))

Buggy submission (verdict: Runtime Error):

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

S = input()

#左端をLにする場合
now = ""
num_substr = 0
ex = ""
stack = []
for i in range(N):
    if (ex == "L" or ex == "") and S[i] == "R":
        start = i
    if ex == "R" and S[i] == "L":
        end = i-1
        stack.append((start, end))
        num_substr += 1
    ex = S[i]
#左からひっくり返す
tmp = min(K,num_substr)
num = stack[tmp-1][1]
ss = "L"*(num+1)+S[num+1:]
ll = num

for i in range(num+1,N):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            ll += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            ll += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            ll += 1


#右からひっくり返す
tmp = min(K,num_substr)
num = stack[len(stack)-tmp][0]
ss = S[:(N-1-num)]+"L"*(N-1-num)

lr = (N-1-num)

for i in range(num):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            lr += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            lr += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            lr += 1


######################################################################
#左端をRにする場合
now = ""
num_substr = 0
ex = ""
stack = []
for i in range(N):
    if (ex == "R" or ex == "") and S[i] == "L":
        start = i
    if ex == "L" and S[i] == "R":
        end = i-1
        stack.append((start, end))
        num_substr += 1
    ex = S[i]
#左からひっくり返す
tmp = min(K,num_substr)
num = stack[tmp-1][1]
ss = "R"*(num+1)+S[num+1:]
rl = num
#print(ll, tmp)
for i in range(num+1,N):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            rl += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            rl += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            rl += 1


#右からひっくり返す
tmp = min(K,num_substr)
num = stack[len(stack)-tmp][0]
ss = S[:(N-1-num)]+"R"*(N-1-num)

rr = (N-1-num)
#print(ll, tmp)
for i in range(num):
    if i == 0:
        if (ss[i+1] == "R" and ss[i] == "R"):
            rr += 1
    elif i == N-1:
        if (ss[i-1] == "L" and ss[i] == "L"):
            rr += 1
    else:
        if (ss[i-1] == "L" and ss[i] == "L") or (ss[i+1] == "R" and ss[i] == "R"):
            rr += 1


print(max([ll,lr,rl,rr]))