結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー roarisroaris
提出日時 2022-10-15 02:47:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 705 ms / 3,000 ms
コード長 1,404 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 87,116 KB
実行使用メモリ 257,380 KB
最終ジャッジ日時 2023-09-09 02:02:20
合計ジャッジ時間 37,873 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,672 KB
testcase_01 AC 94 ms
71,852 KB
testcase_02 AC 460 ms
94,484 KB
testcase_03 AC 450 ms
98,640 KB
testcase_04 AC 416 ms
93,920 KB
testcase_05 AC 345 ms
88,032 KB
testcase_06 AC 470 ms
95,696 KB
testcase_07 AC 376 ms
86,384 KB
testcase_08 AC 233 ms
79,976 KB
testcase_09 AC 444 ms
94,328 KB
testcase_10 AC 269 ms
83,476 KB
testcase_11 AC 289 ms
87,564 KB
testcase_12 AC 397 ms
93,248 KB
testcase_13 AC 522 ms
99,476 KB
testcase_14 AC 371 ms
90,496 KB
testcase_15 AC 435 ms
94,184 KB
testcase_16 AC 592 ms
104,636 KB
testcase_17 AC 290 ms
83,296 KB
testcase_18 AC 411 ms
91,076 KB
testcase_19 AC 323 ms
88,780 KB
testcase_20 AC 554 ms
101,428 KB
testcase_21 AC 299 ms
81,048 KB
testcase_22 AC 705 ms
111,020 KB
testcase_23 AC 700 ms
110,680 KB
testcase_24 AC 649 ms
109,528 KB
testcase_25 AC 672 ms
110,464 KB
testcase_26 AC 672 ms
111,272 KB
testcase_27 AC 644 ms
109,312 KB
testcase_28 AC 648 ms
110,176 KB
testcase_29 AC 682 ms
110,776 KB
testcase_30 AC 621 ms
107,228 KB
testcase_31 AC 687 ms
110,728 KB
testcase_32 AC 624 ms
109,772 KB
testcase_33 AC 663 ms
110,052 KB
testcase_34 AC 632 ms
108,236 KB
testcase_35 AC 667 ms
111,276 KB
testcase_36 AC 642 ms
108,472 KB
testcase_37 AC 596 ms
108,200 KB
testcase_38 AC 638 ms
109,680 KB
testcase_39 AC 681 ms
109,060 KB
testcase_40 AC 604 ms
107,212 KB
testcase_41 AC 639 ms
108,732 KB
testcase_42 AC 481 ms
95,444 KB
testcase_43 AC 483 ms
95,456 KB
testcase_44 AC 488 ms
95,440 KB
testcase_45 AC 488 ms
95,304 KB
testcase_46 AC 499 ms
95,368 KB
testcase_47 AC 429 ms
88,492 KB
testcase_48 AC 418 ms
88,520 KB
testcase_49 AC 424 ms
88,456 KB
testcase_50 AC 411 ms
88,448 KB
testcase_51 AC 410 ms
88,688 KB
testcase_52 AC 501 ms
256,148 KB
testcase_53 AC 497 ms
256,172 KB
testcase_54 AC 417 ms
256,192 KB
testcase_55 AC 372 ms
79,924 KB
testcase_56 AC 377 ms
79,932 KB
testcase_57 AC 369 ms
80,204 KB
testcase_58 AC 471 ms
257,380 KB
testcase_59 AC 661 ms
113,792 KB
testcase_60 AC 225 ms
78,840 KB
testcase_61 AC 372 ms
79,936 KB
testcase_62 AC 153 ms
78,308 KB
testcase_63 AC 291 ms
79,836 KB
testcase_64 AC 275 ms
78,524 KB
testcase_65 AC 330 ms
79,124 KB
testcase_66 AC 375 ms
79,604 KB
testcase_67 AC 387 ms
79,716 KB
testcase_68 AC 370 ms
79,184 KB
testcase_69 AC 380 ms
81,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class RollingHash:
    def __init__(self, s, base=17, mod=10**9+7):
        self.base = base
        self.mod = mod
        self.acc = [0]
        self.power = [1]
        
        for i in range(len(s)):
            self.acc.append((self.acc[-1]*self.base%self.mod+ord(s[i])-ord('a')+1)%self.mod)
            self.power.append(self.power[-1]*self.base%self.mod)
    
    def get(self, l, r):
        return (self.acc[r]-self.acc[l]*self.power[r-l])%self.mod

N = int(input())
ws = set()
base1, mod1 = 17, 10**9+7
base2, mod2 = 31, 10**9+9

for _ in range(N):
    S = input()[:-1]
    n = len(S)
    rh1 = RollingHash(S, base1, mod1)
    rh2 = RollingHash(S, base2, mod2)
    h1 = rh1.get(0, n)
    h2 = rh2.get(0, n)
    
    if (h1, h2) in ws:
        print('Yes')
    else:
        for i in range(n-1):
            nh1 = h1
            nh1 += (ord(S[i+1])-ord(S[i]))*rh1.power[n-1-i]%mod1
            nh1 += (ord(S[i])-ord(S[i+1]))*rh1.power[n-2-i]%mod1
            nh1 %= rh1.mod
            
            nh2 = h2
            nh2 += (ord(S[i+1])-ord(S[i]))*rh2.power[n-1-i]%mod2
            nh2 += (ord(S[i])-ord(S[i+1]))*rh2.power[n-2-i]%mod2
            nh2 %= rh2.mod
            
            if (nh1, nh2) in ws:
                print('Yes')
                break
        else:
            print('No')
        
    ws.add((h1, h2))
0