結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー LyricalMaestro
提出日時 2025-05-06 02:39:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 458 ms / 3,000 ms
コード長 1,792 bytes
コンパイル時間 1,159 ms
コンパイル使用メモリ 82,668 KB
実行使用メモリ 227,408 KB
最終ジャッジ日時 2025-05-06 02:40:26
合計ジャッジ時間 29,277 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/3102

B = 30
H = 9007199254740997

def judge(length_map, len_s, r_hash):
    if len_s in length_map:
        if r_hash in length_map[len_s]:
            return True
    return False


def main():
    N = int(input())
    S = []
    max_len = 0
    for _ in range(N):
        S.append(input())
        max_len = max(max_len, len(S[-1]))

    pow_b = [0] * (max_len + 1)
    b = 1
    for i in range(max_len + 1):
        pow_b[i] = b
        b *= B
        b %= H

    length_map = {}
    for s in S:
        len_s = len(s)

        # ローリングハッシュ計算
        r_hash = 0
        for i in range(len_s):
            r_hash *= B
            r_hash %= H
            x = ord(s[i]) - ord("a") + 1
            r_hash += x
            r_hash %= H

        if judge(length_map, len_s, r_hash):
            print("Yes")
        else:
            print("No")
        
        # そのままのものを入れる
        if len_s not in length_map:
            length_map[len_s] = set()
        length_map[len_s].add(r_hash)

        # 高々1回入れ替えたものを入れる
        for i in range(len_s - 1):
            b1 = ord(s[i]) - ord("a") +1
            b2 = ord(s[i + 1]) - ord("a") +1

            b01 = (b1 * pow_b[len_s - 1 - i]) % H
            b02 = (b2 * pow_b[len_s - 1 - i - 1]) % H
            a01 = (b2 * pow_b[len_s - 1 - i]) % H
            a02 = (b1 * pow_b[len_s - 1 - i - 1]) % H

            r_hash0 = r_hash
            r_hash0 -= b01
            r_hash0 %= H
            r_hash0 -= b02
            r_hash0 %= H
            r_hash0 += a01
            r_hash0 %= H
            r_hash0 += a02
            r_hash0 %= H
            length_map[len_s].add(r_hash0)





                


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