test/5D.py
2026-07-01 19:46:51 +03:00

30 lines
No EOL
666 B
Python

import sys
line = sys.stdin.buffer.readline().split()
n, m = int(line[0]), int(line[1])
g = sys.stdin.buffer.readline().strip()
pi = [0] * n
for i in range(1, n):
j = pi[i-1]
while j > 0 and g[i] != g[j]:
j = pi[j-1]
if g[i] == g[j]: j += 1
pi[i] = j
ans = 0
idx = 0
j = 0
while True:
chunk = sys.stdin.buffer.read(131072)
if not chunk:
break
chunk = chunk.replace(b'\n', b'').replace(b'\r', b'')
for c in chunk:
while j > 0 and c != g[j]:
j = pi[j-1]
if c == g[j]:
j += 1
if j == n:
ans += (idx - n + 1)
j = pi[n-1]
idx += 1
print(ans)