結果

問題 No.515 典型LCP
ユーザー pekempeypekempey
提出日時 2017-05-06 04:38:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 1,000 ms
コード長 2,209 bytes
コンパイル時間 1,585 ms
コンパイル使用メモリ 171,144 KB
実行使用メモリ 14,388 KB
最終ジャッジ日時 2023-10-12 13:32:55
合計ジャッジ時間 3,112 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
14,380 KB
testcase_01 AC 75 ms
14,388 KB
testcase_02 AC 43 ms
13,464 KB
testcase_03 AC 6 ms
13,252 KB
testcase_04 AC 5 ms
13,340 KB
testcase_05 AC 22 ms
13,380 KB
testcase_06 AC 23 ms
13,468 KB
testcase_07 AC 24 ms
13,404 KB
testcase_08 AC 28 ms
13,296 KB
testcase_09 AC 23 ms
13,440 KB
testcase_10 AC 27 ms
13,464 KB
testcase_11 AC 26 ms
13,396 KB
testcase_12 AC 27 ms
13,616 KB
testcase_13 AC 23 ms
13,396 KB
testcase_14 AC 10 ms
13,580 KB
testcase_15 AC 24 ms
13,376 KB
testcase_16 AC 22 ms
13,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

long long sbuf[100100];
long long *s[100000];
int len[100000];
int slen[100000];
char buf[800100];
int rmq[18][100000];
int lcp[100000];

int main() {
	long long n;
	scanf("%lld", &n);
	long long *ptr = sbuf;
	for (int i = 0; i < n; i++) {
		scanf("%s", buf);
		len[i] = strlen(buf);
		slen[i] = (len[i] + 11) / 12;
		s[i] = ptr;
		for (int j = 0; j < len[i]; j += 12) {
			long long val = 0;
			for (int k = 0; k < 12; k++) {
				if (buf[j + k] == 0) {
					break;
				}
				val |= (long long)(buf[j + k] - 'a' + 1) << (5 * (11 - k));
			}
			*ptr = val;
			ptr++;
		}
	}
	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) {
		int k;
		for (k = 0; k < std::min(slen[i], slen[j]); k++) {
			if (s[i][k] != s[j][k]) {
				return s[i][k] < s[j][k];
			}
		}
		return k != slen[j];
	});
    std::vector<int> iperm(n);
    for (int i = 0; i < n; i++) {
        iperm[perm[i]] = i;
    }

	for (int i = 0; i < n - 1; i++) {
		const int l = perm[i];
		const int r = perm[i + 1];
		const int nl = slen[l];
		const int nr = slen[r];
        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 {
			lcp[i] = h * 12 + 11 - std::__lg(s[l][h] ^ s[r][h]) / 5;
		}
	}

	for (int i = 0; i < 100000; i++) {
		rmq[0][i] = lcp[i];
	}
	for (int i = 0; i < 17; i++) {
		for (int j = 0; j + (1 << i) < 100000; 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);
	int di = d % (n - 1);
	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;
		}
        i = iperm[i];
        j = iperm[j];
        if (i > j) {
            std::swap(i, j);
        }
        const int k = std::__lg(j - i);
        ans += std::min(rmq[k][i], rmq[k][j - (1 << k)]);
	}

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