結果
| 問題 |
No.2102 [Cherry Alpha *] Conditional Reflection
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-14 23:35:04 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,619 bytes |
| コンパイル時間 | 254 ms |
| コンパイル使用メモリ | 82,480 KB |
| 実行使用メモリ | 207,708 KB |
| 最終ジャッジ日時 | 2024-06-26 17:33:00 |
| 合計ジャッジ時間 | 72,967 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 31 WA * 39 |
ソースコード
import sys
input = sys.stdin.readline
from collections import *
import random
def era(n):
is_prime = [True]*(n+1)
is_prime[0] = False
is_prime[1] = False
for i in range(2, int(n**0.5)+1):
if not is_prime[i]:
continue
for j in range(2*i, n+1, i):
is_prime[j] = False
return [p for p in range(n+1) if is_prime[p]]
class RollingHash:
def __init__(self, s):
self.base = base
self.mod = pow(2, 61)-1
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')))%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()
primes = era(10**7)
base = random.choice(primes)
for _ in range(N):
S = input()[:-1]
n = len(S)
rh = RollingHash(S)
h = rh.get(0, n)
if h in ws:
print('Yes')
else:
for i in range(n-1):
nh = h
nh -= (ord(S[i])-ord('a'))*rh.power[n-1-i]%rh.mod
nh %= rh.mod
nh -= (ord(S[i+1])-ord('a'))*rh.power[n-2-i]%rh.mod
nh %= rh.mod
nh += (ord(S[i+1])-ord('a'))*rh.power[n-1-i]%rh.mod
nh %= rh.mod
nh += (ord(S[i])-ord('a'))*rh.power[n-2-i]%rh.mod
nh %= rh.mod
if nh in ws:
print('Yes')
break
else:
print('No')
ws.add(h)