Problem:


Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

You can perform the following operation any number of times between 0 and K (inclusive):

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7



Sample input:
6 1
LRLRRL


Sample output:
3

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s = str(input())
ans = 0
for i in range(n-2):
    if s[i + 1] == 'L':
        if s[i] == 'L':
            ans += 1
    if s[i + 1] == 'R':
        if s[i + 2] == 'R':
            ans += 1
if s[0] == 'R':
    if s[1] == 'R':
        ans += 1
if s[n-1] == 'L':
    if s[n - 2] == 'L':
        ans += 1
ans += k * 2
print(min(n-1,ans))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n,k = [int(i) for i in readline().split()]
s = input()

ans = 0
for i,j in zip(s,s[1:]):
    if i == j: ans += 1

#assert (n-1-ans)%2 == 0
print(min(n-1,ans+2*k))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
check = 0
S = input()
for i in range(N-1):
  if S[i] != S[i+1]:
    check += 1
ryouiki_num = check + 1
saiteki_ryouiki = ryouiki_num - 2*K
if saiteki_ryouiki < 1:
  print(N-1)
else:
  print(N-saiteki_ryouiki)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
S = input()

repulsion_end = 0
if S[ 0]=='L': repulsion_end+=1
if S[-1]=='R': repulsion_end+=1

repulsion = 0
back2back = 0
for i in range(N-1):
    if S[i]=='R' and S[i+1]=='L': repulsion+=1
    if S[i]=='L' and S[i+1]=='R': back2back+=1

remove_repulsion = min(K, back2back, repulsion)
back2back -= remove_repulsion
K -= remove_repulsion
repulsion -= remove_repulsion

if K>0 and back2back>0:
    unhappy=1
elif K>0 and repulsion>0 and repulsion_end<2:
    repulsion_end += 1
    repulsion -=1
    unhappy=repulsion*2+repulsion_end
else:
    unhappy=repulsion*2+repulsion_end

print(N-unhappy)

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split()]
s = input()
s_split = []
i = 0
while i < n:
    j = i
    while j < n and s[i] == s[j]:
        j += 1
    s_split.append(s[i:j])
    i = j
i_hp = sum([len(ss) - 1 for ss in s_split])
rs = [ss for i, ss in enumerate(s_split) if i != 0 and i != len(s_split) - 1 and ss[0] == 'R']
ls = [ss for i, ss in enumerate(s_split) if i != 0 and i != len(s_split) - 1 and ss[0] == 'L']
initial_good = s_split[0][0] == 'R'
final_good = s_split[-1][-1] == 'L'
max_flip = max(len(rs), len(ls))
if max_flip >= k:
    print(i_hp + 2 * k)
else:
    print(n - 1)

Submission status: Accepted

Solution code:
n, k = (int(i) for i in input().split())
S = input()
ans = 0
before = S[0]
l = []
c = 0
for s in S:
    if s != before:
        l.append(c)
        c = 1
    else:
        c += 1
    before = s
l.append(c)

ans = n-len(l)+k*2
if ans > n-1:
    ans = n-1
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
s = list(input())

bf = s[0]
kyoukai = 0
sum = 0
for i in range(1,len(s)):
    if bf != s[i]:
        kyoukai += 1
    else:
        sum += 1
    bf = s[i]

sum += min(k*2,kyoukai)
print(sum)

Submission status: Accepted

Solution code:


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

S1 = list(S)
# L → R に変換

lim = 0
flag = False
for i in range(N):
    if S1[i] == "L":
        S1[i] = "R"
        flag = True
    elif flag :
        lim += 1
        flag = False

    if lim == K:
        break
# print(S1)

S2 = list(S)
lim = 0
flag = False
for i in range(N):
    if S2[i] == "R":
        S2[i] = "L"
        flag = True
    elif flag:
        lim += 1
        flag = False

    if lim == K:
        break

