結果
| 問題 |
No.701 ひとりしりとり
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-12-02 19:22:13 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 337 ms / 2,000 ms |
| コード長 | 1,620 bytes |
| コンパイル時間 | 197 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 21,096 KB |
| 最終ジャッジ日時 | 2024-10-09 20:40:02 |
| 合計ジャッジ時間 | 1,831 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 12 |
ソースコード
from random import choices
from string import ascii_lowercase
class ShiritoriWordManage:
def __init__(self, length: int, word_length: int = None) -> None:
self.word_used = set()
self.current_letter = "a"
self.current_iter = 0
self.end_iter = length
if word_length is None:
self.word_length = 6
else:
self.word_length = word_length
def create_word(self, start_letter=None, end_letter=None) -> str:
next_word = ""
if start_letter is None:
start_letter = self.current_letter
while not next_word or next_word in self.word_used:
if end_letter is None:
next_word = start_letter + \
"".join(choices(ascii_lowercase, k=self.word_length - 1))
else:
next_word = start_letter + \
"".join(choices(ascii_lowercase,
k=self.word_length - 2)) + end_letter
return next_word
def exec_shiritori(self):
for self.current_iter in range(self.end_iter):
if self.current_iter == self.end_iter - 1:
print(self.create_word(self.current_letter, "n"))
return
next_word = "n"
while next_word[-1] == "n":
next_word = self.create_word()
self.current_letter = next_word[-1]
self.word_used.add(next_word)
print(next_word)
def main():
n = int(input())
shiritori = ShiritoriWordManage(n)
shiritori.exec_shiritori()
if __name__ == "__main__":
main()