結果

問題 No.1494 LCS on Tree
ユーザー Iván SotoIván Soto
提出日時 2021-05-10 13:59:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 3,441 bytes
コンパイル時間 2,399 ms
コンパイル使用メモリ 213,008 KB
実行使用メモリ 34,848 KB
最終ジャッジ日時 2023-08-12 22:49:35
合計ジャッジ時間 6,283 ms
ジャッジサーバーID
(参考情報)
judge7 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 72 ms
34,768 KB
testcase_04 AC 73 ms
34,736 KB
testcase_05 AC 74 ms
34,768 KB
testcase_06 AC 74 ms
34,728 KB
testcase_07 AC 73 ms
34,724 KB
testcase_08 AC 74 ms
34,744 KB
testcase_09 AC 74 ms
34,776 KB
testcase_10 AC 73 ms
34,776 KB
testcase_11 AC 73 ms
34,724 KB
testcase_12 AC 73 ms
34,748 KB
testcase_13 AC 74 ms
34,732 KB
testcase_14 AC 74 ms
34,704 KB
testcase_15 AC 74 ms
34,764 KB
testcase_16 AC 73 ms
34,780 KB
testcase_17 AC 73 ms
34,772 KB
testcase_18 AC 73 ms
34,776 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 73 ms
34,712 KB
testcase_31 AC 74 ms
34,732 KB
testcase_32 AC 74 ms
34,772 KB
testcase_33 AC 74 ms
34,712 KB
testcase_34 AC 74 ms
34,848 KB
testcase_35 AC 6 ms
4,900 KB
testcase_36 AC 24 ms
12,568 KB
testcase_37 AC 5 ms
4,640 KB
testcase_38 AC 18 ms
10,420 KB
testcase_39 AC 35 ms
17,300 KB
testcase_40 AC 12 ms
7,864 KB
testcase_41 AC 55 ms
26,272 KB
testcase_42 AC 12 ms
7,540 KB
testcase_43 AC 50 ms
23,972 KB
testcase_44 AC 37 ms
18,384 KB
testcase_45 AC 74 ms
34,724 KB
testcase_46 AC 74 ms
34,704 KB
testcase_47 AC 74 ms
34,720 KB
testcase_48 AC 74 ms
34,712 KB
testcase_49 AC 74 ms
34,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *  author: ivanzuki   
 *  created: Sun May 09 2021
**/
#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
template<typename T> void dout(string name, int idx, T arg) {
  cerr << name << " = " << to_string(arg);
}

template<typename T1, typename... T2> void dout(string names, int idx, T1 arg, T2... args) {
  cerr << names.substr(0, names.find(',')) << " = " << to_string(arg) << ", ";
  dout(names.substr(names.find(',') + 2), idx + 1, args...);
}

#ifdef LOCAL
#define debug(...) cerr << "[", dout(#__VA_ARGS__, 0, __VA_ARGS__), cerr << "]" << endl;
#else
#define debug(...) 42
#endif

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<vector<pair<int, char>>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    char c;
    cin >> a >> b >> c;
    --a; --b;
    g[a].emplace_back(b, c);
    g[b].emplace_back(a, c);
  }
  vector<int> ans(n);
  vector<vector<int>> pref(n, vector<int>((int) s.size())), suff(n, vector<int>((int) s.size()));
  function<void(int, int)> DFS = [&](int v, int pv) {
    for (const auto& e : g[v]) {
      int u = e.first;
      char c = e.second;
      if (u == pv) {
        continue;
      }
      DFS(u, v);
      for (int k = 0; k <= (int) s.size(); k++) {
        ans[v] = max(ans[v], (k - 1 < 0 ? 0 : pref[v][k - 1]) + (k < (int) s.size() ? suff[u][k] : 0));
        ans[v] = max(ans[v], (k - 1 < 0 ? 0 : pref[u][k - 1]) + (k < (int) s.size() ? suff[v][k] : 0));
        if (k < (int) s.size() && s[k] == c) {
          ans[v] = max(ans[v], 1 + (k - 1 < 0 ? 0 : pref[v][k - 1]) + (k + 1 < (int) s.size() ? suff[u][k + 1] : 0));
          ans[v] = max(ans[v], 1 + (k - 1 < 0 ? 0 : pref[u][k - 1]) + (k + 1 < (int) s.size() ? suff[v][k + 1] : 0));
        }
      }
      for (int k = 0; k < (int) s.size(); k++) {
        pref[v][k] = max(pref[v][k], pref[u][k]);
        if (s[k] == c) {
          pref[v][k] = max(pref[v][k], 1 + (k - 1 < 0 ? 0 : pref[u][k - 1]));
        }
        if (k > 0) {
          pref[v][k] = max(pref[v][k], pref[v][k - 1]);
        }
      }
      for (int k = (int) s.size() - 1; k >= 0; k--) {
        suff[v][k] = max(suff[v][k], suff[u][k]);
        if (s[k] == c) {
          suff[v][k] = max(suff[v][k], 1 + (k + 1 <= (int) s.size() - 1 ? suff[u][k + 1] : 0));
        }
        if (k + 1 <= (int) s.size() - 1) {
          suff[v][k] = max(suff[v][k], suff[v][k + 1]);
        }
      }
    }
    ans[v] = max({ans[v], 
          *max_element(pref[v].begin(), pref[v].end()), 
          *max_element(suff[v].begin(), suff[v].end())});
  };
  DFS(0, -1);
  debug(pref, suff, ans);
  cout << *max_element(ans.begin(), ans.end()) << '\n';
  return 0;
}
0