結果
| 問題 | No.1909 Detect from Substrings |
| コンテスト | |
| ユーザー |
ygd.
|
| 提出日時 | 2022-04-22 22:32:12 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,305 bytes |
| 記録 | |
| コンパイル時間 | 357 ms |
| コンパイル使用メモリ | 82,496 KB |
| 実行使用メモリ | 252,916 KB |
| 最終ジャッジ日時 | 2024-06-24 03:51:30 |
| 合計ジャッジ時間 | 57,053 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 WA * 8 TLE * 20 |
ソースコード
import sys
#input = sys.stdin.readline
#input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]
#dx8 = [1,1,0,-1,-1,-1,0,1]
#dy8 = [0,1,1,1,0,-1,-1,-1]
class RollingHash():
def __init__(self, s, base, mod):
self.mod = mod
self.pw = pw = [1]*(len(s)+1)
l = len(s)
self.h = h = [0]*(l+1)
v = 0
for i in range(l):
h[i+1] = v = (v * base + ord(s[i])) % mod #文字ではなく数字のリストの場合はord不要
v = 1
for i in range(l):
pw[i+1] = v = v * base % mod
def get(self, l, r): #S[l:r)indexでいうl文字目からr-1文字目
return (self.h[r] - self.h[l] * self.pw[r-l]) % self.mod
base1 = 1007; base2 = 2009
mod1 = 1000000007; mod2 = 1000000009
def main():
N,M = map(int,input().split())
S = []
RHS = []
for i in range(N):
s = str(input())
S.append(s)
temp = RollingHash(s,base1,mod1)
RHS.append(temp.get(0,M))
ans = set([])
mae = S[0]
for i in range(26):
na = chr(i+97) + mae
#print(na)
frh = RollingHash(na[:-1],base1,mod1)
first = frh.get(0,M)
srh = RollingHash(na[1:],base1,mod1)
second = srh.get(0,M)
for j in range(1,N):
if RHS[j] == first or RHS[j] == second:
continue
else:
break
else:
ans.add(na)
ato = S[0]
for i in range(26):
na = ato + chr(i+97)
#print(na)
frh = RollingHash(na[:-1],base1,mod1)
first = frh.get(0,M)
srh = RollingHash(na[1:],base1,mod1)
second = srh.get(0,M)
for j in range(1,N):
#print(RHS[j],first,second)
if RHS[j] == first or RHS[j] == second:
continue
else:
break
else:
ans.add(na)
print(len(ans))
if __name__ == '__main__':
main()
ygd.