結果

問題 No.1494 LCS on Tree
ユーザー 👑 potato167potato167
提出日時 2023-04-14 15:28:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,239 bytes
コンパイル時間 2,422 ms
コンパイル使用メモリ 211,984 KB
実行使用メモリ 50,688 KB
最終ジャッジ日時 2024-04-18 13:49:38
合計ジャッジ時間 5,983 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 73 ms
50,432 KB
testcase_05 AC 73 ms
50,560 KB
testcase_06 AC 73 ms
50,432 KB
testcase_07 AC 72 ms
50,432 KB
testcase_08 AC 73 ms
50,688 KB
testcase_09 AC 73 ms
50,432 KB
testcase_10 AC 72 ms
50,432 KB
testcase_11 AC 74 ms
50,432 KB
testcase_12 AC 74 ms
50,560 KB
testcase_13 AC 73 ms
50,432 KB
testcase_14 AC 70 ms
50,688 KB
testcase_15 AC 70 ms
50,432 KB
testcase_16 AC 71 ms
50,560 KB
testcase_17 AC 71 ms
50,432 KB
testcase_18 AC 70 ms
50,560 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 WA -
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 5 ms
5,760 KB
testcase_36 AC 24 ms
17,280 KB
testcase_37 AC 5 ms
5,760 KB
testcase_38 AC 17 ms
14,336 KB
testcase_39 AC 34 ms
24,704 KB
testcase_40 AC 13 ms
10,496 KB
testcase_41 AC 53 ms
38,016 KB
testcase_42 AC 11 ms
9,984 KB
testcase_43 AC 49 ms
34,560 KB
testcase_44 AC 37 ms
26,368 KB
testcase_45 AC 75 ms
50,432 KB
testcase_46 AC 74 ms
50,688 KB
testcase_47 AC 74 ms
50,432 KB
testcase_48 AC 74 ms
50,432 KB
testcase_49 AC 75 ms
50,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
using ll = long long;
#define all(p) p.begin(),p.end()
const int mod=998244353;
int main(){
	int N;
	cin>>N;
	string S;
	cin>>S;
	vector<vector<pair<int,char>>> G(N);
	rep(i,0,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});
	}
	vector<int> order={0},pare(N,-1);
	pare[0]=-2;
	rep(i,0,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;
	int M=S.size();
	vector<string> T={S};
	reverse(all(S));
	T.push_back(S);
	vector<vector<vector<int>>> dp(2,vector<vector<int>>(N,vector<int>(M+1)));
	for(int po=N-1;po>=0;po--){
		int a=order[po];
		for(auto x:G[a]){
			if(pare[x.first]!=a) continue;
			rep(i,0,2){
				vector<int> n_dp(M+1);
				rep(j,0,M){
					if(x.second==T[i][j]){
						n_dp[j+1]=dp[i][x.first][j]+1;
					}
				}
				rep(j,1,M+1){
					dp[i][x.first][j]=max(n_dp[j],dp[i][x.first][j-1]);
				}
			}
			rep(i,0,2){
				rep(j,0,M+1) ans=max(ans,dp[i][a][j]+dp[1-i][x.first][M-j]);
			}
			rep(i,0,2){
				rep(j,0,M+1) dp[i][a][j]=max(dp[i][a][j],dp[i][x.first][j]);
			}
		}
	}
	cout<<ans<<"\n";
}
0