結果

問題 No.515 典型LCP
ユーザー pekempeypekempey
提出日時 2017-05-06 04:08:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 200 ms / 1,000 ms
コード長 2,340 bytes
コンパイル時間 1,870 ms
コンパイル使用メモリ 181,068 KB
実行使用メモリ 13,660 KB
最終ジャッジ日時 2023-10-12 13:31:47
合計ジャッジ時間 4,051 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 200 ms
13,660 KB
testcase_01 AC 194 ms
13,608 KB
testcase_02 AC 57 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 33 ms
4,348 KB
testcase_06 AC 36 ms
4,348 KB
testcase_07 AC 34 ms
4,352 KB
testcase_08 AC 44 ms
4,356 KB
testcase_09 AC 30 ms
4,348 KB
testcase_10 AC 35 ms
4,352 KB
testcase_11 AC 36 ms
4,348 KB
testcase_12 AC 35 ms
4,348 KB
testcase_13 AC 29 ms
4,352 KB
testcase_14 AC 6 ms
4,348 KB
testcase_15 AC 33 ms
4,352 KB
testcase_16 AC 32 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int main() {
	long long n;
	scanf("%lld", &n);
	std::vector<std::vector<long long>> s(n);
	std::vector<int> len(n);
	std::vector<int> perm(n);
	for (int i = 0; i < n; i++) {
		perm[i] = i;
		static char buf[800100];
		scanf("%s", buf);
		len[i] = strlen(buf);
		for (int j = 0; j < 12; j++) {
			buf[len[i] + j] = 'a' - 1;
		}
        s[i].resize((len[i] + 11) / 12);
		for (int j = 0; j < len[i]; j += 12) {
			long long val = 0;
			for (int k = 0; k < 12; k++) {
				val <<= 5;
				val |= buf[j + k] - ('a' - 1);
			}
            s[i][j / 12] = val;
		}
	}
	std::sort(perm.begin(), perm.end(), [&](int i, int j) {
		return s[i] < s[j];
	});
    std::vector<int> iperm(n);
    for (int i = 0; i < n; i++) {
        iperm[perm[i]] = i;
    }

	std::vector<short> lcp(n - 1);
	for (int i = 0; i < n - 1; i++) {
		const int l = perm[i];
		const int r = perm[i + 1];
		const int nl = s[l].size();
		const int nr = s[r].size();
        short h = 0;
		while (h < std::min(nl, nr) && s[l][h] == s[r][h]) {
			h++;
		}
		lcp[i] = h;
	}

	std::vector<char> lg(lcp.size() + 1);
	for (int i = 2; i < lg.size(); i++) {
		lg[i] = lg[i / 2] + 1;
	}

	std::vector<std::vector<short>> rmq(lg[lcp.size()] + 1, std::vector<short>(lcp.size()));
	for (int i = 0; i < lcp.size(); i++) {
		rmq[0][i] = lcp[i];
	}
	for (int i = 0; i + 1 < rmq.size(); i++) {
		for (int j = 0; j + (1 << i) < rmq[i].size(); j++) {
			rmq[i + 1][j] = std::min(rmq[i][j], rmq[i][j + (1 << i)]);
		}
	}

	long long ans = 0;
	int m;
    long long x, d;
	scanf("%d %lld %lld", &m, &x, &d);
	int ii = x % (n - 1);
	int jj = x / (n - 1);
	const int di = d % (n - 1);
	const int dj = d / (n - 1);
	while (m--) {
		int i = jj;
		int j = ii;
		if (i > j) {
			std::swap(i, j);
		} else {
			j++;
		}
		ii += di;
		if (ii >= n - 1) {
			ii -= n - 1;
			jj++;
		}
		jj += dj;
		if (jj >= n) {
			jj -= n;
		}
		const int pi = i;
		const int pj = j;
        i = iperm[i];
        j = iperm[j];
        if (i > j) {
            std::swap(i, j);
        }
        const char k = lg[j - i];
		int l = std::min(rmq[k][i], rmq[k][j - (1 << k)]);
		if (l == s[pi].size()) {
			l = len[pi];
		} else if (l == s[pj].size()) {
			l = len[pj];
		} else {
			long long v = s[pi][l] ^ s[pj][l];
			l = l * 12 + 11 - std::__lg(v) / 5;
		}
		ans += l;
	}

	printf("%lld\n", ans);
}
0