結果

問題 No.1494 LCS on Tree
ユーザー penguinmanpenguinman
提出日時 2021-04-25 03:16:37
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,229 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 148,680 KB
最終ジャッジ日時 2023-09-17 14:01:12
合計ジャッジ時間 19,241 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,520 KB
testcase_01 AC 72 ms
71,380 KB
testcase_02 AC 72 ms
71,580 KB
testcase_03 RE -
testcase_04 AC 482 ms
142,084 KB
testcase_05 AC 491 ms
142,404 KB
testcase_06 AC 496 ms
142,292 KB
testcase_07 AC 512 ms
144,284 KB
testcase_08 AC 493 ms
142,056 KB
testcase_09 AC 495 ms
143,560 KB
testcase_10 AC 500 ms
142,608 KB
testcase_11 AC 480 ms
142,356 KB
testcase_12 AC 485 ms
142,436 KB
testcase_13 AC 486 ms
143,192 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 484 ms
144,228 KB
testcase_17 AC 498 ms
144,512 KB
testcase_18 AC 486 ms
144,092 KB
testcase_19 AC 73 ms
71,216 KB
testcase_20 AC 72 ms
71,340 KB
testcase_21 AC 71 ms
71,440 KB
testcase_22 AC 72 ms
71,376 KB
testcase_23 AC 72 ms
71,596 KB
testcase_24 AC 72 ms
71,340 KB
testcase_25 AC 72 ms
71,540 KB
testcase_26 AC 74 ms
71,040 KB
testcase_27 AC 75 ms
71,612 KB
testcase_28 AC 73 ms
71,592 KB
testcase_29 AC 72 ms
71,400 KB
testcase_30 RE -
testcase_31 AC 937 ms
143,556 KB
testcase_32 AC 498 ms
144,204 KB
testcase_33 AC 497 ms
143,968 KB
testcase_34 AC 506 ms
144,304 KB
testcase_35 AC 133 ms
81,184 KB
testcase_36 AC 248 ms
97,508 KB
testcase_37 AC 131 ms
80,556 KB
testcase_38 AC 197 ms
92,040 KB
testcase_39 AC 310 ms
107,820 KB
testcase_40 AC 169 ms
87,184 KB
testcase_41 AC 391 ms
125,368 KB
testcase_42 AC 162 ms
86,876 KB
testcase_43 AC 416 ms
120,960 KB
testcase_44 AC 316 ms
109,828 KB
testcase_45 AC 511 ms
142,252 KB
testcase_46 AC 509 ms
141,960 KB
testcase_47 AC 517 ms
142,268 KB
testcase_48 AC 509 ms
142,104 KB
testcase_49 AC 505 ms
141,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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+1)]
rev = [[0]*(M+1) for i in range(N+1)]
ans = 0

def dfs(now):
    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):
            global ans
            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