結果

問題 No.3081 Make Palindromic Multiple
ユーザー gew1fw
提出日時 2025-06-12 13:05:31
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,380 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 82,592 KB
実行使用メモリ 68,340 KB
最終ジャッジ日時 2025-06-12 13:10:42
合計ジャッジ時間 10,695 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 54
権限があれば一括ダウンロードができます

ソースコード

diff #

def generate_nine():
    s = []
    for i in range(99, 0, -1):
        s.append(f"{i} bottle{'s' if i != 1 else ''} of beer on the wall, {i} bottle{'s' if i != 1 else ''} of beer.")
        s.append(f"Take one down and pass it around, {i-1 if i-1 != 0 else 'no more'} bottle{'s' if (i-1) != 1 else ''} of beer on the wall.\n")
    s.append("No more bottles of beer on the wall, no more bottles of beer.")
    s.append("Go to the store and buy some more, 99 bottles of beer on the wall.")
    return '\n'.join(s)

nine_output = generate_nine()
L_song = len(nine_output)
L_hello = len("Hello, World!")

def check_hq9_combination(s):
    current = 0
    code = []
    n = len(s)
    while current < n:
        if s.startswith("Hello, World!", current):
            code.append('H')
            current += L_hello
        elif s.startswith(nine_output, current):
            code.append('9')
            current += L_song
        else:
            return None
    return ''.join(code)

def main():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    S = input[1]
    
    # Case 1: S is the 99 song
    if S == nine_output:
        print("9")
        return
    
    # Case 2: S is "Hello, World!"
    if S == "Hello, World!":
        print("H")
        return
    
    # Case 3: S is "Q"
    if S == "Q":
        print("Q")
        return
    
    # Case 4: S is all Q's and length is a square
    if all(c == 'Q' for c in S):
        m = int(len(S) ** 0.5)
        if m * m == len(S):
            print('Q' * m)
            return
    
    # Case 5: Check if S can be split into H and 9 parts
    code_h9 = check_hq9_combination(S)
    if code_h9 is not None:
        print(code_h9)
        return
    
    # Case 6: Check if S itself is a valid code (consists of H, Q, 9, +)
    valid_chars = {'H', 'Q', '9', '+'}
    if all(c in valid_chars for c in S):
        # Generate the output of code S
        output = []
        for c in S:
            if c == 'H':
                output.append("Hello, World!")
            elif c == 'Q':
                output.append(S)
            elif c == '9':
                output.append(nine_output)
            # '+' does nothing
        generated = ''.join(output)
        if generated == S:
            print(S)
            return
    
    # If none of the above
    print(-1)

if __name__ == "__main__":
    main()
0