結果

問題 No.1192 半部分列
ユーザー shotoyooshotoyoo
提出日時 2021-05-25 00:50:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 2,270 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 82,044 KB
実行使用メモリ 125,308 KB
最終ジャッジ日時 2024-04-21 10:13:35
合計ジャッジ時間 4,268 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,608 KB
testcase_01 AC 40 ms
52,096 KB
testcase_02 AC 44 ms
52,096 KB
testcase_03 AC 43 ms
52,612 KB
testcase_04 AC 40 ms
52,608 KB
testcase_05 AC 39 ms
52,480 KB
testcase_06 AC 267 ms
125,308 KB
testcase_07 AC 233 ms
119,604 KB
testcase_08 AC 232 ms
119,396 KB
testcase_09 AC 230 ms
117,888 KB
testcase_10 AC 40 ms
52,096 KB
testcase_11 AC 41 ms
52,608 KB
testcase_12 AC 229 ms
119,424 KB
testcase_13 AC 141 ms
95,796 KB
testcase_14 AC 179 ms
103,744 KB
testcase_15 AC 172 ms
101,888 KB
testcase_16 AC 147 ms
95,048 KB
testcase_17 AC 180 ms
103,056 KB
testcase_18 AC 58 ms
62,884 KB
testcase_19 AC 97 ms
80,040 KB
testcase_20 AC 49 ms
60,544 KB
testcase_21 AC 43 ms
58,752 KB
testcase_22 AC 44 ms
58,112 KB
testcase_23 AC 231 ms
119,168 KB
testcase_24 AC 43 ms
58,240 KB
testcase_25 AC 43 ms
58,880 KB
testcase_26 AC 40 ms
52,864 KB
testcase_27 AC 52 ms
67,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))

l = [chr(v) for v in range(ord("a"), ord("z")+1)]
def snx(s, target=None, reverse=False, loop=False):
    """文字列sの各要素について、cがi【以上】のインデックスで最初に現れる位置n[i][c]
    ない場合はNone
    loop=Trueのとき周回した先のインデックスまで求める
    """
    if isinstance(s[0], str):
        oa = ord("a")
        s = [ord(c)-oa for c in s]
    if target is None:
        target = list(range(26))
    d = {c:-1 for c in target}
    nx = [[-1]*len(s) for _ in range(len(target))]
    if not reverse:
        i = len(s)-1
        for c in reversed(s):
            d[c] = i
            for j,t in enumerate(target):
                nx[j][i] = d[t]
            i -= 1
        if loop:
            for i in range(len(s)):
                for j in range(len(target)):
                    if nx[j][i]==-1:
                        nx[j][i] = nx[j][0]
    else:
        i = 0
        for c in s:
            d[c] = i
            for j,t in enumerate(target):
                nx[j][i] = d[t]
            i += 1
        if loop:
            for i in range(len(s)):
                for j in range(len(target)):
                    if nx[j][i]==-1:
                        nx[j][i] = nx[j][-1]
    return nx
s = input()
t = input()
n = len(s)
m = len(t)
l = [m]
j = m-1
for i in range(n)[::-1]:
    c = s[i]
    while j>=0 and t[j]!=c:
        j -= 1
    l.append(j)
    if j>=0:
        j -= 1
l = l[::-1]
if l[0]>=0:
    ans = -1
else:
    nx = snx(s)
    tnx = snx(t)
    ans = []
    i = 0
    j = 0
    done = 0
    while 1:
        for c in range(26):
            if not nx[c][i]>=0:
                continue
            if j>=m or tnx[c][j]==-1:
                ans.append(c)
                done = 1
                break
            elif l[nx[c][i]] < tnx[c][j]:
                ans.append(c)
                i = nx[c][i] + 1
                j = tnx[c][j] + 1
                break
        if done:
            break
    ans = "".join([chr(ord("a")+c) for c in ans])
print(ans)
0