from collections import deque def manacher(s): i, j = 0, 0 n = len(s) ret = [0]*n while i < n: while i-j>=0 and i+j=0 and k+ret[i-k]=M or S[i]!=T[i]: break dp[i] = 1 bfs.append(i) for i in range(N): if i>= M or S[N-1-i]!=T[i]: break if dp[i] == INF: bfs.append(i) dp[i] = 1 R = manacher_even(T) G = [[]for i in range(M)] for i in range(M-1): G[i+1].append((i,0)) if R[i] > 0: G[i].append((i+R[i],1)) while len(bfs) > 0: v = bfs.popleft() for (nx, c) in G[v]: if dp[nx] > dp[v] + c: dp[nx] = dp[v] + c if c == 0: bfs.appendleft(nx) else: bfs.append(nx) print(-1 if dp[M-1] == INF else dp[M-1])