Problem: 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?

Submission attempts (in order):
N, K = [int(_c) for _c in input().split(" ")]
S_orig = input()
point_list = []
for target, non_target in zip(["L", "R"], ["R", "L"]):
    S = list(S_orig)

    start_idx_list = []
    before_char = None
    for i, c in enumerate(S):
        if i == 0:
            start_idx_list.append(i)
            before_char = c
        else:
            if before_char == non_target and c == target:
                start_idx_list.append(i)
            before_char = c
    
    if len(start_idx_list) - 1 >= K:
        point_list.append(len(start_idx_list) - 1)
        continue
        
    longest = 0
    start_idx = -1
    end_idx = -1
    for i in range(len(start_idx_list) - K):
        cur_length = start_idx_list[i + K] - start_idx_list[i] + 1
        if cur_length > longest:
            start_idx = start_idx_list[i]
            end_idx = start_idx_list[i + K]
            longest = cur_length

    for _idx in range(start_idx, end_idx):
        S[_idx] = target
    S = ["N"] + S + ["N"]

    point = 0
    for idx in range(1, N + 1):
        if S[idx] == "L":
            if S[idx + 1] == "L":
                point += 1
        if S[idx] == "R":
            if S[idx - 1] == "R":
                point += 1

    point_list.append(point)

print(max(point_list))

N, K = [int(_c) for _c in input().split(" ")]
S = input()
point_list = []
for target, non_target in zip(["L", "R"], ["R", "L"]):
    S = list(S)

    start_idx_list = []
    before_char = None
    for i, c in enumerate(S):
        if i == 0:
            start_idx_list.append(i)
            before_char = c
        else:
            if before_char == non_target and c == target:
                start_idx_list.append(i)
            before_char = c

    longest = 0
    start_idx = -1
    end_idx = -1
    for i in range(len(start_idx_list) - K):
        cur_length = start_idx_list[i + K] - start_idx_list[i] + 1
        if cur_length > longest:
            start_idx = start_idx_list[i]
            end_idx = start_idx_list[i + K]
            longest = cur_length

    for _idx in range(start_idx, end_idx):
        S[_idx] = target
    S = ["N"] + S + ["N"]

    point = 0
    for idx in range(1, N + 1):
        if S[idx] == "L":
            if S[idx + 1] == "L":
                point += 1
        if S[idx] == "R":
            if S[idx - 1] == "R":
                point += 1

    point_list.append(point)

print(max(point_list))

N, K = [int(_c) for _c in input().split(" ")]
S_orig = input()
point_list = []
for target, non_target in zip(["L", "R"], ["R", "L"]):
    S = list(S_orig)

    start_idx_list = []
    before_char = None
    for i, c in enumerate(S):
        if i == 0:
            start_idx_list.append(i)
            before_char = c
        else:
            if before_char == non_target and c == target:
                start_idx_list.append(i)
            before_char = c
    start_idx_list.append(len(start_idx_list) - 1)
    if len(start_idx_list) - 1 <= K:
        point_list.append(len(S) - 1)
        continue

    longest = 0
    start_idx = -1
    end_idx = -1
    for i in range(len(start_idx_list) - K):
        cur_length = start_idx_list[i + K] - start_idx_list[i] + 1
        if cur_length > longest:
            start_idx = start_idx_list[i]
            end_idx = start_idx_list[i + K]
            longest = cur_length

    for _idx in range(start_idx, end_idx):
        S[_idx] = target
    S = ["N"] + S + ["N"]

    point = 0
    for idx in range(1, N + 1):
        if S[idx] == "L":
            if S[idx + 1] == "L":
                point += 1
        if S[idx] == "R":
            if S[idx - 1] == "R":
                point += 1

    point_list.append(point)

print(max(point_list))

Verdicts: Wrong Answer

Wrong Answer

Accepted