結果

問題 No.1494 LCS on Tree
ユーザー penguinmanpenguinman
提出日時 2021-04-25 03:23:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,301 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 77,324 KB
最終ジャッジ日時 2023-09-17 14:01:43
合計ジャッジ時間 3,766 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
12,488 KB
testcase_01 AC 18 ms
8,180 KB
testcase_02 AC 17 ms
8,068 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import resource
sys.setrecursionlimit(3000)
N = int(input())
S = input()
flag = [False]*N
edge = [[]for i in range(N)]
char = [[]for i in range(N)]
for i in range(N-1):
    x, y, z = input().split()
    x = int(x)-1
    y = int(y)-1
    edge[x].append(y)
    edge[y].append(x)
    char[x].append(z)
    char[y].append(z)
M = len(S)
dp = [[0]*(M+1) for i in range(N)]
rev = [[0]*(M+1) for i in range(N)]
ans = 0

def dfs(now):
    global dp
    global rev
    global ans
    flag[now] = True
    for k in range(len(edge[now])):
        nex = edge[now][k]
        if flag[nex]:
            continue
        dfs(nex)
        for i in reversed(range(M)):
            if S[i] == char[now][k]:
                dp[nex][i+1] = max(dp[nex][i+1], dp[nex][i]+1)
        for i in range(M):
            dp[nex][i+1] = max(dp[nex][i+1], dp[nex][i])
        for i in range(M):
            if S[i] == char[now][k]:
                rev[nex][i] = max(rev[nex][i], rev[nex][i+1]+1)
        for i in reversed(range(M)):
            rev[nex][i] = max(rev[nex][i], rev[nex][i+1])
        for i in range(M+1):
            ans = max(ans, dp[now][i]+rev[nex][i], rev[now][i]+dp[nex][i])
            dp[now][i] = max(dp[now][i], dp[nex][i])
            rev[now][i] = max(rev[now][i], rev[nex][i])

dfs(0)
print(ans)
0