結果

問題 No.962 LCPs
ユーザー pekempeypekempey
提出日時 2019-12-25 07:06:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,255 bytes
コンパイル時間 1,599 ms
コンパイル使用メモリ 176,348 KB
実行使用メモリ 16,032 KB
最終ジャッジ日時 2023-10-24 16:49:45
合計ジャッジ時間 4,711 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,928 KB
testcase_01 AC 4 ms
7,868 KB
testcase_02 AC 5 ms
7,868 KB
testcase_03 AC 4 ms
7,928 KB
testcase_04 AC 4 ms
7,928 KB
testcase_05 AC 3 ms
7,928 KB
testcase_06 AC 4 ms
7,928 KB
testcase_07 AC 4 ms
7,928 KB
testcase_08 AC 4 ms
7,928 KB
testcase_09 AC 4 ms
7,928 KB
testcase_10 AC 4 ms
7,928 KB
testcase_11 AC 4 ms
7,928 KB
testcase_12 AC 4 ms
7,928 KB
testcase_13 AC 4 ms
7,928 KB
testcase_14 AC 4 ms
7,928 KB
testcase_15 AC 4 ms
7,928 KB
testcase_16 AC 4 ms
7,928 KB
testcase_17 AC 19 ms
16,032 KB
testcase_18 AC 13 ms
12,124 KB
testcase_19 AC 13 ms
12,124 KB
testcase_20 AC 10 ms
10,816 KB
testcase_21 AC 11 ms
10,816 KB
testcase_22 AC 9 ms
10,168 KB
testcase_23 AC 9 ms
10,168 KB
testcase_24 AC 8 ms
9,684 KB
testcase_25 AC 7 ms
9,684 KB
testcase_26 AC 6 ms
9,388 KB
testcase_27 AC 9 ms
10,324 KB
testcase_28 AC 6 ms
9,496 KB
testcase_29 AC 6 ms
9,340 KB
testcase_30 AC 9 ms
9,964 KB
testcase_31 AC 10 ms
10,840 KB
testcase_32 AC 6 ms
9,184 KB
testcase_33 AC 13 ms
11,728 KB
testcase_34 AC 7 ms
9,508 KB
testcase_35 AC 6 ms
9,460 KB
testcase_36 AC 7 ms
9,900 KB
testcase_37 AC 8 ms
9,176 KB
testcase_38 AC 8 ms
9,164 KB
testcase_39 AC 8 ms
9,164 KB
testcase_40 AC 7 ms
9,168 KB
testcase_41 AC 7 ms
9,156 KB
testcase_42 AC 7 ms
9,172 KB
testcase_43 AC 8 ms
9,172 KB
testcase_44 AC 8 ms
9,172 KB
testcase_45 AC 7 ms
9,168 KB
testcase_46 AC 7 ms
9,160 KB
testcase_47 AC 4 ms
8,192 KB
testcase_48 AC 5 ms
8,328 KB
testcase_49 AC 4 ms
8,456 KB
testcase_50 AC 4 ms
8,192 KB
testcase_51 AC 5 ms
8,352 KB
testcase_52 AC 4 ms
8,192 KB
testcase_53 AC 4 ms
8,408 KB
testcase_54 AC 5 ms
8,368 KB
testcase_55 AC 4 ms
8,192 KB
testcase_56 AC 4 ms
8,456 KB
testcase_57 AC 5 ms
8,192 KB
testcase_58 AC 5 ms
8,192 KB
testcase_59 AC 4 ms
8,192 KB
testcase_60 AC 4 ms
8,376 KB
testcase_61 AC 4 ms
8,376 KB
testcase_62 AC 5 ms
8,376 KB
testcase_63 AC 13 ms
13,096 KB
testcase_64 AC 5 ms
8,324 KB
testcase_65 AC 13 ms
13,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct unionfind {
  vector<int> dat;

  unionfind(int n) : dat(n, -1) {}

  int find(int x) {
    if (dat[x] < 0) return x;
    return dat[x] = find(dat[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (dat[x] > dat[y]) swap(x, y);
    dat[x] += dat[y];
    dat[y] = x;
  }

  int size(int x) {
    return -dat[find(x)];
  }
};

int lcp(string s, string t) {
  int k = 0;
  while (k < s.size() and k < t.size() and s[k] == t[k]) k++;
  return k;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N; cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  vector<vector<int>> pos(200001);
  for (int i = 0; i + 1 < N; i++) {
    pos[lcp(S[i], S[i + 1])].push_back(i);
  }
  ll ans = 0;
  ll ima = 0;
  unionfind uf(N);
  auto f = [&](ll n) {
    return n * (n + 1) * (n + 2) / 6;
  };
  for (int i = 200000; i >= 1; i--) {
    for (int j : pos[i]) {
      ima -= f(uf.size(j));
      ima -= f(uf.size(j + 1));
      uf.unite(j, j + 1);
      ima += f(uf.size(j));
    }
    ans += ima;
  }
  for (int i = 0; i < N; i++) {
    ans += S[i].size();
  }
  cout << ans << endl;
}
0