結果

問題 No.1695 Mirror Mirror
ユーザー chineristACchineristAC
提出日時 2021-10-01 22:54:03
言語 PyPy3
(7.3.15)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,714 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 87,240 KB
実行使用メモリ 177,664 KB
最終ジャッジ日時 2023-08-28 16:07:42
合計ジャッジ時間 17,805 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,952 KB
testcase_01 AC 92 ms
72,352 KB
testcase_02 AC 91 ms
72,268 KB
testcase_03 AC 92 ms
72,300 KB
testcase_04 AC 102 ms
77,416 KB
testcase_05 AC 91 ms
72,168 KB
testcase_06 AC 104 ms
77,028 KB
testcase_07 AC 93 ms
72,168 KB
testcase_08 AC 107 ms
77,296 KB
testcase_09 AC 111 ms
77,424 KB
testcase_10 AC 99 ms
76,312 KB
testcase_11 AC 91 ms
72,376 KB
testcase_12 AC 90 ms
72,120 KB
testcase_13 AC 99 ms
77,028 KB
testcase_14 AC 100 ms
77,128 KB
testcase_15 AC 90 ms
72,380 KB
testcase_16 AC 90 ms
72,508 KB
testcase_17 AC 101 ms
76,980 KB
testcase_18 AC 91 ms
72,456 KB
testcase_19 AC 100 ms
76,944 KB
testcase_20 AC 90 ms
72,136 KB
testcase_21 AC 600 ms
161,188 KB
testcase_22 AC 525 ms
177,664 KB
testcase_23 AC 454 ms
156,088 KB
testcase_24 AC 506 ms
155,984 KB
testcase_25 AC 251 ms
155,608 KB
testcase_26 AC 90 ms
72,272 KB
testcase_27 AC 88 ms
72,460 KB
testcase_28 AC 91 ms
73,436 KB
testcase_29 AC 89 ms
72,588 KB
testcase_30 AC 266 ms
158,052 KB
testcase_31 AC 344 ms
160,276 KB
testcase_32 AC 219 ms
100,168 KB
testcase_33 AC 375 ms
161,760 KB
testcase_34 AC 276 ms
114,024 KB
testcase_35 AC 388 ms
158,436 KB
testcase_36 AC 394 ms
171,696 KB
testcase_37 AC 288 ms
119,356 KB
testcase_38 AC 425 ms
158,656 KB
testcase_39 AC 255 ms
107,380 KB
testcase_40 AC 479 ms
156,748 KB
testcase_41 AC 546 ms
156,028 KB
testcase_42 AC 311 ms
172,264 KB
testcase_43 AC 260 ms
155,152 KB
testcase_44 AC 272 ms
162,236 KB
testcase_45 AC 259 ms
147,460 KB
testcase_46 AC 404 ms
157,480 KB
testcase_47 AC 351 ms
162,172 KB
testcase_48 AC 393 ms
162,504 KB
testcase_49 AC 519 ms
155,732 KB
testcase_50 AC 100 ms
78,220 KB
testcase_51 AC 99 ms
78,692 KB
testcase_52 AC 98 ms
78,312 KB
testcase_53 AC 91 ms
73,324 KB
testcase_54 AC 90 ms
72,932 KB
testcase_55 AC 252 ms
147,152 KB
testcase_56 AC 287 ms
159,196 KB
testcase_57 AC 300 ms
156,188 KB
testcase_58 AC 289 ms
157,376 KB
testcase_59 AC 287 ms
159,752 KB
testcase_60 AC 270 ms
153,816 KB
testcase_61 AC 206 ms
123,792 KB
testcase_62 AC 265 ms
165,924 KB
testcase_63 AC 353 ms
162,968 KB
testcase_64 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    def __init__(self, init_val, segfunc, ide_ele):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        self.size = n
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        k += self.num
        self.tree[k] = x
        while k > 1:
            k >>= 1
            self.tree[k] = self.segfunc(self.tree[2*k], self.tree[2*k+1])

    def query(self, l, r):
        if r==self.size:
            r = self.num

        res = self.ide_ele

        l += self.num
        r += self.num
        right = []
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                right.append(self.tree[r-1])
            l >>= 1
            r >>= 1

        for e in right[::-1]:
            res = self.segfunc(res,e)
        return res

from math import radians
import sys,random

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

"""
from:https://tjkendev.github.io/procon-library/python/string/manacher.html
"""
# 偶数長含めた回文の長さを求める
# R[2*i] = L: S[i]を中心とする奇数長の最大回文
# R[2*i+1] = L: S[i:i+2]を中心とする偶数長の最大回文
# ダミー文字を挟むが、各 R[i] は実際の回文の文字列長と一致する
def manacher(S):
    C = []
    for a in S:
        C.append(a)
        C.append(0)
    C.pop()

    L = len(C)

    R = [0]*L

    i = j = 0
    while i < L:
        while j <= i < L-j and C[i-j] == C[i+j]:
            j += 1
        R[i] = j
        k = 1
        while j-R[i-k] > k <= i < L-k:
            R[i+k] = R[i-k]
            k += 1
        i += k; j -= k
    return R


N,M = mi()
S = input()
T = input()

if M&1:
    exit(print(-1))
if any(T[i]!=T[-i-1] for i in range(M)):
    exit(print(-1))

R = manacher(T)

m = M//2
INF = 10**9
dp = [INF for i in range(M)]
dp[-1] = 0
dp = SegmentTree([INF]*M,min,INF)
dp.update(M-1,0)
for i in range(M-2,-1,-1):
    l = i
    r = i + (R[2*i+1]//2)
    tmp = dp.query(l+1,r+1) + 1
    if tmp < INF:
        dp.update(i,tmp)

dp = dp.tree[dp.num:]

res = INF
for i in range(N):
    if S[i]==T[i]:
        res = min(res,dp[i])
    else:
        break
for i in range(N-1,-1,-1):
    if S[i]==T[M-1-(N-1-i)]:
        res = min(res,dp[N-1-i])
    else:
        break

if res==INF:
    print(-1)
else:
    print(res)
0