結果

問題 No.2606 Mirror Relay
ユーザー 👑 hos.lyrichos.lyric
提出日時 2024-01-12 21:34:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 4,448 bytes
コンパイル時間 1,245 ms
コンパイル使用メモリ 118,624 KB
実行使用メモリ 28,712 KB
最終ジャッジ日時 2024-09-27 21:28:21
合計ジャッジ時間 4,107 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 14 ms
27,200 KB
testcase_40 AC 14 ms
27,180 KB
testcase_41 AC 14 ms
27,200 KB
testcase_42 AC 14 ms
27,244 KB
testcase_43 AC 14 ms
27,136 KB
testcase_44 AC 14 ms
27,132 KB
testcase_45 AC 13 ms
27,152 KB
testcase_46 AC 14 ms
27,132 KB
testcase_47 AC 14 ms
27,124 KB
testcase_48 AC 13 ms
27,136 KB
testcase_49 AC 16 ms
27,664 KB
testcase_50 AC 16 ms
27,576 KB
testcase_51 AC 18 ms
27,644 KB
testcase_52 AC 17 ms
27,448 KB
testcase_53 AC 17 ms
27,640 KB
testcase_54 AC 16 ms
27,576 KB
testcase_55 AC 18 ms
27,932 KB
testcase_56 AC 16 ms
27,564 KB
testcase_57 AC 16 ms
27,612 KB
testcase_58 AC 17 ms
27,672 KB
testcase_59 AC 14 ms
27,288 KB
testcase_60 AC 13 ms
27,204 KB
testcase_61 AC 14 ms
27,156 KB
testcase_62 AC 14 ms
27,280 KB
testcase_63 AC 14 ms
27,184 KB
testcase_64 AC 14 ms
27,192 KB
testcase_65 AC 13 ms
27,180 KB
testcase_66 AC 14 ms
27,216 KB
testcase_67 AC 14 ms
27,188 KB
testcase_68 AC 14 ms
27,340 KB
testcase_69 AC 21 ms
28,712 KB
testcase_70 AC 2 ms
6,944 KB
testcase_71 AC 2 ms
6,940 KB
testcase_72 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


// alphabet is [OFFSET, OFFSET + SIZE), with sentinel (OFFSET - 1)
template <class T, int SIZE, T OFFSET> struct Pam {
  struct Node {
    // ts[pos, pos + len]: suffix when created
    // fail: longest proper palindromic suffix
    int len, pos, fail;
    int nxt[SIZE];
  };

  // nodes[0]: length 0  (also means null in nxt)
  // nodes[1]: length -1
  // nodes[2, nodesLen): non-empty palindromic substring
  // suf: node for longest palindromic suffix
  int nodesLen, suf;
  vector<Node> nodes;
  // current whole string: ts[0, r)  (0 <= r <= nR)
  int nR, r;
  vector<T> tsBuffer;
  T *ts;

  Pam(int nR_) : nR(nR_) {
    nodesLen = 2;
    suf = 0;
    nodes.resize(2 + nR);
    nodes[0].len =  0; nodes[0].pos = 0; nodes[0].fail = 1;
    nodes[1].len = -1; nodes[1].pos = 0; nodes[1].fail = 1;
    memset(nodes[0].nxt, 0, sizeof(nodes[0].nxt));
    memset(nodes[1].nxt, 0, sizeof(nodes[1].nxt));
    r = 0;
    tsBuffer.assign(1 + nR, OFFSET - 1);
    ts = tsBuffer.data() + 1;
  }
  const Node &operator[](int u) const {
    return nodes[u];
  }

  void pushBack(T t) {
    assert(r < nR);
    const int a = t - OFFSET;
    ts[r++] = t;
    for (; ts[r - 2 - nodes[suf].len] != t; suf = nodes[suf].fail) {}
    Node &f = nodes[suf];
    if (!f.nxt[a]) {
      Node &g = nodes[nodesLen];
      g.len = f.len + 2; g.pos = r - g.len;
      int u = f.fail;
      for (; ts[r - 2 - nodes[u].len] != t; u = nodes[u].fail) {}
      g.fail = nodes[u].nxt[a];
      memset(g.nxt, 0, sizeof(g.nxt));
      f.nxt[a] = nodesLen++;  // this needs to be after setting g.fail
    }
    suf = f.nxt[a];
  }

  void dfsPrint(ostream &os, int u, const string &branch, int type) const {
    const Node &f = nodes[u];
    os << branch << ((type == 0) ? "" : (type == 1) ? "|-- " : "`-- ");
    if (f.len <= 0) {
      os << "(" << f.len << ")";
    } else {
      for (int i = f.pos; i < f.pos + f.len; ++i) os << ts[i];
    }
    os << " " << u << " " << f.fail;
    // debug here
    os << "\n";
    int a0 = -1;
    for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) a0 = a;
    for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) {
      dfsPrint(os, f.nxt[a],
               branch + ((type == 0) ? "" : (type == 1) ? "|   " : "    "),
               (a == a0) ? 2 : 1);
    }
  }
  friend ostream &operator<<(ostream &os, const Pam &pam) {
    pam.dfsPrint(os, 0, "  ", 0);
    pam.dfsPrint(os, 1, "", 0);
    return os;
  }
};


int N;
char S[200'010];

int main() {
  for (; ~scanf("%s", S); ) {
    N = strlen(S);
    
    Pam<char, 26, 'a'> pam(N);
    vector<int> us(N + 1);
    us[0] = 0;
    for (int i = 0; i < N; ++i) {
      pam.pushBack(S[i]);
      us[i + 1] = pam.suf;
    }
// cerr<<pam<<"us = "<<us<<endl;
    
    vector<Int> dp(pam.nodesLen, 0);
    for (int i = 1; i <= N; ++i) ++dp[us[i]];
// cerr<<"dp = "<<dp<<endl;
    for (int u = pam.nodesLen; --u >= 2; ) dp[pam.nodes[u].fail] += dp[u];
// cerr<<"dp = "<<dp<<endl;
    for (int u = 0; u < pam.nodesLen; ++u) dp[u] *= pam.nodes[u].len;
    for (int u = 2; u < pam.nodesLen; ++u) dp[u] += dp[pam.nodes[u].fail];
// cerr<<"dp = "<<dp<<endl;
    Int ans = 0;
    for (int u = 0; u < pam.nodesLen; ++u) chmax(ans, dp[u]);
    printf("%lld\n", ans);
  }
  return 0;
}
0