During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen. Initially the children stood in the order they entered the canteen. However, after a while the boys started feeling awkward for standing in front of the girls in the queue and they started letting the girls move forward each second.
Let's describe the process more precisely. Let's say that the positions in the queue are sequentially numbered by integers from 1 to n, at that the person in the position number 1 is served first. Then, if at time x a boy stands on the i-th position and a girl stands on the (i + 1)-th position, then at time x + 1 the i-th position will have a girl and the (i + 1)-th position will have a boy. The time is given in seconds.
You've got the initial position of the children, at the initial moment of time. Determine the way the queue is going to look after t seconds.
Input
The first line contains two integers n and t (1 ≤ n, t ≤ 50), which represent the number of children in the queue and the time after which the queue will transform into the arrangement you need to find.
The next line contains string s, which represents the schoolchildren's initial arrangement. If the i-th position in the queue contains a boy, then the i-th character of string s equals "B", otherwise the i-th character equals "G".
Output
Print string a, which describes the arrangement after t seconds. If the i-th position has a boy after the needed time, then the i-th character a must equal "B", otherwise it must equal "G".
Examples
Input
5 1
BGGBG
Output
GBGGB
Input
5 2
BGGBG
Output
GGBGB
Input
4 1
GGGB
Output
GGGB
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to simulate a process where boys and girls in a queue can swap if a boy is immediately in front of a girl. At each second, all such adjacent pairs (boy at i, girl at i+1) swap simultaneously? The problem statement: "if at time x a boy stands on the i-th position and a girl stands on the (i + 1)-th position, then at time x + 1 the i-th position will have a girl and the (i + 1)-th position will have a boy." It doesn't explicitly say that swaps happen for all such pairs simultaneously, but typical problems (like Codeforces 266B Queue at the School) describe that at each second, from left to right, each boy (or something) can swap with the next girl, but with concurrency issues. In that known problem (Queue at the School, CF 266B) the description: "at every unit of time, if the i-th person is a boy and the (i+1)-th person is a girl, then they swap positions." But they often handle by scanning left to right and swapping, and then moving i by 2 after a swap to simulate simultaneous? Actually in the known problem, the process is simulated as: for t iterations, we go through positions i from 1 to n-1, if s[i] = 'B' and s[i+1] = 'G', then swap them and increment i by 2 (skip the next position). That is because swaps happen instantly at that second, so after swapping, the boy moves to i+1 and the girl to i, and they shouldn't be swapped again in the same second because the boy is now behind the girl. However, the problem statement here is exactly the same as Queue at the School. Let's verify with examples.
Given Input: n=5, t=1, s=BGGBG.
If we simulate simultaneous swapping (like scanning left to right with skip), we get:
Initial: B G G B G
We go i=1: s[1]='B', s[2]='G' => swap => G B G B G, then skip i to 3 (since i+1=2 swapped, but we skip? Actually after swapping we should increase i by 2 because we shouldn't consider the swapped boy at position 2? The typical algorithm: for i from 0 to n-2: if s[i]=='B' and s[i+1]=='G': swap them and i += 1 (to skip the girl now at i). After swapping, the boy moves to i+1, and the girl moves to i. So if we continue without skipping, at next i (i+1) we might see a boy? But it's the same i? Let's do step: initial index 0 (1-based? We'll use 0-based for convenience). 0:B,1:G => swap => s[0]=G, s[1]=B. Then we need to not consider the pair (1,2) with the boy we just swapped? Actually after swapping, at index 1 we have a boy. The next pair is index 1 and 2. If we increment i by 1 normally, we go to i=1: s[1]=B, s[2]=G? s[2] initially G? After swap, s[2] is still G (unchanged). So we would see B at 1 and G at 2, and swap again => would result in G G B B G? But that would be two swaps in the same second, which may not be intended. The known solution to CF 266B uses a loop: for time in 1..t: for i from 0 to n-2: if s[i]=='B' and s[i+1]=='G': swap, i++ (i.e., skip next). That is to simulate simultaneous: because if we swapped at i, the boy moves to i+1; but then at i+1 and i+2 we could potentially swap again? But is that allowed in the same second? In the description of CF 266B: "At each second, each boy looks at the next person in line. If that person is a girl, they swap positions." The process is simultaneous: all boys look and decide to swap at the same time. So if a boy at i swaps with a girl at i+1, then the boy moves to i+1. But then could the same boy (now at i+1) swap again with a girl at i+2 in the same second? In the typical simultaneous model, no: because at time x, the decision is based on positions at time x. After swapping, the new positions are only effective at time x+1. So each boy swaps at most once per second. So after swapping, the boy should not be considered for further swap in that second. Hence the typical simulation uses scanning left to right and skipping after a swap (to avoid multiple swaps for the same boy). Another approach is to simulate by generating new state: for each second, we can apply rule: For each i from 1 to n-1, if at current state s[i] = 'B' and s[i+1] = 'G', then they will swap to produce next state. But need to ensure that a position is not involved in two swaps. Since swaps are between adjacent pairs (i,i+1), a position can be involved in at most one swap if we treat them as disjoint? Not necessarily; consider B B G: positions: 1:B,2:B,3:G. At time x, pair (2,3): B at 2, G at 3 => they would swap. Pair (1,2): B at 1, B at 2 (since before swap, 2 is B) => no swap. So after swapping, we get B G B? Actually original: B B G. Swapping (2,3) yields B G B. If we swapped (1,2) as well, that would not happen. So overlapping is only if there are three consecutive B G? Consider B G G: (1,2) B G would swap; (2,3) G G no. So again no conflict. But there could be conflict if pattern B G B? That is (1,2): B G swaps, (2,3): G B no because front is G, not B. So swapping only occurs when a boy is directly before a girl. The only potential conflict is when pattern B G G? Actually (1,2) B G swaps, but after swapping the girl moves to position 1 and boy to position 2; then (2,3) now maybe B G? But originally (2,3) was G G, so no. Pattern B B G: Only (2,3) qualifies. So overlaps may not be a problem because if a position is part of a swap (i,i+1), then it cannot also be part of another swap because that would require overlapping pairs like (i-1,i) and (i,i+1). For (i-1,i) to swap, we need boy at i-1 and girl at i. For (i,i+1) to swap, we need boy at i and girl at i+1. At the start of the second, we have some configuration. Could both conditions be true? That would mean at positions i-1:B, i:G, i+1:? For (i,i+1) to swap we need i:B, i+1:G. But if i is both G and B simultaneously, impossible. So at most one of these pairs can be swappable. So they are non-overlapping. However, there is also possibility of chain like B G B G? Let's examine: positions: 1:B,2:G,3:B,4:G. At time x, pair (1,2): B G -> swap; pair (2,3): G B -> no; pair (3,4): B G -> swap. So (1,2) and (3,4) both swap, they are disjoint. So no conflict. So actually the swaps are independent: a boy at position i and a girl at i+1 will swap; this doesn't affect any other adjacent pair because the swapped positions are used only once. However, there is a nuance: after swapping (i,i+1), the new position i is a girl, and i+1 is a boy. Could this affect the possibility of swapping with i-1 or i+2 in the same second? Since we consider only the configuration at time x to decide swaps, the swaps should be determined based on the state at the beginning of the second and then applied simultaneously. So yes, the result state at time x+1 is obtained by applying all those swaps simultaneously to the state at time x. Since the swaps are on disjoint pairs (as argued, no overlapping due to contradiction), we can safely perform them in any order, as long as we use the initial state to decide swaps. So we could simulate by scanning i from 1 to n-1, and if s[i] == 'B' and s[i+1] == 'G', we mark that a swap will happen, and then after scanning we apply all swaps. Alternatively, the skip method (swap and then i++) simulates left-to-right but ensures that after swapping, we don't consider the same boy again because the next i increment skips the swapped boy? But let's test with pattern B G B G: using left-to-right with skip (i++ after swap) vs simultaneous.
We'll test both methods:
Simultaneous method: start: B G B G (index 1,2,3,4). At time x, pairs to swap: (1,2) and (3,4). Apply: positions become G B G B. That's after 1 sec.
Skip method: start: i=1: B G => swap => G B B G; then i++ (to i=2). Now i=2: s[2]=B, s[3]=B? Actually after swap, s[2]=B (the boy from pos1), s[3]=B? Wait original s[3]=B, unchanged. So s[2]=B, s[3]=B -> no swap. i=3: s[3]=B, s[4]=G => swap => G B G B? Let's step carefully:
Initial arr: index1:B,2:G,3:B,4:G.
i=1 (1-based) : check positions 1 and 2 -> B G => swap. After swap: 1:G,2:B,3:B,4:G. Then we skip? Typical algorithm: for i from 1 to n-1: if s[i]=='B' and s[i+1]=='G' then swap(s[i],s[i+1]); i++ (so increase i by 2? Actually i is loop variable, we need to manually skip. In code often: for(int i=0; i<n-1; i++) { if (s[i]=='B' && s[i+1]=='G') { swap(s[i],s[i+1]); i++; } } That means after swapping, i is incremented again by the loop's increment, leading to skipping the next index. Let's simulate 0-based: indices 0..3. Loop i=0: arr[0]=B, arr[1]=G -> swap -> now arr[0]=G, arr[1]=B; then i++ due to loop, but also we do i++ inside? Actually typical code: for (int i=0; i<n-1; i++) { if (s[i]=='B' && s[i+1]=='G') { swap(s[i], s[i+1]); i++; } } That means after the if, the loop will also increment i at the end of iteration. So if we swapped, we do i++ inside if, then at end of loop, i++ again. So effectively i increases by 2. So after swapping at i=0, i becomes 1 (from inner i++) then after loop iteration, i++ increments to 2. So next i is 2. Then check i=2: arr[2]=B, arr[3]=G -> swap -> arr[2]=G, arr[3]=B; then i++ inside if, so i becomes 3, then loop i++ makes it 4, exit. Final arr: index0:G,1:B,2:G,3:B => G B G B. That matches simultaneous.
If we used naive left-to-right without skip: for i=0 to n-2: if s[i]=='B' and s[i+1]=='G': swap(s[i],s[i+1]); and then just continue with i+1. Let's try: start: B G B G. i=0: swap => G B B G. i=1: check arr[1]=B, arr[2]=B -> no swap. i=2: arr[2]=B, arr[3]=G -> swap => G B G B. Final also G B G B? Actually after i=2 swap we get arr: G B G B. So same result? For B G B G it gave same. But is there case where naive left-to-right without skip would give a different result? Consider pattern B G G? Actually B G G: i=0 swap => G B G; i=1: now arr[1]=B, arr[2]=G => swap => G G B. That gives two swaps in one second. In simultaneous model, only (1,2) should swap because at time x, positions: 1:B,2:G,3:G. The eligible pairs: (1,2) because B before G; (2,3) is G before G, not eligible. So only (1,2) swaps, resulting G B G. Not G G B. So naive left-to-right without skip yields extra swap. The skip method would: i=0 swap => G B G; then i increments by 2 (skip to i=2). Since n=3, i=2 is out of range? Actually n-2=1? Wait n=3, loop i from 0 to 1. i=0, swap then i++ inside and loop increments -> i becomes 2, next iteration i=2 not less than 2? 2 < 2? false, so exit. So we get G B G. So skip method correct.
Thus we should simulate t seconds, each second applying the swaps simultaneously. Implementation wise, we can for each second: iterate i from 0 to n-2, if s[i]=='B' and s[i+1]=='G', then swap them and increment i (skip next). This is typical. Since n and t ≤ 50, any straightforward simulation works.
Alternatively, we can simulate using a new string: for each second, initialize result = s; then for i from 0 to n-2, if s[i]=='B' and s[i+1]=='G', then set result[i]='G', result[i+1]='B'; but careful to not override later swaps if using result as we go? Since we use original s to determine swaps, we can build new string. That's simpler: for each second, create a copy of s. For i from 0 to n-2, if s[i]=='B' and s[i+1]=='G', then swap in copy: copy[i]='G', copy[i+1]='B'. Since swaps are independent, this works because we are only modifying copy based on original s, and multiple swaps on different indices won't conflict because they are on distinct indices (if a position appears in two swaps? we argued it can't). However, is it possible for a position to be involved in two swaps? For position i, it could be in swap (i-1,i) and (i,i+1) only if conditions for both hold. Let's examine: For (i-1,i) swap: need s[i-1]='B' and s[i]='G'. For (i,i+1) swap: need s[i]='B' and s[i+1]='G'. So s[i] would need to be both 'G' and 'B', impossible. So indeed a position cannot be in two swaps. So the independent method is safe.
Thus we can simulate using for sec in range(t): new = list(s); for i in range(n-1): if s[i]=='B' and s[i+1]=='G': new[i]='G'; new[i+1]='B'; s = ''.join(new). Or using the skip method is also fine.
Let's test with examples.
Example 1:
n=5, t=1, s=BGGBG.
Initial: B G G B G
Apply: For i=0: B G => swap => new becomes G B G B G? Actually careful: i=0: s[0]='B', s[1]='G' => swap: new[0]='G', new[1]='B'. new[2] initially = s[2]='G', new[3]='B', new[4]='G'. i=1: check s[1]='G', s[2]='G' => no. i=2: s[2]='G', s[3]='B' => no. i=3: s[3]='B', s[4]='G' => swap: new[3]='G', new[4]='B'. So new becomes: index0:G,1:B,2:G,3:G? Wait we set new[3]='G' and new[4]='B'? Actually s[3]='B', s[4]='G', so new[3] becomes 'G', new[4] becomes 'B'. But we also have new[2] remains 'G', new[1]='B'. So final new: 0:G,1:B,2:G,3:G? no: new[3] set to 'G', new[4]='B'. So string: G B G G B? That gives "GBGGB"? Wait G at 0, B at1, G at2, G at3, B at4 => "GBGGB". That matches example output: "GBGGB". Good.
Example 2: t=2. We need to continue. Starting from "GBGGB"? Actually from example: after 1 sec they gave "GBGGB". After 2 sec they gave "GGBGB". Let's test: after 1 sec we got "GBGGB". Now apply another second. s = "GBGGB". Check pairs: i=0: G B -> no (since front is not B). i=1: B G -> swap: positions 1 and 2. s[1]='B', s[2]='G' -> swap => new: at i=1 'G', i=2 'B'. i=2: after swap, but we must use original s to decide swaps. So original s[2] is 'G', but careful: we are building new from original s, not from partially updated new. So: original s = "GBGGB". For i=2: s[2]='G', s[3]='G'? s[3]='G'? Actually s = G B G G B. So s[2]='G', s[3]='G' => no. i=3: s[3]='G', s[4]='B' => no (B is not G). So only swap at i=1. New becomes: index0='G' (unchanged), index1='G', index2='B', index3='G', index4='B'? Wait we swapped: set new[1] = 'G', new[2]='B'. Others unchanged: new[0]='G', new[3]='G', new[4]='B'. So new = "GGBGB". That matches example.
Example 3: n=4, t=1, s=GGGB. Apply: i=0: G G no; i=1: G G no; i=2: G B no because front G not B. So no swap, output "GGGB". matches.
Thus the described algorithm works.
So solution: read n,t, then s. For _ in range(t): use original s to create list new = list(s). for i in range(n-1): if s[i]=='B' and s[i+1]=='G': new[i]='G'; new[i+1]='B'; then set s = ''.join(new). Print final s.
Edge Cases: n=1 => no swaps possible, just output same.
Time complexity: O(n*t) ≤ 2500, fine.
Now we can produce final answer.
But note: The input constraints state 1 ≤ n, t ≤ 50. So safe.
Implementation details: read input. Use sys.stdin or input().
Thus solution is straightforward.
But maybe the problem expects the skip method? But either yields same result.
I'll produce solution with the method building a new string each second.
Also ensure to handle the scenario where we might accidentally do sequential modifications if we use in-place with skip method; but either is fine.
Let's write code in Python.
Pseudo:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0])
t = int(data[1])
s = data[2]
arr = list(s)
for _ in range(t):
# create copy
new = arr[:] # or list(s)
i = 0
while i < n-1:
if arr[i] == 'B' and arr[i+1] == 'G':
new[i] = 'G'
new[i+1] = 'B'
i += 2 # skip next pair? Actually we are iterating through original arr, so we don't need to skip because we use original for decision, but we need to avoid double-checking the same indices? But since we are using original arr, we don't need to skip because the decisions are independent; but we must not mistakenly also consider i+1 with i+2 based on original? That's fine because we are not altering arr while checking. However, if we iterate i from 0 to n-2 normally, and use original arr for conditions, it's fine: we can just loop i from 0 to n-2 and check condition. There's no risk of double-swapping because we are only building new based on original. And since swaps are non-overlapping, we might set new[i] and new[i+1] multiple times? But if swaps were overlapping, you could have conflict; but we argued they can't. So we don't need skip. Simpler: for i in range(n-1): if s[i]=='B' and s[i+1]=='G': then swap in new. That's it.
# No need for skip.
else:
i += 1? Actually using for loop we don't handle skip. But if we use for loop, we cannot skip inside, but we don't need to. However, in the for loop we would check every i; but is there any case where checking i+1 after a swap condition could lead to an incorrect decision? Since we are using original s, it's fine. For i and i+1 being part of a swap, they are swapped in new. For i+1 and i+2, we may also check condition; but if i+1 was involved in swap, original s[i+1] was 'G', but for pair (i+1,i+2) we need s[i+1]=='B' and s[i+2]=='G' to swap. Since original s[i+1] is 'G', not 'B', condition fails. So it's fine. So we can just use a simple for loop. So implement:
code
for i in range(n-1):
if arr[i]=='B' and arr[i+1]=='G':
new[i]='G'
new[i+1]='B'
arr = new
print(''.join(arr))
Wait: but in my pseudo I used while loop. But for simplicity, we can just use for loop. Since we are not modifying arr during the loop, it's fine.
But careful: if we use for i in range(n-1), and we do new[i]='G' and new[i+1]='B', that's okay. However, if there is a pattern like B G B G, then for i=0 we swap; for i=1 we check original arr[1]='G', arr[2]='B' -> no swap; for i=2 swap; So we get both swaps. That's correct. And no conflict because new gets updated multiple times? For i=0 swap: set new[0]='G', new[1]='B'. For i=2 swap: set new[2]='G', new[3]='B'. No overlap.
Thus simple for loop is correct.
Implementation:
def solve():
import sys
input_data = sys.stdin.read().split()
n = int(input_data[0])
t = int(input_data[1])
s = input_data[2]
for _ in range(t):
s_list = list(s)
# create a list to modify
for i in range(n-1):
if s[i] == 'B' and s[i+1] == 'G':
s_list[i] = 'G'
s_list[i+1] = 'B'
s = ''.join(s_list)
print(s)
This matches examples.
Alternatively, we could implement in-place with skip for each second:
for _ in range(t):
i=0
while i < n-1:
if s[i] == 'B' and s[i+1] == 'G':
s = s[:i] + "GB" + s[i+2:]
i += 2
else:
i += 1
But string slicing may be less efficient but n is small so fine.
We'll go with the first method.
Now produce final answer.
Note: There's possibility of input lines containing extra spaces? But next line is a string without spaces, so fine.
We'll implement solve() and call it if name == "main".
I'll write a clean solution.
Now write the explanation and final code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
t = int(data[1])
s = data[2]
code
for _ in range(t):
# Create a new list from the current arrangement
new = list(s)
# Check every adjacent pair (i, i+1) based on the current state
for i in range(n - 1):
if s[i] == 'B' and s[i + 1] == 'G':
new[i] = 'G'
new[i + 1] = 'B'
s = ''.join(new)
print(s)