結果

問題 No.515 典型LCP
ユーザー nebukuro09nebukuro09
提出日時 2017-05-11 11:13:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 262 ms / 1,000 ms
コード長 1,808 bytes
コンパイル時間 1,896 ms
コンパイル使用メモリ 158,280 KB
実行使用メモリ 42,312 KB
最終ジャッジ日時 2023-10-13 11:00:31
合計ジャッジ時間 5,387 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 262 ms
42,312 KB
testcase_01 AC 261 ms
41,644 KB
testcase_02 AC 152 ms
28,596 KB
testcase_03 AC 3 ms
8,036 KB
testcase_04 AC 3 ms
7,980 KB
testcase_05 AC 105 ms
28,604 KB
testcase_06 AC 106 ms
28,480 KB
testcase_07 AC 104 ms
28,488 KB
testcase_08 AC 134 ms
28,480 KB
testcase_09 AC 110 ms
28,864 KB
testcase_10 AC 112 ms
28,448 KB
testcase_11 AC 110 ms
28,556 KB
testcase_12 AC 110 ms
28,460 KB
testcase_13 AC 106 ms
28,456 KB
testcase_14 AC 20 ms
10,924 KB
testcase_15 AC 98 ms
28,628 KB
testcase_16 AC 100 ms
28,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);i++)
#define REP2(i,m,n) for (int i=m;i<(n);i++)

const int NMAX = 101010;
const int MMAX = 3010101;
const int LOGNMAX = 25;
int N, M;
long x, d;
vector<pair<string, int> > S;
int A[NMAX], I[MMAX], J[MMAX], rev[NMAX], D[NMAX];
int dp[NMAX][LOGNMAX];


int getmin(int i, int j) {
    int m = j - i + 1;
    int k = 0;
    while (m >>= 1) k += 1;
    int h = A[dp[i][k]] <= A[dp[j-(1<<k)+1][k]] ? dp[i][k] : dp[j-(1<<k)+1][k];
    return A[h];
}    


int main() {
    cin >> N;
    REP(i, N) {
        string s;
        cin >> s;
        S.push_back(make_pair(s, i));
    }
    cin >> M >> x >> d;

    REP(k, M) {
        I[k] = (int)((x / (N - 1)) + 1);
        J[k] = (int)((x % (N - 1)) + 1);
        if (I[k] > J[k]) swap(I[k], J[k]);
        else J[k] += 1;
        x = (x + d) % ((long long)N * ((long long)N - 1));
    }

    sort(S.begin(), S.end());
    REP(i, N) rev[S[i].second] = i;

    memset(A, 0, sizeof(A));
    REP(i, N-1) REP(j, min(S[i].first.length(), S[i+1].first.length())) {
        if (S[i].first[j] == S[i+1].first[j]) A[i]++;
        else break;
    }

    REP(i, N-1) dp[i][0] = i;
    for (int j = 1; 1 << j <= N-1; j++)
        for (int i = 0; i + (1 << j) - 1 < N-1; i++)
            if (A[dp[i][j - 1]] < A[dp[i + (1 << (j - 1))][j - 1]])
                dp[i][j] = dp[i][j - 1];
            else
                dp[i][j] = dp[i + (1 << (j - 1))][j - 1];

    //long[int][int] mem;
    long long ans = 0;
    REP(k, M) {
        int i = rev[I[k] - 1];
        int j = rev[J[k] - 1];
        if (i > j) swap(i, j);
        //if (i in mem && j in mem[i]) ans += mem[i][j];
        //else ans += mem[i][j] = st.getmin(i, j-1);
        ans += getmin(i, j-1);
    }

    cout << ans << endl;
    
}
0