結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,244 KB
testcase_01 AC 72 ms
71,272 KB
testcase_02 AC 72 ms
71,056 KB
testcase_03 RE -
testcase_04 AC 478 ms
142,252 KB
testcase_05 AC 489 ms
142,200 KB
testcase_06 AC 488 ms
141,968 KB
testcase_07 AC 498 ms
144,128 KB
testcase_08 AC 480 ms
142,100 KB
testcase_09 AC 482 ms
143,456 KB
testcase_10 AC 488 ms
142,508 KB
testcase_11 AC 492 ms
142,180 KB
testcase_12 AC 486 ms
141,988 KB
testcase_13 AC 488 ms
143,156 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 487 ms
144,048 KB
testcase_17 AC 486 ms
144,440 KB
testcase_18 AC 488 ms
144,192 KB
testcase_19 AC 74 ms
71,292 KB
testcase_20 AC 74 ms
71,248 KB
testcase_21 AC 74 ms
71,396 KB
testcase_22 AC 74 ms
71,288 KB
testcase_23 AC 74 ms
71,420 KB
testcase_24 AC 74 ms
71,476 KB
testcase_25 AC 75 ms
71,248 KB
testcase_26 AC 75 ms
71,336 KB
testcase_27 AC 75 ms
71,124 KB
testcase_28 AC 75 ms
71,192 KB
testcase_29 AC 75 ms
71,284 KB
testcase_30 RE -
testcase_31 AC 948 ms
143,356 KB
testcase_32 AC 513 ms
144,316 KB
testcase_33 AC 497 ms
143,552 KB
testcase_34 AC 506 ms
144,048 KB
testcase_35 AC 131 ms
80,508 KB
testcase_36 AC 247 ms
97,404 KB
testcase_37 AC 132 ms
80,248 KB
testcase_38 AC 199 ms
91,884 KB
testcase_39 AC 310 ms
108,004 KB
testcase_40 AC 171 ms
86,712 KB
testcase_41 AC 397 ms
125,316 KB
testcase_42 AC 162 ms
86,700 KB
testcase_43 AC 412 ms
120,796 KB
testcase_44 AC 323 ms
110,008 KB
testcase_45 AC 512 ms
141,948 KB
testcase_46 AC 509 ms
141,712 KB
testcase_47 AC 514 ms
142,208 KB
testcase_48 AC 521 ms
141,792 KB
testcase_49 AC 513 ms
141,612 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)
        global dp
        global rev
        global ans
        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