用户输出
10
6
3
1
系统信息
Exited with return code 0
编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#20134 | #2037. 弦论(another version) | Time Limit Exceeded | 14 | 26816 ms | 207132 K | Python 3 / 3.0 K | admin | 2024-12-08 18:13:11 |
import sys
sys.setrecursionlimit(200000)
class SuffixAutomaton:
MAXS = 27
N = 400005
def __init__(self):
self.tot = 1
self.ru = [0] * self.N
self.link = [0] * self.N
self.maxlen = [0] * self.N
self.endpos = [0] * self.N
self.ch = [[0] * self.MAXS for _ in range(self.N)]
self.last = 1
def insert(self, w):
if self.ch[self.last][w]:
p = self.last
x = self.ch[p][w]
if self.maxlen[p] + 1 == self.maxlen[x]:
self.endpos[x] += 1
self.last = x
return
else:
y = self.tot + 1
self.tot += 1
self.maxlen[y] = self.maxlen[p] + 1
self.ch[y] = self.ch[x][:]
while p and self.ch[p][w] == x:
self.ch[p][w] = y
p = self.link[p]
self.link[y] = self.link[x]
self.link[x] = y
self.endpos[y] += 1
self.last = y
return
z = self.tot + 1
self.tot += 1
p = self.last
self.maxlen[z] = self.maxlen[self.last] + 1
while p and not self.ch[p][w]:
self.ch[p][w] = z
p = self.link[p]
if not p:
self.link[z] = 1
else:
x = self.ch[p][w]
if self.maxlen[p] + 1 == self.maxlen[x]:
self.link[z] = x
else:
y = self.tot + 1
self.tot += 1
self.maxlen[y] = self.maxlen[p] + 1
self.ch[y] = self.ch[x][:]
while p and self.ch[p][w] == x:
self.ch[p][w] = y
p = self.link[p]
self.link[y] = self.link[x]
self.link[z] = self.link[x] = y
self.endpos[z] += 1
self.last = z
def dfs(self, u):
for v in self.p[u]:
self.dfs(v)
self.endpos[u] += self.endpos[v]
def get_endpos(self):
self.p = [[] for _ in range(self.N)]
for i in range(2, self.tot + 1):
self.p[self.link[i]].append(i)
self.dfs(1)
self.p = [[] for _ in range(self.N)]
def init():
pass
def solve():
sam = SuffixAutomaton()
n, m = map(int, input().split())
for _ in range(n):
s = input().strip()
sam.last = 1
for char in s:
sam.insert(ord(char) - ord('a'))
sam.get_endpos()
q = int(input())
for _ in range(q):
t = input().strip()
tp = 1
for i, char in enumerate(t):
if not sam.ch[tp][ord(char) - ord('a')]:
print(0)
break
tp = sam.ch[tp][ord(char) - ord('a')]
if i + 1 == len(t):
print(sam.endpos[tp])
if __name__ == "__main__":
import sys
init()
solve()
用户输出
10
6
3
1
系统信息
Exited with return code 0
100 200000
xyhglfbxleieeancsaipttydqghtjmhblotpsuvytxynwaatqiezrilmgxdfqvizbndvsksxzvobqxthgkxvbvgo
<440065 bytes omitted>
100 200000
hlbcgcmaxdfioijoubnojpmrfohulumwirfarwovepnrwykzrzzfvorynjoxumvhsxpvabbpkryuruclumhmteew
<300119 bytes omitted>
100 200000
usnsprodkeljgibmantimnkpzguxfcuobzwxerknjjbccxqlcvunmmymjzlksccstncetmtqecmpxiuxhplazyat
<300119 bytes omitted>
100 200000
xobjjfjmjhmgsxasjmbeigsuatjqhaenqcnwjzopjpodnkowiqzlycqbxjqhqrrcqpafuvnyidgbcheftpxttqgo
<298506 bytes omitted>
100 200000
ssiwurakptykaljovijyaoopdspzxayggnhfsubqsrpwodufsvwzmpabkgxryesanszczewjystsyftjkspenbre
<292658 bytes omitted>
1 200000
higlcljtjjezelczszusltndbahanudplnftpxjkyxcapghfkgsvyrclugymaqrrakvnxscwhvlmqqqdmuojlysapv
<349915 bytes omitted>
1 200000
aidiygcnhnlugueymginpzbrpkygsddmdamadrrqskbclnetprrwazizyiqpngvzlavrakeiszfflbuieywlnxfcdy
<349935 bytes omitted>
100 200000
otyctwfxuvhyqqsghwqihvwquekwvsrkbgwayazmcepjgccnugcaexvoavauiviwenyrdrsmrekmsidscuevfogt
<202317 bytes omitted>
100 200000
hyzlqvwoyvjgezzmzrzwsbiibebexgaxjlccviwtfcpgzwmbuddlvhruxmndjdgyqxiqgtfopezoxfyndwhbclhx
<600120 bytes omitted>
100 200000
hhssofajgkxkqrlkxqrvkkotbaiypfeglpwfltwpryovffwnfdaetqaelpvdwyvuabzjxiudfkvvehoqwhpqbzhn
<800120 bytes omitted>
200000 200000
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
<1199923 bytes omitted>