結果

問題 No.515 典型LCP
ユーザー pekempeypekempey
提出日時 2017-05-06 05:57:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 333 ms / 1,000 ms
コード長 1,887 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 44,684 KB
実行使用メモリ 227,952 KB
最終ジャッジ日時 2023-10-12 14:11:23
合計ジャッジ時間 4,138 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 333 ms
195,956 KB
testcase_01 AC 106 ms
138,544 KB
testcase_02 AC 96 ms
138,268 KB
testcase_03 AC 58 ms
138,200 KB
testcase_04 AC 59 ms
138,164 KB
testcase_05 AC 218 ms
225,736 KB
testcase_06 AC 219 ms
225,732 KB
testcase_07 AC 218 ms
225,708 KB
testcase_08 AC 227 ms
225,728 KB
testcase_09 AC 79 ms
138,460 KB
testcase_10 AC 80 ms
138,668 KB
testcase_11 AC 79 ms
138,428 KB
testcase_12 AC 79 ms
138,480 KB
testcase_13 AC 83 ms
140,624 KB
testcase_14 AC 73 ms
142,980 KB
testcase_15 AC 215 ms
227,916 KB
testcase_16 AC 216 ms
227,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <algorithm>
#include <ctime>

constexpr int N = 800001;
char buf[N];
int trie[N][26];
int pos[100000];
bool marked[N];
int eulerID[N];
int now;
int rmq[21][N * 2];
int lg[N * 2];

int readint() {
	int n;
	scanf("%d", &n);
	return n;
}

long long readlong() {
	long long n;
	scanf("%lld", &n);
	return n;
}

void dfs(int u, int depth) {
	eulerID[u] = now;
	rmq[0][now++] = depth;
	for (int i = 0; i < 26; i++) {
		if (!trie[u][i]) {
			continue;
		}
		dfs(trie[u][i], depth + 1);
		rmq[0][now++] = depth;
	}
}

int main() {
	clock_t beg, end;
	beg = clock();
	const int n = readint();
	int cnt = 1;
	for (int i = 0; i < n; i++) {
		scanf("%s", buf);
		int u = 0;
		for (int j = 0; buf[j] != 0; j++) {
			char c = buf[j] - 'a';
			if (!trie[u][c]) {
				trie[u][c] = cnt++;
			}
			u = trie[u][c];
		}
		pos[i] = u;
	}
	dfs(0, 0);
	for (int i = 2; i < N * 2; i++) {
		lg[i] = lg[i / 2] + 1;
	}
	for (int i = 0; i < 20; i++) {
		for (int j = 0; j + (1 << i) < N * 2; j++) {
			rmq[i + 1][j] = std::min(rmq[i][j], rmq[i][j + (1 << i)]);
		}
	}
	end = clock();
	fprintf(stderr, "%.20f\n", (double)(end - beg) / CLOCKS_PER_SEC);
	const int m = readint();
	const long long x = readlong();
	const long long d = readlong();
	int ii = x % (n - 1);
	int jj = x / (n - 1);
	const int di = d % (n - 1);
	const int dj = d / (n - 1);
	long long ans = 0;
	for (int foo = 0; foo < m; foo++) {
		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;
		}
		int l = eulerID[pos[i]];
		int r = eulerID[pos[j]];
		if (l > r) {
			std::swap(l, r);
		}
		r++;
		const int k = lg[r - l];
		ans += std::min(rmq[k][l], rmq[k][r - (1 << k)]);
	}
	printf("%lld\n", ans);
	end = clock();
	fprintf(stderr, "%.20f\n", (double)(end - beg) / CLOCKS_PER_SEC);
}
0