anss =[0,0]
for i,s in enumerate([S1,S2]):
    Rcot = 0
    Lcot = 0
    for k in range(N - 1):  #Rのカウント
        if s[k] == "R" and  s[k + 1] == "R":
            Rcot += 1
    for k in range(1,N ):  #Rのカウント
        if s[k ] == "L" and  s[k -1] == "L":
            Rcot += 1
    anss[i] = Rcot + Lcot
print(max(anss))


# before = S[0]
# acc = []
# cot = 1
# for c in S[1:]:
#     if c == before:
#         cot += 1
#     else:
#         acc.append(cot )
#         cot = 1
#     before = c
# acc.append(cot )
#
# print(acc)
# cot  = 0
# Sl = list(S)
# for i,temp in enumerate(acc):
#     if i % 2 == 0:
#         for j in range()
#     cot += temp
#
#


# evens = (acc[::2])
# odds = (acc[1::2])
#
#
# evens_sorted = sorted(evens,reverse=True)
# el = min(K , len(evens_sorted))
#
# odds_sorted = sorted(odds,reverse=True)
# ol = min(K , len(odds_sorted))
#
#
# even_sum = sum(evens) + sum(odds_sorted[:ol]) + ol
# odd_sum = sum(odds)+ sum(evens_sorted[:el]) + el
#
# print(max(even_sum,odd_sum))

Submission status: Accepted

Solution code:

n, k = map(int,input().split())
s = list(input())
cn = [0]*(n-1)
for i in range(n-1):
    if s[i] == s[i+1]:
        cn[i] = 1
if (n-1 - sum(cn)) <= 2*k:
    print(n-1)
else:
    print(sum(cn)+k*2)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
s = input()
ans = 0
for i in range(n-1):
  if s[i] == s[i+1]:
    ans += 1
for i in range(k):
  if ans < n-2:
    ans += 2
  elif ans == n-2:
    ans += 1
  else:
    pass
print(ans)

Submission status: Accepted

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

ans = 0
RL = 0
LR = 0

for i in range(N-1):
    if S[i] == S[i+1]:
        ans += 1
    elif S[i] == "R" and S[i+1] == "L":
        RL += 1
    elif S[i] == "L" and S[i+1] == "R":
        LR += 1
if RL != 0 and LR != 0 and LR <= RL:
    ans += 2 * min(K, LR)
elif RL != 0 and LR != 0 and LR-RL == 1:
    ans += min(2*K, 2*RL+1)
elif RL != 0 and LR != 0:
    ans += 2 * min(K, RL+1)
else:
    ans = N-1
print(ans)

Submission status: Accepted

Solution code:
n1, n2 = [int(i) for i in input().split()]

s1 = input()

c1 = ""
r1 = 0

for i1 in range(n1):
    if i1 == 0:
        c1 = s1[0]
    elif i1 == n1 - 1:
        if c1 == s1[i1]:
            r1 += 1
        else:
            c1 = s1[i1]
    else:
        if c1 == s1[i1]:
            r1 += 1
        else:
            c1 = s1[i1]

r1 += (2 * n2)
if r1 >= n1:
    r1 = n1 - 1
print(r1)

Submission status: Accepted

Solution code:
N,K =list(map(int,input().split()))
S = input()
S2 = [(1 if S[i]+S[i+1]== 'LR' else -1) if S[i]!= S[i+1] else 0 for i in range(N-1)]
score = sum([1 if S[i]==S[i+1] else 0 for i in range(N-1)])
S3 = min(S2.count(1),S2.count(-1))
if S3 >= K:
    score += K*2
else:
    if S2.count(1) == S2.count(-1):
        score += S3*2
    else:
        score += S3*2+1
print(score)

Submission status: Accepted

Solution code:
n,k = list(map(int, input().split()))
s = input()

diff = 0

for i in range(n-1):
    if s[i] != s[i+1]:
        diff += 1

print(n - (diff - k*2) -1 if (diff - k*2) >= 0 else n-1)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
max_happy = N-1
init_happy = 0
for i in range(1, N-1):
  if S[i]=="L" and S[i-1]=="L":
    init_happy += 1
  elif S[i]=="R" and S[i+1]=="R":
    init_happy += 1

