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 # class t2: # def __init__(self): # self.v1 = 0 # self.u1 = -1 # self.v2 = -1 # self.u2 = -1 # # def __repr__(self): # return f"({self.v1},{self.u1},{self.v2},{self.u2})" # # def update(self, v, u): # if v > self.v1: # self.v2, self.u2 = self.v1, self.u1 # self.v1, self.u1 = v, u # elif v > self.v2 and u != self.u1: # self.v2, self.u2 = v, u # # def merge(self, other): # if self.u1 == other.u1: # return max(self.v1+other.v2, self.v2+other.v1) # return other.v1+self.v1 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, u): if val > dp[i][j][0]: dp[i][j][0], dp[i][j][1] = val, u return if val > dp[i][j][2] and u != dp[i][j][1]: dp[i][j][2], dp[i][j][3] = val, u return dp = [[[0, -1, -1, -1] for _ in range(m+1)] for _ in range(n)] for u in uu[::-1]: p = parent[u] for i, c in enumerate(s): pre = dp[u][i] if c == cc[u]: chmax(p, i+1, pre[0]+1, u) elif pre[0]: if p != -1: chmax(p, i, pre[0], u) if i+1 <= m: chmax(u, i+1, pre[0], pre[1]) return dp dp = dodp(s) rdp = dodp(s[::-1]) ans = max(dp[0][m][0], rdp[0][m][0]) for u in range(n): for i in range(m+1): a, b, c, d = dp[u][i] p, q, r, s = rdp[u][m-i] if b == q: ans = max(ans, a+r, c+p) else: ans = max(ans, a+p) # pDB(u, i, dp[u][i], rdp[u][m-i]) # pDB(ans) print(ans)