結果

問題 No.1494 LCS on Tree
ユーザー mkawa2mkawa2
提出日時 2022-11-09 14:28:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,537 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,148 KB
実行使用メモリ 142,164 KB
最終ジャッジ日時 2024-07-22 20:28:06
合計ジャッジ時間 23,372 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,748 KB
testcase_01 AC 38 ms
52,820 KB
testcase_02 AC 38 ms
54,304 KB
testcase_03 WA -
testcase_04 AC 802 ms
141,780 KB
testcase_05 AC 733 ms
141,804 KB
testcase_06 WA -
testcase_07 AC 706 ms
141,832 KB
testcase_08 AC 696 ms
141,696 KB
testcase_09 AC 703 ms
141,852 KB
testcase_10 AC 833 ms
141,496 KB
testcase_11 AC 715 ms
141,528 KB
testcase_12 AC 693 ms
141,716 KB
testcase_13 AC 833 ms
141,792 KB
testcase_14 AC 547 ms
141,080 KB
testcase_15 AC 572 ms
141,276 KB
testcase_16 AC 597 ms
141,012 KB
testcase_17 AC 605 ms
141,376 KB
testcase_18 AC 580 ms
141,412 KB
testcase_19 AC 37 ms
52,480 KB
testcase_20 AC 38 ms
52,608 KB
testcase_21 AC 39 ms
52,864 KB
testcase_22 AC 38 ms
52,864 KB
testcase_23 AC 37 ms
52,864 KB
testcase_24 AC 37 ms
52,480 KB
testcase_25 AC 38 ms
52,864 KB
testcase_26 AC 39 ms
52,736 KB
testcase_27 AC 41 ms
52,992 KB
testcase_28 AC 39 ms
52,736 KB
testcase_29 AC 37 ms
52,480 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 588 ms
141,320 KB
testcase_33 WA -
testcase_34 AC 591 ms
141,084 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 156 ms
80,404 KB
testcase_38 AC 271 ms
92,168 KB
testcase_39 AC 419 ms
107,072 KB
testcase_40 AC 224 ms
87,340 KB
testcase_41 AC 563 ms
125,028 KB
testcase_42 AC 218 ms
86,480 KB
testcase_43 AC 514 ms
120,320 KB
testcase_44 AC 432 ms
108,828 KB
testcase_45 AC 758 ms
140,712 KB
testcase_46 AC 776 ms
140,996 KB
testcase_47 AC 768 ms
140,896 KB
testcase_48 AC 815 ms
141,176 KB
testcase_49 AC 766 ms
140,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

n = II()
s = [ord(c)-97 for c in SI()]
m = len(s)
to = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, c = SI().split()
    u = int1(u)
    v = int1(v)
    c = ord(c)-97
    to[u].append((v, c))
    to[v].append((u, c))

parent, depth, vis = [-1]*n, [0]*n, [0]*n
cc = [""]*n

def dfs(root):
    uu = []
    vis[root] = 1
    stack = [root]
    while stack:
        iu = stack.pop()
        i, u = divmod(iu, n)
        if i == 0:
            uu.append(u)
        # care to's format
        while i < len(to[u]) and vis[to[u][i][0]]: i += 1
        if i < len(to[u]):
            stack.append((i+1)*n+u)
            # care to's format
            v, c = to[u][i]
            vis[v] = 1
            stack.append(v)
            parent[v] = u
            depth[v] = depth[u]+1
            cc[v] = c

    return uu

uu = dfs(0)

def dodp(s):
    def chmax(i, j, val):
        if val > dp[i][j]: dp[i][j] = val

    dp = [[0]*(m+1) for _ in range(n)]
    for u in uu[::-1]:
        if u == 0: break
        if len(to[u]) == 1:
            val = 0
            for i, c in enumerate(s):
                if c == cc[u]: val = 1
                dp[u][i+1] = val
        p = parent[u]
        for i in range(m+1):
            pre = dp[u][i]
            if i<m and cc[p] == s[i]:
                chmax(p, i+1, pre+1)
            elif pre:
                chmax(p, i, pre)
                if i+1 <= m: chmax(u, i+1, pre)

    return dp

dp = dodp(s)
rdp = dodp(s[::-1])
# pDB(rdp)

ans = 0
for u in range(n):
    for i in range(m+1):
        m1 = m2 = 0
        for v, _ in to[u]:
            if v == parent[u]: continue
            v1, v2 = dp[v][i], rdp[v][m-i]
            if v1+m2 > ans: ans = v1+m2
            if v2+m1 > ans: ans = v2+m1
            if v1 > m1: m1 = v1
            if v2 > m2: m2 = m2

print(ans)
0