if N>1:
  if S[0]=="R" and S[1]=="R":
    init_happy += 1

  if S[-1]=="L" and S[-2]=="L":
    init_happy += 1

ans = min(max_happy, init_happy+K*2)

print(ans)

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    

n,k=map(int,input().split())
s=input()
l =[]
cnt=0

for i in range(len(s)-1):
	if s[i]==s[i+1]: 
		cnt+=1
		
d=len(s)-1-cnt
while d>1 and k>0:
	cnt+=2
	d-=2
	k-=1
if d==1 and k>0:
	cnt+=1
	d-=1
	
print(cnt)

Submission status: Accepted

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

r_area = 0
l_area = 0
flag = None
for i, s in enumerate(S):
    if s == "R":
        if flag == "R":
            continue
        flag = "R"
        r_area += 1
    if s == "L":
        if flag == "L":
            continue
        flag = "L"
        l_area += 1

small_area = min(r_area, l_area)
large_area = max(r_area, l_area)
if small_area <= K:
    print(N-1)
else:
    print(N-(small_area+large_area-2*K))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
ct = 0
ans = 0
st = 0
for i in range(N-1):
    if S[i] == S[i+1]:
        st += 1
    else:
        ans += st
        st = 0
        ct += 1
else:
    ans += st

if K * 2 < ct:
    print(ans+2*K)
else:
    print(N-1)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
lens = []

l = 1
for i in range(1, N):
  if S[i] != S[i-1]:
    lens.append(l)
    l = 1
  else:
    l += 1
lens.append(l)

ans = 0
ans += N - len(lens) - 1
ans += min(K*2+1, len(lens))

print(ans)

Submission status: Accepted

Solution code:
import sys
n=list(map(int,input().split()))
s=input()
a,b,c=[],s[0],0
if n[0]==1:
    print('0')
    sys.exit()
for i in range(n[0]-1):
    if b==s[i+1]:
        c+=1
    else:
        a.append(c)
        c=0
    b=s[i+1]
if c!=0:
    a.append(c)
if s[-2]==s[-1]:
    a[-1]+=1
else:
    a.append(1)
if n[1]>=len(a)//2:
    print(n[0]-1)
