結果

問題 No.1494 LCS on Tree
ユーザー penguinmanpenguinman
提出日時 2021-04-25 03:22:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 503 ms / 2,000 ms
コード長 1,301 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 82,504 KB
実行使用メモリ 146,636 KB
最終ジャッジ日時 2024-04-30 17:40:35
合計ジャッジ時間 17,365 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
60,692 KB
testcase_01 AC 47 ms
61,584 KB
testcase_02 AC 45 ms
60,992 KB
testcase_03 AC 496 ms
144,620 KB
testcase_04 AC 472 ms
142,752 KB
testcase_05 AC 482 ms
142,688 KB
testcase_06 AC 477 ms
142,520 KB
testcase_07 AC 479 ms
142,472 KB
testcase_08 AC 476 ms
142,464 KB
testcase_09 AC 463 ms
142,468 KB
testcase_10 AC 479 ms
142,776 KB
testcase_11 AC 472 ms
142,844 KB
testcase_12 AC 475 ms
142,684 KB
testcase_13 AC 476 ms
142,828 KB
testcase_14 AC 492 ms
145,648 KB
testcase_15 AC 486 ms
145,052 KB
testcase_16 AC 473 ms
144,668 KB
testcase_17 AC 480 ms
144,516 KB
testcase_18 AC 473 ms
144,416 KB
testcase_19 AC 47 ms
61,100 KB
testcase_20 AC 46 ms
60,636 KB
testcase_21 AC 46 ms
62,180 KB
testcase_22 AC 45 ms
60,424 KB
testcase_23 AC 45 ms
60,556 KB
testcase_24 AC 46 ms
60,460 KB
testcase_25 AC 46 ms
60,992 KB
testcase_26 AC 46 ms
60,900 KB
testcase_27 AC 46 ms
60,380 KB
testcase_28 AC 46 ms
60,244 KB
testcase_29 AC 46 ms
61,508 KB
testcase_30 AC 496 ms
146,636 KB
testcase_31 AC 497 ms
144,564 KB
testcase_32 AC 491 ms
144,768 KB
testcase_33 AC 484 ms
144,036 KB
testcase_34 AC 490 ms
144,204 KB
testcase_35 AC 115 ms
81,616 KB
testcase_36 AC 230 ms
97,944 KB
testcase_37 AC 111 ms
81,444 KB
testcase_38 AC 178 ms
93,280 KB
testcase_39 AC 290 ms
107,984 KB
testcase_40 AC 154 ms
88,048 KB
testcase_41 AC 379 ms
126,108 KB
testcase_42 AC 143 ms
87,164 KB
testcase_43 AC 399 ms
121,180 KB
testcase_44 AC 303 ms
110,012 KB
testcase_45 AC 498 ms
142,420 KB
testcase_46 AC 498 ms
142,496 KB
testcase_47 AC 503 ms
142,540 KB
testcase_48 AC 499 ms
142,852 KB
testcase_49 AC 496 ms
142,344 KB
権限があれば一括ダウンロードができます

ソースコード

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