Last active
April 13, 2021 23:36
-
-
Save wonderful-panda/42d68172c3dc606a72cdb9c74f19c6f0 to your computer and use it in GitHub Desktop.
#競プロ典型90問 6日目 https://twitter.com/e869120/status/1378840212449595393
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
K = int(input()) | |
TEXT = input() | |
N = len(TEXT) | |
ORD_A = ord('a') # 97 | |
# a ~ z の文字ごとの出現位置 | |
alphabets = [deque() for _ in range(26)] | |
min_index = 0 | |
def push(char, index): | |
""" | |
index番目の文字charをdequeのリストに追加する | |
""" | |
global min_index | |
alphabet_index = ord(char) - ORD_A | |
alphabets[alphabet_index].append(index) | |
# pushした文字より後の文字の出現位置をクリアする | |
# 例えば d を push した場合、それまでに出ていたe~zの出現位置は破棄される | |
for i in range(alphabet_index + 1, 26): | |
alphabets[i].clear() | |
min_index = min(min_index, alphabet_index) | |
def pop(): | |
""" | |
dequeのリストから一番小さい文字を取り出す | |
""" | |
global min_index | |
for i in range(min_index, 26): | |
if len(alphabets[i]) > 0: | |
alphabets[i].popleft() | |
return chr(i + ORD_A) | |
else: | |
min_index += 1 | |
raise Exception("Something wrong") | |
answer = [] | |
for i, c in enumerate(TEXT): | |
push(c, i) | |
if N - K <= i: | |
answer.append(pop()) | |
print("".join(answer)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment