牛客華為機試HJ64
阿新 • • 發佈:2022-04-21
1. 問題描述
2. Solution
滑動視窗
import sys if sys.platform != "linux": file_in = open("input/HJ64.txt") sys.stdin = file_in def solve(n, s): cur = 1 start = 1 for c in s: if c == 'U': if cur == 1: # 到達左邊界 cur = n # 游標移到最後一首歌 start = n - 3 else: cur -= 1 if cur < start: start = cur elif c == 'D': if cur == n: # 到達右邊界 cur = 1 start = 1 else: cur += 1 if cur > start + 3: # 大於顯示的最後一首歌的座標 start += 1 if n < 4: [print(x, end=" ") for x in range(1, n + 1)] else: [print(x, end=" ") for x in range(start, start + 4)] print() print(cur) while True: try: n = int(input().strip()) s = input().strip() solve(n, s) except: break