結果

問題 No.515 典型LCP
ユーザー PachicobuePachicobue
提出日時 2017-08-26 19:51:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 295 ms / 1,000 ms
コード長 3,092 bytes
コンパイル時間 2,159 ms
コンパイル使用メモリ 183,104 KB
実行使用メモリ 18,528 KB
最終ジャッジ日時 2024-04-23 17:15:55
合計ジャッジ時間 5,118 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 295 ms
18,436 KB
testcase_01 AC 292 ms
18,528 KB
testcase_02 AC 112 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 75 ms
6,940 KB
testcase_06 AC 73 ms
6,940 KB
testcase_07 AC 74 ms
6,940 KB
testcase_08 AC 96 ms
6,944 KB
testcase_09 AC 75 ms
6,940 KB
testcase_10 AC 81 ms
6,940 KB
testcase_11 AC 85 ms
6,940 KB
testcase_12 AC 82 ms
6,940 KB
testcase_13 AC 77 ms
6,944 KB
testcase_14 AC 5 ms
6,940 KB
testcase_15 AC 73 ms
6,940 KB
testcase_16 AC 73 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// {{{ Templates
#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

// }}}

struct Min {
    using T = int;
    T operator()(const T& a, const T& b) const
    {
        return min(a, b);
    }
};
template <typename Base>
class SparseTable
{
public:
    using T = typename Base::T;
    using SemiLattice = Base;
    SparseTable(const vector<T>& val) : size(val.size()), lg2(size + 1, 0)
    {
        for (int i = 2; i <= size; i++) {
            lg2[i] = lg2[i / 2] + 1;
        }
        table.resize(size, vector<T>(lg2[size] + 1));
        for (int i = 0; i < size; i++) {
            table[i][0] = val[i];
        }
        for (int j = 0; j < lg2[size]; j++) {
            const int w = 1 << j;
            for (int i = 0; i <= size - (w << 1); i++) {
                T tl = table[i][j], tr = table[i + w][j];
                table[i][j + 1] = op(tl, tr);
            }
        }
    }

    T accumulate(const int l, const int r) const
    {
        assert(0 <= l and l < r and r <= size);
        const int j = lg2[r - l];
        return op(table[l][j], table[r - (1 << j)][j]);
    }


private:
    const int size;
    vector<int> lg2;
    vector<vector<T>> table;
    const SemiLattice op{};
};

inline int common(const string& s1, const string& s2)
{
    int c = 0;
    for (int i = 0; i < min(s1.size(), s2.size()); i++) {
        if (s1[i] != s2[i]) {
            break;
        }
        c++;
    }
    return c;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    vector<pair<string, int>> S(N);
    for (int i = 0; i < N; i++) {
        cin >> S[i].first;
        S[i].second = i;
    }
    sort(S.begin(), S.end());
    vector<int> convert(N, 0);
    for (int i = 0; i < N; i++) {
        convert[S[i].second] = i;
    }

    vector<int> lcp(N - 1, 0);
    for (int i = 0; i < N - 1; i++) {
        lcp[i] = common(S[i].first, S[i + 1].first);
    }

    SparseTable<Min> table{lcp};

    int M;
    cin >> M;
    ll x, d;
    cin >> x >> d;
    ll sum = 0;
    for (int q = 0; q < M; q++) {
        ll i = x / (N - 1) + 1;
        ll j = x % (N - 1) + 1;
        if (i > j) {
            swap(i, j);
        } else {
            j++;
        }
        i--;
        j--;
        int ind1 = convert[i];
        int ind2 = convert[j];
        if (ind1 > ind2) {
            swap(ind1, ind2);
        }
        sum += table.accumulate(ind1, ind2);
        x = (x + d) % (N * (N - 1));
    }
    cout << sum << endl;
    return 0;
}
0