結果

問題 No.515 典型LCP
ユーザー pekempeypekempey
提出日時 2017-05-06 02:19:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 1,000 ms
コード長 2,351 bytes
コンパイル時間 809 ms
コンパイル使用メモリ 73,412 KB
実行使用メモリ 17,104 KB
最終ジャッジ日時 2023-10-12 11:25:41
合計ジャッジ時間 3,456 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 227 ms
17,056 KB
testcase_01 AC 218 ms
17,104 KB
testcase_02 AC 123 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 78 ms
4,348 KB
testcase_06 AC 79 ms
4,352 KB
testcase_07 AC 78 ms
4,348 KB
testcase_08 AC 94 ms
4,352 KB
testcase_09 AC 79 ms
4,348 KB
testcase_10 AC 82 ms
4,352 KB
testcase_11 AC 82 ms
4,352 KB
testcase_12 AC 82 ms
4,352 KB
testcase_13 AC 79 ms
4,352 KB
testcase_14 AC 6 ms
4,352 KB
testcase_15 AC 78 ms
4,348 KB
testcase_16 AC 79 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>

#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

int main() {
	int n;
	scanf("%d", &n);
	std::vector<std::vector<long long>> s(n);
	std::vector<int> len(n);
	for (int i = 0; i < n; i++) {
		static char buf[800100];
		char c;
		while ((c = getchar()) < 'a');
		buf[0] = c;
		int l = 1;
		while ((c = getchar()) >= 'a') {
			buf[l++] = c;
		}
		len[i] = l;
		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;
				if (buf[j + k] != 0) {
					val |= buf[j + k] - 'a' + 1;
				}
			}
			s[i][j / 12] = val;
		}
	}
	std::vector<int> perm(n);
	for (int i = 0; i < n; i++) {
		perm[i] = i;
	}
	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<int> 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();
		int h = 0;
		while (h < std::min(nl, nr) && s[l][h] == s[r][h]) {
			h++;
		}
		if (h == nl) {
			lcp[i] = len[l];
		} else if (h == nr) {
			lcp[i] = len[r];
		} else {
			long long v = s[l][h] ^ s[r][h];
			int j;
			for (j = 0; j < 12; j++) {
				if (v >> (5 * (11 - j)) & 0x1f) {
					break;
				}
			}
			lcp[i] = h * 12 + j;
		}
	}

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

	std::vector<std::vector<int>> rmq(lg[lcp.size()] + 1, std::vector<int>(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);
	while (m--) {
		int i = x / (n - 1);
		int j = x % (n - 1);
		if (i > j) {
			std::swap(i, j);
		} else {
			j++;
		}

		i = iperm[i];
		j = iperm[j];
		if (i > j) {
			std::swap(i, j);
		}
		const int k = lg[j - i];
		ans += std::min(rmq[k][i], rmq[k][j - (1 << k)]);

		x = (x + d) % (1LL * n * (n - 1));
	}

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