結果

問題 No.1909 Detect from Substrings
ユーザー 👑 rin204rin204
提出日時 2022-04-22 21:34:40
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 715 ms / 2,000 ms
コード長 896 bytes
コンパイル時間 160 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 118,896 KB
最終ジャッジ日時 2024-06-24 02:39:37
合計ジャッジ時間 14,117 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 9)

n, m = map(int, input().split())
lst = [input() for _ in range(n)]
S = lst[0]
T = lst[1]
tmp = []
ans = 0

def ok():
    for S in lst:
        p = 0
        for t in tmp:
            if S[p] == t:
                p += 1
                if p == m:
                    break
        if p != m:
            return False
    return True
    

def dfs(sp, tp, x):
    if sp == m and tp == m:
        global ans
        if ok():
            ans += 1
        return
    if sp < m and tp < m and S[sp] == T[tp]:
        tmp.append(S[sp])
        dfs(sp + 1, tp + 1, x)
        tmp.pop()
    else:
        if sp < m and not x & 1:
            tmp.append(S[sp])
            dfs(sp + 1, tp, x ^ 1)
            tmp.pop()
        if tp < m and not x & 2:
            tmp.append(T[tp])
            dfs(sp, tp + 1, x ^ 2)
            tmp.pop()

dfs(0, 0, 0)
print(ans)
0