結果

問題 No.515 典型LCP
ユーザー pekempeypekempey
提出日時 2017-05-06 03:24:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 1,000 ms
コード長 2,263 bytes
コンパイル時間 1,940 ms
コンパイル使用メモリ 181,532 KB
実行使用メモリ 16,308 KB
最終ジャッジ日時 2023-10-12 13:15:52
合計ジャッジ時間 4,076 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 164 ms
16,308 KB
testcase_01 AC 155 ms
16,232 KB
testcase_02 AC 84 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 49 ms
4,348 KB
testcase_06 AC 49 ms
4,352 KB
testcase_07 AC 47 ms
4,348 KB
testcase_08 AC 64 ms
4,348 KB
testcase_09 AC 49 ms
4,348 KB
testcase_10 AC 50 ms
4,352 KB
testcase_11 AC 52 ms
4,348 KB
testcase_12 AC 51 ms
4,352 KB
testcase_13 AC 50 ms
4,352 KB
testcase_14 AC 6 ms
4,352 KB
testcase_15 AC 48 ms
4,352 KB
testcase_16 AC 48 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int main() {
	int n;
	scanf("%d", &n);
	std::vector<std::vector<long long>> s(n);
	std::vector<int> len(n); 
    int total = 0;
	for (int i = 0; i < n; i++) {
		static char buf[800100];
		scanf("%s", buf);
		len[i] = strlen(buf);
        total += len[i];
        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<char> 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);
	rmq[0] = lcp;
	for (int i = 0; i + 1 < rmq.size(); i++) {
		rmq[i + 1].resize(rmq[i].size() - (1 << i));
		for (int j = 0; j < rmq[i + 1].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);
    const long long N = 1LL * n * (n - 1);
	while (m--) {
		int i = x / (n - 1);
		int j = x % (n - 1);
		if (i > j) {
			std::swap(i, j);
		} else {
			j++;
		}
        x += d;
        if (x >= N) {
            x -= N;
        }
        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)]);
	}

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