結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー flygon
提出日時 2022-10-15 14:34:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 513 ms / 3,000 ms
コード長 1,507 bytes
コンパイル時間 234 ms
コンパイル使用メモリ 82,132 KB
実行使用メモリ 129,888 KB
最終ジャッジ日時 2024-06-26 19:49:42
合計ジャッジ時間 27,371 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

class Hasher():
    def __init__(self, length, base, mod):
        self.length=length
        self.base=base
        self.mod=mod

        self.power=pw=[1]*length
        for i in range(1,length):
            pw[i]=(base*pw[i-1])%mod

    def __repr__(self):
        return "length: {}\nbase: {}\nmod: {}".format(self.length, self.base, self.mod)

    def encode(self, S):
        code=0; N=len(S)
        for i in range(N):
            #code+=ord(S[i])*self.power[N-1-i]%self.mod
            code+=S[i]*self.power[N-1-i]%self.mod

        return code%self.mod

    def decode(self, S):
        pass
def hashing(a,b):
    return a*q+b
from random import randint
n = int(input())
s = [""]*n
L = 0
for i in range(n):
  s[i] = [ord(a) - ord("a")+ 1 for a in input()]
  L = max(L, len(s[i]))
  
p = 10**9+7
beta = randint(2, p-1)
q = 10**9+9
gamma = randint(2, q-1)
Bp = [1]*L
Bq= [1]*L
Hp = Hasher(L, beta, p)
Hq = Hasher(L, gamma, q)

for j in range(1, L):
  Bp[j] = Bp[j-1]*beta%p
  Bq[j] = Bq[j-1]*gamma%q

for j in range(L):
  Bp[j] *= beta-1
  Bq[j] *= gamma-1
  Bp[j] %= p
  Bq[j] %= q

E = set()
ans = [0]*n
for i in range(n):
  hp = Hp.encode(s[i])
  hq = Hq.encode(s[i])
  h = hashing(hp, hq)

  if h in E:
    ans[i] = 1
    continue

  lensi = len(s[i])
  for j in range(lensi-1):
    kp = (hp + Bp[lensi-j-2]*(s[i][j+1] - s[i][j]))%p
    kq = (hq + Bq[lensi-j-2]*(s[i][j+1] - s[i][j]))%q
    if hashing(kp, kq) in E:
      ans[i] = 1
  E.add(h)

for i in ans:
  print('Yes') if i else print('No')
0