If you actually try it by hand, you can see that the Kth to Nth are arranged in order, then the 1st to K-1th are arranged in order, and the reverse is arranged. The 1st to K-1th arrangement Of course, it depends on whether the number of operations is odd or even, so it can be solved with the following code.
N, K = map(int, input().split())
S = input()
if (N - K) % 2 == 0:
print(S[K-1:] + S[:K-1][::-1])
else:
print(S[K-1:] + S[:K-1])
I thought about rewriting the array while moving the pointer and outputting the result based on the pointer information, but it was complicated and my head got confused, so I decided to execute the operation straight with deque. There was nothing difficult with this, and I could solve it with the following code.
from collections import deque
N, M = map(int, input().split())
a = list(map(int, input().split()))
S = input()
q = deque(a)
for c in S:
if c == 'L':
t = q.popleft()
q[0] += t
q.append(0)
elif c == 'R':
t = q.pop()
q[-1] += t
q.appendleft(0)
print(*q)
Postscript: I also tried to solve it by moving the pointer. It was complicated.
N, M = map(int, input().split())
a = list(map(int, input().split()))
S = input()
p = 0
for c in S:
if c == 'L':
p = max(p - 1, -N + 1)
if p < 0:
a[-p] += a[-p - 1]
a[-p - 1] = 0
elif c == 'R':
p = min(p + 1, N - 1)
if p > 0:
a[N - 1 - p] += a[N - p]
a[N - p] = 0
if p > 0:
print(*([0] * p + a)[:N])
elif p < 0:
print(*(a + [0] * -p)[-p:N - p])
else:
print(*a)
Recommended Posts