#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } // graph template // ref : https://ei1333.github.io/library/graph/graph-template.cpp template struct Edge{ int from,to; T w; int idx; Edge()=default; Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){} operator int() const{return to;} }; template struct Graph{ vector>> g; int V,E; Graph()=default; Graph(int n):g(n),V(n),E(0){} size_t size(){ return g.size(); } void resize(int k){ g.resize(k); } inline const vector> &operator[](int k)const{ return (g.at(k)); } inline vector> &operator[](int k){ return (g.at(k)); } void add_directed_edge(int from,int to,T cost=1){ g[from].emplace_back(from,to,cost,E++); } void add_edge(int from,int to,T cost=1){ g[from].emplace_back(from,to,cost,E); g[to].emplace_back(to,from,cost,E++); } void read(int m,int pad=-1,bool weighted=false,bool directed=false){ for(int i=0;i>u>>v; u+=pad,v+=pad; T w=T(1); if(weighted) cin>>w; if(directed) add_directed_edge(u,v,w); else add_edge(u,v,w); } } }; template struct Rerooting{ using E=function)>;//集合,頂点番号 using M=function; using F=function; Graph g; // dp_v = fix(merge(f(dp_c1,edge(v,c1)),...,f(dp_ck,edge(v,ck))),v) E f; // f(T,Edge)->T 辺情報を考慮し,merge前の形 M merge;// TxT->T,子を集約する関数,モノイド T e; // mergeの単位元 F fix; // TxN->T 補正 vector> dp; vector ans; Rerooting(Graph &g,E f,M merge,T e,F fix=[](T a,int b){return a;}): g(g),f(f),merge(merge),e(e),fix(fix),dp(g.size()),ans(g.size(),e){} T dfs1(int pre,int now){ T ret=e; for(int i=0;i<(int)g[now].size();i++)if(g[now][i]!=pre){ dp[now][i]=dfs1(now,g[now][i]); ret=merge(ret,f(dp[now][i],g[now][i])); } return fix(ret,now); } void dfs2(int pre,int now,T frompar){ for(int i=0;i<(int)g[now].size();i++){ if(g[now][i]==pre){ dp[now][i]=frompar; break; } } vector lsum(g[now].size()+1),rsum(g[now].size()+1);//親も混ぜて累積 lsum[0]=e;rsum[g[now].size()]=e; for(int i=0;i<(int)g[now].size();i++) lsum[i+1]=merge(lsum[i],f(dp[now][i],g[now][i])); for(int i=(int)g[now].size();i>0;i--) rsum[i-1]=merge(rsum[i],f(dp[now][i-1],g[now][i-1])); for(int i=0;i<(int)g[now].size();i++)if(g[now][i]!=pre){ T fromme=merge(lsum[i],rsum[i+1]); dfs2(now,g[now][i],fix(fromme,now)); } } void build(int root=0){ for(int i=0;i<(int)g.size();i++) dp[i].resize(g[i].size()); dfs1(-1,root); dfs2(-1,root,e); for(int i=0;i<(int)g.size();i++){ for(int j=0;j<(int)g[i].size();j++) ans[i]=merge(ans[i],f(dp[i][j],g[i][j])); ans[i]=fix(ans[i],i); } } }; vector> solve(Graph &G,string S,vector> &nxt){ cout<<"Hello "<> dp(N,vector(M+1,0)); function dfs=[&](int pre,int cur){ for(auto &e:G[cur])if(e.to!=pre){ dfs(cur,e.to); rep(j,M+1){ chmax(dp[cur][j],dp[e][j]); int nj=nxt[j][e.w-'a']; if(nj>N; string S;cin>>S; Graph G(N); G.read(N-1,-1,true); int M=(int)S.size(); vector> nxt(M+1,vector(26,M)); for(int i=M-1;i>=0;i--){ rep(j,26) nxt[i][j]=nxt[i+1][j]; nxt[i][S[i]-'a']=i; } auto f=[&](vector v,Edge e){ vector ret(M+1,-INF); rep(i,M+1){ int ni=nxt[i][e.w-'a']; chmax(ret[i],v[i]); if(ni l,vector r){ rep(i,M+1) l[i]=max(l[i],r[i]); return l; }; vector e(M+1,-INF); e[0]=0; Rerooting,char> R(G,f,merge,e); R.build(); int ans=0; rep(i,N)rep(j,M+1) chmax(ans,R.ans[i][j]); cout<