Skip to content

Instantly share code, notes, and snippets.

@wonderful-panda
Last active April 13, 2021 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wonderful-panda/42d68172c3dc606a72cdb9c74f19c6f0 to your computer and use it in GitHub Desktop.
Save wonderful-panda/42d68172c3dc606a72cdb9c74f19c6f0 to your computer and use it in GitHub Desktop.
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