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:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""


N, K =  map(int, sys.stdin.readline().split())
L =  list(sys.stdin.readline().rstrip())

target = 0
for a, b in zip(L, L[1::]):
    if a != b:
        target += 1

print(min(N - 1, N - (target - 2 * K) - 1))

Submission status: Accepted

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

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

main()

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""
n,k =  map(int, sys.stdin.readline().split())
s=  list(sys.stdin.readline().rstrip())
ans=0

for i in range(1,n):
        if s[i] == s[i-1]:
                ans +=1
print(min(ans+k*2,n-1))
#連続するLRをグループ単位で見るのがポイント
#n-1(限度) - 異なるグループの個数(=グループの個数)
#初期値＋2k（1回の操作で最大＋２）とn-1のminをとる

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

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

Submission status: Accepted

Solution code:
from itertools import groupby

n,k = map(int,input().split())
s = [0] + list(input())
ans = 0
for i in range(1,n):#調べる対象はs[2]からs[n-1]まで
    if s[i] == s[i+1]:
        ans += 1

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

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

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

ans = 0
for i in range(N-1):
    if s[i] == "L" and s[i+1] == "L":
        ans += 1
    elif s[i] == "R" and s[i+1] == "R":
        ans += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

cnt -= K*2
if cnt < 0: cnt = 0
print (len(S) - cnt -1)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
#LR数できまる.
#両側が同じときはさらにもう一つ減る

#現在のansをはかりKの分2を足す
ans = 0
pre = S[0]
for i in range(N-1):
    if pre == S[i+1]:
        ans += 1
    pre = S[i+1]

ans = min([N-1, ans+2*K])

print(ans)

Submission status: Accepted

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

point = n - (S.count('RL') + S.count('LR')) - 1

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

Submission status: Accepted

Solution code:
# 2019/09/08


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

cnt=0
res=0
for i in range(n-1):
    if s[i]==s[i+1]:
        res+=1
    else:
        cnt+=1
res+=min(cnt,k)*2
res=min(n-1,res)

print(res)

Submission status: Accepted

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

    ary = [0]
    t = S[0]
    for s in S:
        if s == t:
            ary[-1] += 1
        else:
            ary.append(1)
            t = s

    ans = sum(ary[:K*2+1])-1
    for i in range(K*2+1, len(ary)):
        ans += ary[i]-1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

streak = 0

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

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

Submission status: Accepted

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

x = S.count("RL")
y = int(S[0] == "L") + int(S[-1] == "R")
    
