結果

問題 No.515 典型LCP
ユーザー kimiyukikimiyuki
提出日時 2017-05-09 23:24:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,616 bytes
コンパイル時間 1,251 ms
コンパイル使用メモリ 108,144 KB
実行使用メモリ 155,360 KB
最終ジャッジ日時 2023-10-12 21:52:37
合計ジャッジ時間 7,587 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 202 ms
27,552 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 189 ms
27,744 KB
testcase_06 AC 193 ms
27,844 KB
testcase_07 AC 191 ms
27,824 KB
testcase_08 AC 195 ms
27,840 KB
testcase_09 AC 192 ms
28,092 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 193 ms
27,588 KB
testcase_14 AC 19 ms
4,832 KB
testcase_15 AC 178 ms
27,504 KB
testcase_16 AC 179 ms
27,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <numeric>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using ll = long long;
using namespace std;

int compute_lcp(string const & a, string const & b) {
    int i = 0;
    while (i < a.length() and i < b.length() and a[i] == b[i]) ++ i;
    return i;
}

template <class Monoid>
struct sparse_table {
    typedef typename Monoid::type T;
    vector<vector<T> > table;
    Monoid mon;
    sparse_table(vector<T> const & init, Monoid const & a_mon = Monoid())
            : mon(a_mon) {
        int n = init.size();
        int log_n = sqrt(n) + 1;
        table.resize(log_n, vector<T>(n, mon.unit));
        table[0] = init;
        for (int k = 0; k < log_n-1; ++ k) {
            for (int i = 0; i < n; ++ i) {
                table[k+1][i] = mon.append(table[k][i], i + (1<<k) < n ? table[k][i + (1<<k)] : mon.unit);
            }
        }
    }
    T operator () (int l, int r) {
        assert (0 <= l and l <= r and r <= table[0].size());
        if (l == r) return mon.unit;
        int k = log2(r - l);
        return mon.append(table[k][l], table[k][r - (1<<k)]);
    }
};
struct min_t {
    typedef int type;
    const int unit = 1e9+7;
    int append(int a, int b) { return min(a, b); }
};

int main() {
    // input
    int n; cin >> n;
    vector<string> s(n); repeat (i,n) cin >> s[i];
    int m; ll x, d; cin >> m >> x >> d;
    // generate queries
    vector<int> qi(m);
    vector<int> qj(m);
    repeat (k,m) {
        qi[k] = x / (n - 1);
        qj[k] = x % (n - 1);
        if (qi[k] > qj[k]) {
            swap(qi[k], qj[k]);
        } else {
            qj[k] += 1;
        }
        x = (x + d) % (n * (n - 1));
    }
    // construct a sparse table
    vector<int> rank(n);
    whole(iota, rank, 0);
    whole(sort, rank, [&](int i, int j) { return s[i] < s[j]; });
    vector<int> lcp_0(n-1);
    repeat (i,n-1) lcp_0[i] = compute_lcp(s[rank[i]], s[rank[i+1]]);
    sparse_table<min_t> table(lcp_0);
    vector<int> rank_of(n);
    repeat (i,n) rank_of[rank[i]] = i;
    // compute
    ll result = 0;
    repeat (q,m) {
        int l = rank_of[qi[q]];
        int r = rank_of[qj[q]];
        if (l > r) swap(l, r);
        result += table(l, r);
    }
    // output
    cout << result << endl;
    return 0;
}
0