結果

問題 No.701 ひとりしりとり
ユーザー ThetaTheta
提出日時 2022-12-02 19:22:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 301 ms / 2,000 ms
コード長 1,620 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 21,228 KB
最終ジャッジ日時 2024-04-18 02:57:45
合計ジャッジ時間 1,867 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
11,136 KB
testcase_01 AC 34 ms
11,264 KB
testcase_02 AC 34 ms
11,136 KB
testcase_03 AC 30 ms
11,264 KB
testcase_04 AC 33 ms
11,264 KB
testcase_05 AC 33 ms
11,264 KB
testcase_06 AC 32 ms
11,136 KB
testcase_07 AC 36 ms
11,136 KB
testcase_08 AC 33 ms
11,136 KB
testcase_09 AC 34 ms
11,392 KB
testcase_10 AC 57 ms
12,288 KB
testcase_11 AC 301 ms
21,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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()
0