print(min(N - 1, N - (x * 2 + y) + K * 2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=input()
happy=0
cur=s[0]
for i in range(1,n):
  if s[i]==cur:
    happy=happy+1
  else:
    cur=s[i]

print(min(happy+2*k,n-1))

Submission status: Accepted

Solution code:
import sys

if __name__ == '__main__':
    N, K = map(int, sys.stdin.readline().split())
    S = sys.stdin.readline()
    # ans = 0
    # for i in range(N-1):
    #     if S[i] == S[i+1]:
    #         ans += 1
    ans = N - 1 - S.count('LR') - S.count('RL')
    ans = min(N-1, ans+K*2)
    print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, K = map(int, input().split())
    S = input()
    # ans = 0
    # for i in range(N-1):
    #     if S[i] == S[i+1]:
    #         ans += 1
    ans = N - 1 - S.count('LR') - S.count('RL')
    ans = min(N-1, ans+K*2)
    print(ans)

Submission status: Accepted

Solution code:
import sys

if __name__ == '__main__':
    N, K = map(int, sys.stdin.readline().split())
    S = input()
    ans = 0
    for i in range(N-1):
        if S[i] == S[i+1]:
            ans += 1
    ans = min(N-1, ans+K*2)
    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import copy

sys.setrecursionlimit(1000000)

# input = sys.stdin.readline
# from collections import defaultdict
# import bisect

# ~~~~~~~~~~~~~~~~~~~~~_(＾～＾ ｣ ∠)_~~~~~~~~~~~~~~~~~~~~~

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

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

print(ans + min(bad, 2 * K))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=input()
happy=0
seq=[]
cur=s[0]
seq.append(cur)
for i in range(1,n):
  if s[i]!=cur:
    seq.append(s[i])
    cur=s[i]
  else:
    happy=happy+1

if len(seq)%2==1:
  rev=len(seq)//2
  print(happy+min(rev,k)*2)
else:
  rev=len(seq)//2
  if rev>k:
    print(happy+k*2)
  else:
    print(happy+(rev-1)*2+1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from heapq import heappop, heappush
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7
INF = float('inf')

def sol():
    N, K = map(int, input().split())
    S = input().rstrip()

    if N == 1:
        print(0)
        return

    # N - 連結成分数
    # 1回の反転で，連結成分数は1または2減る
    # 反転箇所が端の場合は1でそれ以外は2
    # 中央から順に反転

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

    for _ in range(K):
        if ans < N - 2:
            ans += 2
        elif ans == N - 2:
            ans += 1
        elif ans == N - 1:
            break

    print(ans)

sol()

Submission status: Accepted

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

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
    ans += K*2
    if ans >= N:
        ans = N-1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,K = II()
S = str(input())

count = 0
ans = 0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        count += 1
    else:
        ans += 1
count += 1

n = (count-1)//2
if count%2==0:
    if n+1<=K:
        ans += 2*n+1
    else:
        ans += 2*K
else:
    if n<=K:
        ans += 2*n
    else:
        ans += 2*K

print(ans)

Submission status: Accepted

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

if N == 1:
    print(0)
else:
    for i in range(N - 1):
        if S[i+1] != S_2[-1]:
            S_2.append(S[i+1])
        if S[i+1] == S[i]:
            ans += 1
    
    num_lr = len(S_2)   # L集合・R集合の数の合計
    num = num_lr // 2   # 便宜的に左端がL集合としたとき、R集合だけを操作できる回数
    
    if num_lr == 1:
        ans = ans
    elif num_lr % 2 == 0:
        if num > K:
            ans += (K * 2)
        elif num <= K:
            ans += ((num * 2) - 1)
    elif num_lr % 2 == 1:
        if num >= K:
            ans += (K * 2)
        elif num < K:
            ans += (num * 2)
    
    print(ans)

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
print(min(ans + 2 * K, N - 1))

Submission status: Accepted

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

Submission status: Accepted

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

dup = 0
for i in range(1,N):
    if S[i] == S[i-1]:
        dup += 1
#print(min(N-1, dup + K*2))
print(min(N-1,dup+K*2))

Submission status: Accepted

Solution code:
import bisect

def turn(S,l,r):
    S1=S[:l]
    S2=S[l:r+1][::-1]
    S3=S[r+1:]
    S2=S2.replace('L',' ')
    S2=S2.replace('R','L')
    S2=S2.replace(' ','R')
    return S1+S2+S3

def score(S):
    ans=0
    for i in range(N):
        if((i<N-1 and S[i]==S[i+1]=='R') or (i>0 and S[i]==S[i-1]=='L')):
            ans+=1
    return ans

N,K=map(int,input().split())
S=input()
L=[];R=[]
c0=S[0]

for i in range(1,N):
    c=S[i]
    if c0!=c=='L':
        L.append(i)
    elif c0!=c=='R':
        R.append(i)
    c0=c

lenL,lenR=len(L),len(R)

for i in range(K):
    if i<min(lenL,lenR):
        S=turn(S,min(L[i],R[i]),max(L[i],R[i])-1)
    elif i==min(lenL,lenR)<max(lenL,lenR):
        if lenL>lenR:
            S=turn(S,L[lenL-1],N-1)
        elif lenL<lenR:
            S=turn(S,R[lenR-1],N-1)
    else:
        break

print(score(S))

Submission status: Accepted

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

count = 0
first = 0
end = 0

if s[0] == 'L':
    first = 1

if s[-1] == 'R':
    end = 1

for i in range(len(s)):
    d = s[i]
    if d == 'L' and i>0 and s[i-1]=='L':
        count += 0
    elif d == 'R' and i<n-1 and s[i+1]=='R':
        count += 0
    else:
        count += 1

line = first + end

for i in range(k):
    if count-2 >= line:
        count += -2
    else:
      	count += -1


if count < 1:
    count = 1
    
print(n-count)

Submission status: Accepted

Solution code:
# ABC140D - Face Produces Unhappiness
def main():
    N, K = tuple(map(int, input().split()))
    S = input().rstrip()
    ans = min(N - 1, sum(i == j for i, j in zip(S, S[1:])) + K * 2)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC140 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
#(n-1) - 異なっているグループの個数(グループの個数-1)
n,k = LI()
s = input()
cnt = 1
for i in range(1,n):
    if s[i] != s[i-1]:
        cnt += 1
ans = n-1 - max(0,(cnt-1)-2*k)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N, K = map(int, input().split())
S = input()

L = deque([])
R = deque([])

now = S[0]
count = 0
for val in S:
    if now != val:
        if now == "L":
            L.append(count)
        else:
            R.append(count)
        
        now = val
        count = 1
    else:
        count += 1

if now == "L":
        L.append(count)
else:
        R.append(count)

while(K):
    if len(L) == 1 or len(R) == 1:
        break
    
    L[1] += L[0]
    R[1] += R[0]

    L.popleft()
    R.popleft()

    K -= 1

if K:
    if len(L) >= len(R):
        L = [N]
        R = []
    else:
        L = []
        R = [N]

ans = sum([val - 1 for val in L]) + sum([val - 1 for val in R])
print(ans)

Submission status: Accepted

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

happiness = N
if S[0] == "L":
  happiness -= 1
if S[-1] == "R":
  happiness -= 1
for i in range(N-1):
  if S[i:i+2] == "RL":
    happiness -= 2

happiness += 2*K

print(min(N-1, happiness))

Submission status: Accepted

Solution code:
from collections import defaultdict

N, K = [int(x) for x in input().split()]
S = input()

ans = S
target_char = S[0]
replacing = False
for i in range(N):
    if S[i] != target_char:
        ans = ans[0:i] + target_char + ans[i + 1:]
        replacing = True
    else:
        if replacing:
            K -= 1
            replacing = False
    if K == 0:
        break

score = 0
for i in range(N):
    if i != 0 and ans[i] == 'L' and ans[i - 1] == 'L':
        score += 1
    if i != N - 1 and ans[i] == 'R' and ans[i + 1] == 'R':
        score += 1

print(score)

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
for i in range(0, N-1):
    if S[i] == S[i+1]:
        ans += 1
print(min(N-1, ans+K*2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
R = []
L = []
direction = S[0]
cnt = 1
for i in range(1, N):
    if S[i] == direction:
        cnt += 1
    else:
        if direction == 'L':
            L.append(cnt)
        else:
            R.append(cnt)
        cnt = 1
        direction = S[i]
if direction == 'L':
    L.append(cnt)
else:
    R.append(cnt)
# print(L, R)
res = sum(L) - len(L) + sum(R) - len(R)
res += 2 * K
print(min(res, N - 1))

Submission status: Accepted

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

p = 0
for i in range(1, n):
  if s[i] == s[i-1]:
    p += 1
print(min(p+2*k, n-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# LRLRRL
# 123456
# LLLRLL
n, k = map(int, input().split())
s = input()

def rev(s, k):
    n = len(s)
    happy = 0
    for i in range(n):
        if i < n - 1 and s[i] == s[i + 1]:
            happy += 1
    return min(n - 1, happy + 2 * k)

ans = rev(s, k)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
s = input().rstrip()

def count(s):
    cnt = 0
    for i in range(n):
        if i+1 < len(s) and s[i] == 'R' and s[i+1] == 'R':
            cnt += 1
        elif i-1 >= 0 and s[i] == 'L' and s[i-1] == 'L':
            cnt += 1
    return cnt


print(min(count(s)+2*k, n-1))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s = input()
count = 0
s = 'X' + s + 'X'
for i in range(1, n+1):
    #print(f's[i]: {s[i]} s[i-1]: {s[i-1]} s[i+1]:{s[i+1]}')
    if (s[i] == s[i-1] and s[i] == 'L') or \
        (s[i] == s[i+1] and s[i] == 'R'):
        count += 1
print(min(n-1, count + 2*k))

Submission status: Accepted

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

Submission status: Accepted

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

if s[0]=='R':
    s=['L' if i=='R' else 'R' for i in s]
p=[]
for i in range(n-1):
    if s[i]!=s[i+1]:
    	p.append(i)

unti=len(p)/2
kaisu=ceil(len(p)/2)

if kaisu<=k:
    print(n-1)
else:
	# if unti%2==0:
	# 	print(n-1-len(p)+k*2)
	# else:
	print(n-1-len(p)+k*2)
	# for i in range(k:)

Submission status: Accepted

Solution code:
def find(S,K):
    pattern=[]
    cur=None
    cur_len=0
    for i in range(len(S)):
        if cur==None:
            cur=S[i]
            cur_len+=1
        elif cur==S[i]:
            cur_len+=1
        else:
            cur=S[i]
            pattern+=[cur_len]
            cur_len=1
    pattern+=[cur_len]
    #print(pattern)
    if K>=len(pattern)//2:
        return len(S)-1
    else:
        total=0
        for x in pattern:
            total+=x-1
        total+=2*K
        return total
    
n,K=list(map(int,input().strip().split(" ")))
S=input()
print(find(S,K))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=input()
rl=s.count("RL")
lr=s.count("LR")
cnt=0
for i in range(n):
    if (i>0 and s[i]=="L" and s[i-1]=="L") or (i<n-1 and s[i]=="R" and s[i+1]=="R"):
        cnt+=1
for i in range(k):
    if min(rl,lr)>0:
        cnt+=2
        rl-=1
        lr-=1
    elif rl>0:
        cnt+=1
        rl-=1
    elif lr>0:
        cnt+=1
        lr-=1
    else:
        break
print(cnt)

Submission status: Accepted

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

    happy = 0

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

    happy += K * 2
    happy = min(happy, N-1)

    print(happy)

if __name__ == '__main__':
    func()

Submission status: Accepted

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

A = []
for s in S:
    if s == 'L':
        A.append(0)
    else:
        A.append(1)

def count(S):

    s = -1
    next_s = A[0]

    c = 0
    i = 1
    while i <= N:
        prev_s = s
        s = next_s
        if i < N:
            next_s = A[i]
        else:
            next_s = -1

        if s == 0: # L
            if s != prev_s:
                c += 1
        else: # R
            if s != next_s:
                c += 1
        i += 1
    return c

unhappy = count(S)
unhappy -= K * 2
if unhappy <= 0:
    unhappy = 1
print(N - unhappy)

Submission status: Accepted

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

# find the largest subsequence
packet = []
packet_len = 1
direction = S[0]
seq = 1
for i in range(1, N - 1):
    if S[i] == direction:
        seq += 1
    else:
        packet.append(seq)
        seq = 1
        direction = S[i]
        packet_len += 1

if S[N - 1] == direction:
    seq += 1
    packet.append(seq)
else:
    packet.append(seq)
    seq = 1
    direction = S[N - 1]
    packet.append(seq)
    packet_len += 1

#print(packet)
#print(packet_len)

# There is no need to choose the 'most invertible' index (i.e. max_idx) !!!
# Also, there is no need to rewrite the packet

num = 2 * K + 1
if packet_len == 1 or packet_len == 2 or num >= packet_len:
    print(N - 1)
elif num < packet_len:
    rem = packet_len - num
    print(N - rem - 1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC140 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
s = input()
score = 0
for i in range(n):
    if s[i] == 'L':
        if i > 0:
            if s[i-1] == 'L':
                score += 1
    else:
        if i < n-1:
            if s[i+1] == 'R':
                score += 1
print(min(score+2*k,n-1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import re
a = input().split(' ')
N = int(a[0])
K = int(a[1])
S = input()

LR = re.findall(r'LR', S)
RL = re.findall(r'RL', S)

num1 = len(LR)
num2 = len(RL)

if num1 < num2:
  num = num1
else:
  num = num2
  
if K < num:
  num = K
  
if ((num1 - num2 == 1) and num1 <= K) or ((num2 - num1 == 1) and num2 <= K):
  point = num * 2 + 1
else:
  point = num * 2
  
for i in range(N):
  if i != 0:
    if S[i] == S[i-1]:
      point = point + 1


print(point)

Submission status: Accepted

Solution code:
a = [int(i) for i in input().split()]
s = input()
data = []
for i in range(a[0]):
    if s[i] == "L":
        data.append(0)
    else:
        data.append(1)
        
#print(data)
count = 1
for i in range(a[0]-1):
    count += (data[i] + data[i+1])%2

#print(count)

if count > a[1]*2:
    print(a[0]+a[1]*2-count)
else:
    print(a[0]-1)

Submission status: Accepted

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

if n == 1:
  print(0)
  exit()

happy = n
for i in range(n-1):
  if s[i] == 'R' and s[i+1] == 'L':
    happy -= 2

if s[0] == 'L':
  happy -= 1
if s[-1] == 'R':
  happy -= 1

print(min(n-1, happy + 2 * k))

Submission status: Accepted

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

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

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


if __name__ == '__main__':
    d()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

cnt = sum([S[i] != S[i+1] for i in range(N-1)])
ans = (N-1) - cnt
ans += min(2*K, cnt)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n, k = map(int, input().split())
s = input()
s = s.replace('LR', 'L,R').replace('RL', 'R,L').split(',')
s = np.array(list(map(len, s))) - 1
ans = sum(s)
ans += k*2
ans = min(n-1, ans)
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=input()
t=s[0]
c=1
d=[]
k*=2
for i in s[1:]:
    if t!=i and k==0:
        d.append(c)
        c=0
    elif t!=i:
        k-=1
    t=i
    c+=1
d.append(c)
print(sum(d)-len(d))

Submission status: Accepted

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

# 10 1
# LLLLLRRRRRの時、happy + 2 * kだけやったら10になるけど、幸福になる人数は最高でも9人(n-1人)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=input()
t=s[0]
#c=[-1,1][s[0]=="R"]
c=1
d=[]
k*=2
for i in s[1:]:
    if t!=i and k==0:
        d.append(c)
        c=0
    elif t!=i:
        k-=1
        #c*=-1
    t=i
    #c+=[-1,1][i=="R"]
    c+=1
d.append(c)
c=len(d)
chk=0
ans=0
print(sum(d)-len(d))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#Face Produces Unhappiness

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

score = 0
for i in range(N-1):
    if S[i] == S[i+1]: score += 1 
ans = min(score+2*K, N-1)
print (ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
s = input()
# print(n)
# print(k)
# 文字列sの幸福度をカウントする関数
def count_happy(s):
    count = 0
    for i in range(1,len(s)-1):
        if s[i]==s[i-1]:
            count += 1
    return count

# print(count_happy(s))
ans = count_happy(s) + 2*k
ans_final = min(n-1,ans)
print(ans_final)

Submission status: Accepted

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

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

ans +=k*2

if ans>n-1:
    ans = n-1

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=input()
t=s[0]
c=[-1,1][s[0]=="R"]
d=[]
k*=2
for i in s[1:]:
    if t!=i and k==0:
        d.append(c)
        c=0
    elif t!=i:
        k-=1
        c*=-1
    t=i
    c+=[-1,1][i=="R"]
d.append(c)
c=len(d)
chk=0
ans=0
for i in d:
    if (i>0 and chk>0) or (i<0 and chk<0):
        chk+=i
    else:
        if chk!=0: ans+=abs(chk)-1
        chk=i
print(ans+(abs(chk)-1))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7

s = input()		# 文字列の入力  abcdefg...

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

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

print(ans)

Submission status: Accepted

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

s = input()

S = list(s)

num = 0
for i in range(N-1):

    if S[i] == S[i+1]:
        num += 1

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

Submission status: Accepted

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

score = 0
for i in range(N-1):
    if (S[i] == S[i+1]):
        score += 1
ans = min(score+2*K, N-1)

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = [1]+list(map(lambda x:-1 if x == "L" else 1, input()))+[-1]

happy = 0
for i in range(1, N+1):
  if S[i] == S[i+S[i]]:
    happy += 1
rl_inner = 0
for i in range(1, N):
  if S[i] == 1 and S[i+1] == -1:
    rl_inner += 1
rl_outer = (1 if S[1] == -1 else 0) + (1 if S[N] == 1 else 0)
if rl_inner+rl_outer > 1:
  happy += min(rl_inner, K)*2
  if rl_inner < K and rl_outer > 1:
    happy += 1
elif rl_inner == 1:
  happy += 1
print(happy)