結果

問題 No.962 LCPs
ユーザー risujirohrisujiroh
提出日時 2019-12-24 23:27:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 2,237 bytes
コンパイル時間 2,067 ms
コンパイル使用メモリ 182,392 KB
実行使用メモリ 46,596 KB
最終ジャッジ日時 2023-10-23 22:19:53
合計ジャッジ時間 6,337 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 3 ms
4,368 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 2 ms
4,368 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 2 ms
4,368 KB
testcase_09 AC 2 ms
4,368 KB
testcase_10 AC 2 ms
4,368 KB
testcase_11 AC 2 ms
4,368 KB
testcase_12 AC 1 ms
4,368 KB
testcase_13 AC 2 ms
4,368 KB
testcase_14 AC 2 ms
4,368 KB
testcase_15 AC 2 ms
4,368 KB
testcase_16 AC 2 ms
4,368 KB
testcase_17 AC 107 ms
46,596 KB
testcase_18 AC 77 ms
24,700 KB
testcase_19 AC 78 ms
24,700 KB
testcase_20 AC 62 ms
18,392 KB
testcase_21 AC 60 ms
18,392 KB
testcase_22 AC 56 ms
14,804 KB
testcase_23 AC 56 ms
14,804 KB
testcase_24 AC 52 ms
12,396 KB
testcase_25 AC 51 ms
12,396 KB
testcase_26 AC 47 ms
10,868 KB
testcase_27 AC 56 ms
15,320 KB
testcase_28 AC 49 ms
12,080 KB
testcase_29 AC 43 ms
10,044 KB
testcase_30 AC 55 ms
14,512 KB
testcase_31 AC 63 ms
19,960 KB
testcase_32 AC 43 ms
9,756 KB
testcase_33 AC 75 ms
24,172 KB
testcase_34 AC 49 ms
11,952 KB
testcase_35 AC 48 ms
11,208 KB
testcase_36 AC 53 ms
14,084 KB
testcase_37 AC 41 ms
9,140 KB
testcase_38 AC 40 ms
9,140 KB
testcase_39 AC 40 ms
9,132 KB
testcase_40 AC 40 ms
9,152 KB
testcase_41 AC 40 ms
9,140 KB
testcase_42 AC 40 ms
9,148 KB
testcase_43 AC 39 ms
9,160 KB
testcase_44 AC 39 ms
9,152 KB
testcase_45 AC 42 ms
9,136 KB
testcase_46 AC 42 ms
9,164 KB
testcase_47 AC 9 ms
5,552 KB
testcase_48 AC 8 ms
5,016 KB
testcase_49 AC 10 ms
5,968 KB
testcase_50 AC 11 ms
5,888 KB
testcase_51 AC 12 ms
6,608 KB
testcase_52 AC 9 ms
5,316 KB
testcase_53 AC 11 ms
5,748 KB
testcase_54 AC 8 ms
5,060 KB
testcase_55 AC 7 ms
4,884 KB
testcase_56 AC 12 ms
6,276 KB
testcase_57 AC 16 ms
5,744 KB
testcase_58 AC 16 ms
5,744 KB
testcase_59 AC 16 ms
5,744 KB
testcase_60 AC 18 ms
5,708 KB
testcase_61 AC 18 ms
5,708 KB
testcase_62 AC 18 ms
5,708 KB
testcase_63 AC 79 ms
30,748 KB
testcase_64 AC 11 ms
7,356 KB
testcase_65 AC 76 ms
30,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct RollingHash {
  static constexpr long long m31 = (1LL << 31) - 1;
  static int mod_m31(long long a) {
    a = (a >> 31) + (a & m31);
    return a >= m31 ? a - m31 : a;
  }
  static long long base[2];
  static vector<int> powb[2];
  vector<int> h[2];
  RollingHash(const string& s) : h{{0}, {0}} {
    for (int k : {0, 1}) {
      for (char c : s) h[k].push_back(mod_m31(h[k].back() * base[k] + c));
      while (powb[k].size() <= s.size())
        powb[k].push_back(mod_m31(powb[k].back() * base[k]));
    }
  }
  int get(int l, int r, int k) const {
    return mod_m31(h[k][r] + (m31 - h[k][l]) * powb[k][r - l]);
  }
  auto get(int l, int r) const { return make_pair(get(l, r, 0), get(l, r, 1)); }
};
long long RollingHash::base[2] = {1095803926, 1410736294};
vector<int> RollingHash::powb[2] = {{1}, {1}};

template <class T, T Op(T, T)> struct SparseTable {
  vector< vector<T> > t;
  SparseTable(const vector<T>& v) : t{v} {
    for (int k = 1, n = v.size(); 1 << k <= n; ++k) {
      t.emplace_back(n - (1 << k) + 1);
      for (int i = 0; i + (1 << k) <= n; ++i)
        t[k][i] = Op(t[k - 1][i], t[k - 1][i + (1 << (k - 1))]);
    }
  }
  T fold(int l, int r) const {
    assert(l < r);
    int k = __lg(r - l);
    return Op(t[k][l], t[k][r - (1 << k)]);
  }
};
int minimum(int a, int b) { return b < a ? b : a; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> s(n);
  vector<RollingHash> rh;
  for (auto&& e : s) {
    cin >> e;
    rh.emplace_back(e);
  }
  vector<int> v(n - 1);
  for (int i = 0; i < n - 1; ++i) {
    int ok = 0, ng = min(s[i].size(), s[i + 1].size()) + 1;
    while (ng - ok > 1) {
      int mid = (ok + ng) / 2;
      (rh[i].get(0, mid) == rh[i + 1].get(0, mid) ? ok : ng) = mid;
    }
    v[i] = ok;
  }
  long long res = 0;
  SparseTable<int, minimum> st(v);
  for (int i = 0; i < n; ++i) {
    int m = s[i].size();
    for (int j = 0; j < m; ++j) {
      int ok = i, ng = n;
      while (ng - ok > 1) {
        int mid = (ok + ng) / 2;
        (st.fold(i, mid) > j ? ok : ng) = mid;
      }
      ok = ok - i + 1;
      res += (long long)ok * (ok + 1) / 2;
    }
  }
  cout << res << '\n';
}
0