else:
    print(sum(a)+2*n[1]-1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

def get_sequence(arr):
    if len(arr) == 0:
        return [], []
    elements = []
    nums = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            elements.append(prev)
            nums.append(n)
            prev = c
            n = 1
    elements.append(prev)
    nums.append(n)
    return elements, nums

N, K = inpl()
S = input().rstrip('\n')

elements, nums = get_sequence(S)
loss = len(nums)
loss -= K*2
if loss < 1:
    loss = 1
print(N-loss)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline

def runLength(s):
    a = []
    for c in s:
        if len(a)==0 or a[-1][0]!=c:
            a.append([c,1])
        else:
            a[-1][1] += 1
    return a

def main():
    N,K = map(int, input().split())
    S = input().strip()
    ans = 0
    for i,c in enumerate(S):
        if c == "L":
            if i > 0 and S[i-1] == "L":
                ans += 1
        if c == "R":
            if i < N-1 and S[i+1] == "R":
                ans += 1
    A = runLength(S)
    A.sort(key=lambda x:x[1], reverse=True)
    for i in range(K):
        if i < len(A):
            ans += 2
            if ans >= N:
                break
    if ans >= N:
        ans = N-1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import groupby
N, K = map(int,input().split())
S = input()
L = []
for i in range(len(S)):
  L.append(S[i])
sum = 0
for key, value in groupby(L):
    sum += len(list(value))-1

print(min(N-1,sum+K*2))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
kazulist = []
now = 1
for i in range(1, N):
    if S[i] == S[i-1]:
        now += 1
    else:
        kazulist.append(now)
        now = 1
kazulist.append(now)
lenkazu = len(kazulist)
sumkazu = sum(kazulist)
spl = max(lenkazu - 2 * K , 0)
if spl >= 1:
    print(sumkazu - spl)
else:
    print(N-1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N, K = map(int,input().split())
s = input().split() 
s = list(s[0])
renketu=[]
cnt=1
for i in range(1,N):
    if s[i] != s[i-1]:
        renketu.append(cnt)
        cnt=1
    else:
        cnt+=1
renketu.append(cnt)
cnt=0
ans=0
for i in range(len(renketu)):
    if i%2==1:
        cnt+=1
    if cnt <= K:
        ans+=renketu[i]
    else:
        ans+=renketu[i]-1
print(ans-1)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

pre = ''
cnt = 0
f = False
K_cnt = 0

for i in range(N):
    k = S[i]
    if pre==k:
        cnt += 1
        #flag立っていてかつ最後までそんまま
        if i==N-1:
            if K_cnt<K:
                if f:
                    cnt += 1
    else:
        if f:
            if K_cnt<K:
                cnt += 2
                K_cnt += 1
                f = False
        else:
            if not i==0:
                f = True
                if i==N-1:
                    if K_cnt<K:
                            cnt += 1
    pre = k
print(cnt)

Submission status: Accepted

Solution code:
N, k = map(int, input().split())
b = input()


score = 0
up1_num = 0
up2_num = 0

switch = []

prev = None
cnt =0
for i in range(N):
  if prev == None:
    prev = b[i]
    cnt = 1
    continue
    
  if prev==b[i]:
    cnt += 1
  else:
    score += (cnt-1)
    cnt = 1
    prev = b[i]
    switch.append(i)

if cnt != 0:
  score += (cnt-1)
  
  
if k <= len(switch)/2:
  score += 2*k
else:
  score += 2*(len(switch)//2)
  if len(switch)%2==1:
    score += 1
    
print(score)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s = input()
ans = 0
for c1, c2 in zip(s, s[1:]):
    if c1 == 'R' and c2 == "R":
        ans += 1
for c1, c2 in zip(s[-1::-1], s[-2::-1]):
    if c1 == 'L' and c2 == "L":
        ans += 1
print(min(k * 2 + ans, n - 1))

Submission status: Accepted

Solution code:

n,k = map(int,input().split())
s = input()
l = [0 for i in range(len(s)+1)]
r = [0 for i in range(len(s)+1)]

count = 0
kukann = 1
for i in range(0,len(s)-1):
    if s[i] != s[i+1]:
        kukann += 1
    if s[i] == 'L' and s[i+1] == 'L':
        count += 1
    elif s[i] == 'R' and s[i+1] == 'R':
        count += 1
if ((kukann-2)//2) >= k:
    count += k*2
elif kukann % 2 == 0:
    if ((kukann-2)//2) >= 0:
        count += ((kukann-2)//2) * 2
    if ((kukann-2)//2) < k and kukann != 1:
        count += 1
else:
    count += min(k*2,(kukann-1))
print(count)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
ls = []
l = 1
ans = 0
count = 0
pcount = 0
flag = False
for i in range(N):
  if S[i]=='L':
    if i!=0 and S[i]==S[i-1]:
      ans += 1
  else:
    if i!=N-1 and S[i]==S[i+1]:
      ans += 1
  
  if i!=N-1 and S[i]!=S[i+1]:
    count += 1
#print(ans, count, pcount)
for i in range(K):
  if count>1:
    ans += 2
    count -= 2
    continue
  elif count==1:
    ans += 1
    break
print(ans)

Submission status: Accepted

Solution code:
[N,K] = list(map(int,input().split()))
#[N,K] = [13,3]
S = str(input())
tmp = S[0]
num = []
num_tmp = 0
for i in range(N):
    if S[i] == tmp:
        num_tmp += 1
    else:
        tmp = S[i]
        num.append(num_tmp)
        num_tmp = 1
    if i == N-1:
        num.append(num_tmp)
ans = 0
for i in range(len(num)):
    ans += num[i] - 1
Kmax = len(num) // 2
if Kmax <= K:
    print(N-1)
else:
    print(2*K+ans)

Submission status: Accepted

Solution code:
from sys import stdin

n, k = [int(x) for x in stdin.readline().rstrip().split()]
s = stdin.readline().rstrip()

ans = 0
tmp = s[0]
ll = [0]
index = 0 
for i in range(len(s)):
    if tmp == s[i]:
        ll[index] += 1
    else:
        tmp = s[i]
        index += 1
        ll.append(1)

#print(ll)

for i in range(len(ll)):
    ans += ll[i]-1

ans += k*2
ans = min(ans, n-1)

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=input()
defa=0
for i in range(n-1):
  if s[i]==s[i+1]:
    defa+=1
print(min((defa+2*k),n-1))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s = input()


c = [1]
prev = s[0]
c_ind = 0

for i in range(1, n):
    if s[i] == prev:
        c[-1] += 1
    else:
        c.append(1)
        c_ind += 1
    prev = s[i]

cur_len = len(c)
cur_sum = sum(c) - cur_len

for _ in range(k):
    if cur_len == 1:
        break
    elif cur_len == 2:
        cur_sum += 1
        break
    else:
        cur_sum += 2
        cur_len -= 2

print(cur_sum)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = list(input())

flag = True
for i in range(1, N):
    if K != 0:
        if flag:
            if S[i-1] != S[i]:
                flag = False
        if not flag:
            S[i] = 'L' if S[i] == 'R' else 'R'
            if i < N - 1:
                if S[i] == S[i+1]:
                    flag = True
                    K -= 1

ans = 0
if S[0] == 'R' and S[1] == 'R':
    ans += 1
for i in range(1, N - 1):
    if S[i] == 'R' and S[i + 1] == 'R':
        ans += 1
    elif S[i] == 'L' and S[i - 1] == 'L':
        ans += 1
if N > 1:
    if S[-1] == 'L' and S[-2] == 'L':
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n, k  = list(map(int, input().split()))
s = list(input())

ans = 0
for i in range(n - 1):
	if s[i] == s[i + 1]:
		ans += 1

cnt = n - ans
print(min(n - 1, ans + k * 2))

Submission status: Accepted

Solution code:
import copy

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

lenLorR, LR, temp, happness = 0, S[0], 1, 0
for i in range(1, N):
    if LR == S[i]:  temp += 1
    else:
        lenLorR += 1
        happness += temp - 1
        LR, temp = S[i], 1
lenLorR += 1
happness += temp - 1

if lenLorR // 2 <= K:   print(N-1)
else:   print(happness + K * 2)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=input()
ans=0
if s[0]=="R" and s[n-1]=="R":
    for i in range(n-1):
        if s[i]=="R":
            if s[i+1]=="R":
                ans+=1
        else:
            if s[i-1]=="L":
                ans+=1
elif s[0]=="R" and s[n-1]=="L":
    for i in range(n):
        if s[i]=="R":
            if s[i+1]=="R":
                ans+=1
        else:
            if s[i-1]=="L":
                ans+=1
elif s[0]=="L" and s[n-1]=="R":
    for i in range(1,n-1):
        if s[i]=="R":
            if s[i+1]=="R":
                ans+=1
        else:
            if s[i-1]=="L":
                ans+=1
else:
    for i in range(1,n):
        if s[i]=="R":
            if s[i+1]=="R":
                ans+=1
        else:
            if s[i-1]=="L":
                ans+=1
print(min(n-1,ans+2*k))

Submission status: Accepted

Solution code:
n, k = (int(x) for x in input().split())
s = list(input())
cnt = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt = cnt + 1
print(min(cnt+k*2, n-1))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s = list(input())

ls = []
c = s[0]
l = 0
for i in range(1, n):
    if s[i] == c:
        l += 1
    else:
        ls.append(l)
        c = s[i]
        l = 0
ls.append(l)
ans = sum(ls)
m = len(ls)
for i in range(k):
    if m == 1:
        break
    elif 3 <= m:
        ans += 2
        m -= 2
    else:
        ans += 1
        m -= 1
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split(' '))
S = input()

lens = {'L':[],'R':[]}

ps = S[0]
l = 1
ans = 0
for s in S[1:]:
	if ps == s:
		ans += 1
		l += 1
	else:
		lens[ps].append(l)
		l = 1
		ps = s
lens[ps].append(l)

nL = len(lens['L'])
nR = len(lens['R'])
if nL > nR:
	ans += 2 * min(K,nL-1)
elif nL == nR:
	ans += 2 * min(K,nL-1)
	if K > nL - 1:
		ans += 1
else:
	ans += 2 * min(K,nR-1)

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
s = input()
ans = 0
for i in range(n):
    if s[i] == "L":
        if i != 0:
            if s[i - 1] == "L":
                ans += 1
    else:
        if i != n - 1:
            if s[i + 1] == "R":
                ans += 1
ans += 2 * k
print(min(ans, n - 1))

Submission status: Accepted

Solution code:
def main():

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

    count = []
    ct = 0
    for i in range(N):
        if i == 0 or S[i] == S[i-1]:
            ct += 1
        else:
            count.append(ct)
            ct = 1
    count.append(ct)

    happiness = 0
    for c in count:
        happiness += c - 1

    n = len(count) - 1
    if 2 * K >= n:
        return N-1
    else:
        return happiness + 2*K


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
s = input()

bad = 0
for i in range(n-1):
    if s[i] != s[i+1]:
        bad += 1

if bad > k*2:
    ans = (n-1) - bad + (k*2)
else:
    ans = n-1

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
ans = 0
temp = 0
for s1, s2 in zip(S, S[1:]):
    temp += 1
    if s1 != s2:
        ans += temp - 1
        temp = 0
ans += temp
ans = min(ans + 2*K, N-1)
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    N,K=map(int,input().split())
    S=input()
    c=0
    for i in range(N):
        if S[i]=='L':
            if i>0:
                if S[i-1]=='L':
                    c+=1
        else:
            if i<N-1:
                if S[i+1]=='R':
                        c+=1
    a=2*K
    print(min(c+a,N-1))
resolve()

Submission status: Accepted

Solution code:
num, k = map(int, input().split())

s = input()


prev = s[0]

ans = 0
chance = 0
prev = ""

for i in range(num-1):
  x = s[i]
  y = s[i+1]
  if x != y:
    chance += 1
  else:
    ans += 1

    
print(ans + min([chance, k*2]))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=list(input())
s=l[0]
m=0
c=1
for i in range(1,n):
  if l[i]!=s:
    c+=1
    s=l[i]
if c//2<=k:
  print(n-1)
else:
  for i in range(1,n-1):
    if l[i]=="R":
      if l[i+1]=="R":
        m+=1
    if l[i]=="L":
      if l[i-1]=="L":
        m+=1
  if l[0]=="R" and l[0]==l[1]:
    m+=1
  if l[-1]=="L" and l[-2]==l[-1]:
    m+=1
  print(m+k*2)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=str(input())
a=[]
now=1
ans=2*k
for i in range(1,n):
  if s[i]==s[i-1]:
    now+=1
  else:
    ans+=now-1
    now=1
ans+=now-1
if ans>n-1:
  print(n-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s_list = input()

#r=lかつr+1=l-1かつr!=r+1となるようなr,lを選ぶと+2点操作になる.
# また、r=n-1かつl!=l-1あるいはl=0かつr!=r+1となるようなr,lを選ぶと+2点操作になる.
#それ以外の場合は必ず0点以下となる.
# つまり、l!=l-1かつl=R,l!=l-1かつl=L,r!=r+1かつr=R,r!=r+1かつr=Lの数をそれぞれ数え、いい感じにマッチングすれば答えが出る.
mismatch_count = {"R":[0, 0], "L":[0, 0]}
ans = 0
for i in range(n):
    if(i < n-1):
        if(s_list[i] != s_list[i+1]):
            mismatch_count[s_list[i]][1] += 1
        elif(s_list[i] == "R"):
            ans += 1
    if(i > 0):
        if(s_list[i] != s_list[i-1]):
            mismatch_count[s_list[i]][0] += 1
        elif(s_list[i] == "L"):
            ans += 1
            
plus_r = min(mismatch_count["R"])
plus_l = min(mismatch_count["L"])
left_r = max(mismatch_count["R"]) - plus_r
left_l = max(mismatch_count["L"]) - plus_l
if(k >= plus_r):
    ans += plus_r * 2
    k -= plus_r
    plus_l -= plus_r
elif(plus_r > 0):
    ans += k * 2
    k = 0
if(k >= plus_l):
    ans += plus_l * 2
    k -= plus_l
elif(plus_l > 0):
    ans += k * 2
    k = 0
if(k >= left_r):
    ans += left_r
    k -= left_r
    left_l -= left_r
elif(left_r > 0):
    ans += k
    k = 0
if(k >= left_l):
    ans += left_l
    k -= left_l
elif(left_l > 0):
    ans += k
    k = 0

print(ans)

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.readline
def main():
    N, K = map( int, input().split())
    S = input()
    now = S[0]
    cnt = 0
    for s in S[1:]:
        if s == now:
            continue
        now = s
        cnt += 1
    if cnt%2 == 0:
        cnt //= 2
        if cnt <= K:
            print(N-1)
            return
        print(N-1-(cnt-K)*2)
    else:
        cnt //= 2
        if cnt + 1 <= K:
            print(N-1)
            return
        print(N-1-(cnt-K)*2-1)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
for i in range(1,N):
    if S[i-1] == S[i]:
        ans += 1

ans += 2 * K

if ans > N-1:
    ans = N-1

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N,K=map(int,input().split())
S=input()
count=0
ans=0
for i in range(N-1):
    if S[i]==S[i+1]:
        ans=ans+1
    elif S[i]!=S[i+1]:
        count=count+0.5
        if K>=count:
            ans=ans+1
print(ans)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
s = list(input())
happy0 = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        happy0 += 1
#print(happy0)
ans = min(happy0+2*k, n-1)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from heapq import heappush, heappop, heapify
from bisect import bisect_left
from itertools import groupby

class Solution:
    def main(self):
        N, K = map(int, input().split())
        S = input()

        counts = [len(list(g)) for k, g in groupby(S)]

        n_thr = len(counts) - 1
        n_thr = max(0, n_thr - 2*K)
        print(N-1-n_thr)


if __name__ == '__main__':
    Solution().main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)

def main():
    n, k = map(int, input().split())
    s = input()
    a = []
    pre = s[0]
    cnt = 0
    for c in s:
        if pre == c:
            cnt += 1
        else:
            a.append(cnt)
            cnt = 1
        pre = c
    a.append(cnt)
    # print(a)
    if len(a) <= 2 * k + 1:
        print(n - 1)
    else:
        a = [sum(a[:2 * k + 1])] + a[2 * k + 1:]
        # print(a)
        print(sum(x - 1 for x in a))

main()

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split()]
S = input()
cnt = 0
for i in range(len(S)-1):
    cnt += S[i] == S[i+1]
print(min(cnt+ 2*K, N-1))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
S = input()
ans = 0
c = 0
s = ''
for i in range(N):
    if S[i] != s:
        c += 1
        s = S[i]
    else:
        ans += 1

while K > 0 and c > 1:
    if c >= 3:
        K -= 1
        c -= 2
        ans += 2
    else:
        K -= 1
        c -= 1
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
def main():    
    n,k = map(int,input().split())
    s = input()
    x = []
    tmp = 0
    for i in range(n-1):
        if s[i] == s[i+1]:
            tmp+= 1
        else:
            x.append(tmp)
            tmp = 0
    x.append(tmp)
    t = len(x)-2
    ans = sum(x)
    if s[0] == s[-1]:
        t = (len(x)-1)//2
        ans += 2*min(t,k)
    else:
        t = len(x)//2-1
        ans += 2*min(t,k)
        if k-t > 0:
            ans += 1
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
c = 0
ans = 0
for i, j in zip(S, S[1:]):
    if i != j:
        c += 1
    else:
        ans += 1
if S[0] != S[-1]:
    c -= 1
c //= 2
if c <= K:
    ans += 2*c
    K -= c
    if K > 0 and S[0] != S[-1]:
        ans += 1
else:
    ans += K * 2
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s = input()

b = 1
for i in range(1, n):
    if s[i] != s[i-1]:
        b += 1
# print(b)

b = max(1, b-2*k)

ans = n - b

print(ans)

Submission status: Accepted

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

# 操作はl, r番目の人以外の幸福状態を変えない。適切に選んで操作をすると1回1人(l=r)か2人増える)
groups = []

nowDirection = S[0]
sameCount = 1
for i in range(1, N):
    if S[i] == nowDirection:
        sameCount += 1
    else:
        groups.append(sameCount)
        nowDirection = S[i]
        sameCount = 1
groups.append(sameCount)

groupCount = len(groups)
ans = sum(groups) - groupCount

for _ in range(K):
    if groupCount >= 3:
        ans += 2
        groupCount -= 2
    elif groupCount == 2:
        ans += 1
        groupCount -= 1
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
N, K = (int(x) for x in input().split(' '))
S = input()

x = [1]
for i in range(1, N):
    if S[i-1] == S[i]:
        x[-1] += 1
    else:
        x.append(1)

if len(x) > 2*K+1:
    ans = sum(x) - len(x) + 2*K
else:
    ans = N - 1
print(ans)

Submission status: Accepted

Solution code:
#ABC140D
N,K = map(int,input().split())
S = input()

p1 = p2 = cnt = 0
for i in range(N-1):
    if S[i] == 'L' and S[i+1] == 'R':
        p1 += 1
    if S[i] == 'R' and S[i+1] == 'L':
        p2 += 1
    if S[i] == 'R' and S[i+1] == 'R':
        cnt += 1
    if S[i] == 'L' and S[i+1] == 'L':
        cnt += 1
#print(cnt,p1,p2)
a1 = min(p1,p2,K)
ans = a1*2 + cnt
if K > a1:
    a2 = min(max(p1,p2)-a1,K-a1)
    ans += a2

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=input()+"."
a,b=[1],1
for i in range(1,n):
  if s[i-1]==s[i]:a[-1]+=1
  else:a.append(1)
s=0
for i in a:s+=max(i,1)-1
l=len(a)
for i in range(k):
  if l==1:break
  if l==2:s+=1;break
  l-=2
  s+=2
print(s)

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split()]
s = input()

ans = 0
lblocks = 0
# if s[0] == "L":
#     lblocks += 1
flag = False
if s[0] == "L":
    flag = True
for i,v in enumerate(s):
    if i == 0:
        continue
    if s[i] == s[i-1]:
        ans += 1
    if s[i] == "L":
        if not flag:
            lblocks += 1
            flag = True
    else:
        flag = False
if lblocks == 0:
    print(n-1)
else:
    if lblocks < k:
        print(n-1)
    else:
        print(ans+k+k)

Submission status: Accepted

Solution code:
N,K=[int(i) for i in input().split()]
S = input()
num = 0
for i in range(N-1):
  if S[i]==S[i+1]: num+=1
print(min(num+2*K,N-1))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s = input().strip()

ls = []
left = 0
right = 0
count = 0
while left < n:
    prev = s[left]
    right = left + 1
    while right <= n:
        if right == n:
            ls.append(right - left)
            left = right
            break
        c = s[right]
        if c == prev:
            right += 1
        else:
            ls.append(right - left)
            left = right
            break

m = len(ls)
if m <= 2 * k:
    print(n - 1)
else:
    print(n - m + 2 * k)

Submission status: Accepted

Solution code:
I = list(map(int, input().split()))
N, K = I[0], I[1]
S = input()

count = 0
flag = False

for i in range(N):
    if i == N-1:
        pass
    elif S[i] == S[i+1]:
        count += 1

need = (N - 1 - count) // 2
if (N - 1 - count) % 2 == 1:
    flag = True

if K > need:
    if flag:
        result = count + need * 2 + 1
    else:
        result = count + need * 2
else:
    result = count + K * 2

print(result)