結果

問題 No.1494 LCS on Tree
ユーザー mkawa2mkawa2
提出日時 2022-11-09 15:44:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 796 ms / 2,000 ms
コード長 2,537 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 142,668 KB
最終ジャッジ日時 2024-11-20 15:09:57
合計ジャッジ時間 22,843 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,348 KB
testcase_01 AC 41 ms
53,548 KB
testcase_02 AC 37 ms
53,912 KB
testcase_03 AC 725 ms
142,072 KB
testcase_04 AC 782 ms
142,252 KB
testcase_05 AC 683 ms
142,668 KB
testcase_06 AC 676 ms
142,164 KB
testcase_07 AC 668 ms
142,392 KB
testcase_08 AC 647 ms
142,640 KB
testcase_09 AC 655 ms
142,052 KB
testcase_10 AC 796 ms
142,300 KB
testcase_11 AC 662 ms
142,504 KB
testcase_12 AC 640 ms
142,336 KB
testcase_13 AC 761 ms
142,032 KB
testcase_14 AC 604 ms
141,608 KB
testcase_15 AC 595 ms
141,060 KB
testcase_16 AC 617 ms
141,644 KB
testcase_17 AC 609 ms
141,892 KB
testcase_18 AC 598 ms
141,624 KB
testcase_19 AC 37 ms
53,080 KB
testcase_20 AC 37 ms
54,720 KB
testcase_21 AC 36 ms
54,220 KB
testcase_22 AC 37 ms
54,024 KB
testcase_23 AC 35 ms
53,020 KB
testcase_24 AC 44 ms
53,112 KB
testcase_25 AC 36 ms
54,012 KB
testcase_26 AC 36 ms
53,824 KB
testcase_27 AC 37 ms
54,332 KB
testcase_28 AC 37 ms
53,840 KB
testcase_29 AC 36 ms
53,264 KB
testcase_30 AC 645 ms
141,168 KB
testcase_31 AC 784 ms
141,124 KB
testcase_32 AC 673 ms
141,952 KB
testcase_33 AC 663 ms
141,752 KB
testcase_34 AC 667 ms
141,408 KB
testcase_35 AC 150 ms
80,600 KB
testcase_36 AC 319 ms
98,004 KB
testcase_37 AC 151 ms
80,256 KB
testcase_38 AC 253 ms
92,392 KB
testcase_39 AC 389 ms
107,424 KB
testcase_40 AC 218 ms
87,324 KB
testcase_41 AC 511 ms
125,348 KB
testcase_42 AC 210 ms
87,096 KB
testcase_43 AC 472 ms
120,760 KB
testcase_44 AC 408 ms
109,980 KB
testcase_45 AC 732 ms
140,996 KB
testcase_46 AC 722 ms
140,952 KB
testcase_47 AC 714 ms
141,044 KB
testcase_48 AC 764 ms
140,964 KB
testcase_49 AC 743 ms
141,312 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:
            for i, c in enumerate(s):
                if c == cc[u]:
                    dp[u][i+1] = 1
                    break
        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)
            if 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 = v2

print(ans)
0