結果

問題 No.962 LCPs
ユーザー parukiparuki
提出日時 2020-01-28 17:44:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 2,243 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 177,164 KB
実行使用メモリ 31,376 KB
最終ジャッジ日時 2023-10-13 20:48:05
合計ジャッジ時間 7,712 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 37 ms
4,348 KB
testcase_18 AC 22 ms
4,348 KB
testcase_19 AC 22 ms
4,348 KB
testcase_20 AC 17 ms
4,348 KB
testcase_21 AC 18 ms
4,352 KB
testcase_22 AC 14 ms
4,352 KB
testcase_23 AC 14 ms
4,348 KB
testcase_24 AC 13 ms
4,352 KB
testcase_25 AC 13 ms
4,348 KB
testcase_26 AC 12 ms
4,352 KB
testcase_27 AC 16 ms
4,356 KB
testcase_28 AC 13 ms
4,352 KB
testcase_29 AC 12 ms
4,348 KB
testcase_30 AC 15 ms
4,356 KB
testcase_31 AC 17 ms
4,352 KB
testcase_32 AC 11 ms
4,432 KB
testcase_33 AC 22 ms
4,348 KB
testcase_34 AC 12 ms
4,348 KB
testcase_35 AC 12 ms
4,352 KB
testcase_36 AC 13 ms
4,368 KB
testcase_37 AC 77 ms
10,508 KB
testcase_38 AC 73 ms
10,748 KB
testcase_39 AC 73 ms
10,432 KB
testcase_40 AC 74 ms
10,492 KB
testcase_41 AC 73 ms
10,496 KB
testcase_42 AC 66 ms
10,416 KB
testcase_43 AC 73 ms
10,496 KB
testcase_44 AC 72 ms
10,460 KB
testcase_45 AC 72 ms
10,432 KB
testcase_46 AC 73 ms
10,536 KB
testcase_47 AC 170 ms
21,944 KB
testcase_48 AC 146 ms
18,356 KB
testcase_49 AC 203 ms
23,832 KB
testcase_50 AC 212 ms
24,716 KB
testcase_51 AC 172 ms
23,524 KB
testcase_52 AC 112 ms
17,820 KB
testcase_53 AC 184 ms
21,432 KB
testcase_54 AC 147 ms
18,668 KB
testcase_55 AC 91 ms
15,020 KB
testcase_56 AC 218 ms
24,844 KB
testcase_57 AC 8 ms
4,464 KB
testcase_58 AC 8 ms
4,400 KB
testcase_59 AC 8 ms
4,568 KB
testcase_60 AC 15 ms
4,348 KB
testcase_61 AC 14 ms
4,348 KB
testcase_62 AC 14 ms
4,348 KB
testcase_63 AC 29 ms
4,352 KB
testcase_64 AC 228 ms
31,376 KB
testcase_65 AC 30 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

struct RollingHash {
    static const int C = 31, C2 = 29, P = 1000000007, P2 = 1000000009;
    int _n;
    vector<long long> pw, pw2, hs, hs2;

    RollingHash() {}
    RollingHash(const string &st)
        :_n((int)st.size()), pw(_n + 1), pw2(_n + 1), hs(_n + 1), hs2(_n + 1)
    {
        pw[0] = pw2[0] = 1;
        rep(i, _n) {
            pw[i + 1] = (pw[i] * C) % P;
            pw2[i + 1] = (pw2[i] * C2) % P2;
            hs[i + 1] = (C*hs[i] + st[i]) % P;
            hs2[i + 1] = (C2*hs2[i] + st[i]) % P2;
        }
    }

    // [l, r)
    pii getHash(int l, int r) {
        int res, res2;
        res = (hs[r] - hs[l] * pw[r - l]) % P;
        res2 = (hs2[r] - hs2[l] * pw2[r - l]) % P2;
        if (res < 0)res += P;
        if (res2 < 0)res2 += P2;
        return mp(res, res2);
    }
};


void solve() {
    int N;
    cin >> N;

    map<pii, vi> indices;
    rep(i, N) {
        string S;
        cin >> S;
        RollingHash rh(S);
        int M = sz(S);
        for (int len = 1; len <= M; ++len) {
            indices[rh.getHash(0, len)].push_back(i);
        }
    }

    ll ans = 0;
    each(idsids, indices) {
        auto &ids = idsids.second;
        int M = sz(ids);
        for (int i = 0; i < M;) {
            int j = i + 1;
            while (j < M && ids[j - 1] + 1 == ids[j]) {
                j++;
            }
            // [i, j)は連続している
            ll m = j - i;
            ans += m * (m + 1)*(m + 1) / 2 - m * (m + 1)*(2 * m + 1) / 6;
            i = j;
        }
    }
    cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0