結果
| 問題 |
No.205 マージして辞書順最小
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-09-15 18:30:48 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 36 ms / 5,000 ms |
| コード長 | 1,326 bytes |
| コンパイル時間 | 103 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 10,880 KB |
| 最終ジャッジ日時 | 2024-11-17 06:07:39 |
| 合計ジャッジ時間 | 1,718 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 15 |
ソースコード
import string
class StringMerge:
def __init__(self):
self.atoi = lambda a: ord(a)-ord('a') # alphabet to index
self.endchar = chr(ord('z')+1)
self.strmat = [[] for _ in range(len(string.ascii_lowercase))]
def add(self, word):
self._add(word+self.endchar)
def _add(self, word):
index = self.atoi(word[0])
self.strmat[index].append(word)
def merge(self):
cur_top = 0 # init index = 'a'(=0)
buff = []
while cur_top < len(self.strmat):
if len(self.strmat[cur_top]) > 0:
self.strmat[cur_top].sort() # sorted by char e.g. fae,fab->x fab,fae->o
tar = self.strmat[cur_top].pop(0)
buff.append(tar[0])
if len(tar) >= 3: # !! include endchar
# rest sub string, add
self._add(tar[1:])
# update top index
or_top = self.atoi(tar[1])
if cur_top > or_top:
cur_top = or_top
else:
# miss index, walk next
cur_top += 1
return ''.join(buff)
if __name__ == '__main__':
n = int(input())
sm = StringMerge()
for _ in range(n):
line = input()
sm.add(line)
print(sm.merge())