#include #pragma GCC optimize("Ofast") #define _GLIBCXX_DEBUG using namespace std; using std::cout; using std::cin; using std::endl; using ll=long long; using ld=long double; ll ILL=2167167167167167167; const int INF=2100000000; const ll mod=998244353; #define rep(i,a) for (int i=0;i using _pq = priority_queue, greater>; template ll LB(vector &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();} template ll UB(vector &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();} template bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;} template bool chmax(T &a,const T &b){if(a void So(vector &v) {sort(v.begin(),v.end());} template void Sore(vector &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});} void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";} template void vec_out(vector &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<>t; rep(i,t) solve(); } void solve(){ int N; cin>>N; string S; cin>>S;int M=S.size(); vector>> G(N); map,char> edge; rep(i,N-1){ int a,b; char c; cin>>a>>b>>c; a--,b--; G[a].push_back({b,c}); G[b].push_back({a,c}); edge[{a,b}]=c; edge[{b,a}]=c; } vector> dp1(N,vector(M+1)); vector> dp2(N,vector(M+1)); vector order={0},pare(N,-1); pare[0]=-2; rep(i,N){ int a=order[i]; for(auto x:G[a]){ if(pare[x.first]==-1){ pare[x.first]=a; order.push_back(x.first); } } } int ans=0; for(int i=N-1;i>=0;i--){ int a=order[i]; for(auto x:G[a]){ if(pare[a]==x.first) continue; rep(j,M+1){ chmax(dp1[a][j],dp1[x.first][j]); chmax(dp2[a][j],dp2[x.first][j]); } } if(i==0) continue; char c=edge[{a,pare[a]}]; vector n_dp(M+1); rep(j,M){ if(S[j]==c) n_dp[j+1]=dp1[a][j]+1; else n_dp[j+1]=max(n_dp[j],dp1[a][j]); } swap(dp1[a],n_dp); rep(j,M+1) n_dp[j]=0; for(int j=M-1;j>=0;j--){ if(S[j]==c) n_dp[j]=dp2[a][j+1]+1; else n_dp[j]=max(dp2[a][j+1],n_dp[j+1]); } swap(dp2[a],n_dp); } rep(i,N){ rep(j,M+1){ int X=0,Y=0,x_ind=-1,y_ind=-2; int A=0,B=0,a_ind=-1,b_ind=-2; for(auto x:G[i]){ if(pare[x.first]==i){ if(chmax(Y,dp1[x.first][j])){ y_ind=x.first; if(Y>X) swap(x_ind,y_ind),swap(X,Y); } if(chmax(B,dp2[x.first][j])){ b_ind=x.first; if(B>A) swap(a_ind,b_ind),swap(A,B); } } } if(x_ind!=a_ind) chmax(ans,X+A); else chmax(ans,max(X+B,Y+A)); } } chmax(ans,max(dp1[0][M],dp2[0][0])); cout<