結果

問題 No.1192 半部分列
ユーザー shotoyoo
提出日時 2021-05-25 00:50:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 2,270 bytes
コンパイル時間 309 ms
コンパイル使用メモリ 82,472 KB
実行使用メモリ 125,312 KB
最終ジャッジ日時 2024-10-13 09:00:56
合計ジャッジ時間 4,066 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

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