結果

問題 No.2162 Copy and Paste 2
ユーザー 👑 ygussanyygussany
提出日時 2022-03-07 21:51:48
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 426 ms / 7,000 ms
コード長 1,735 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 33,496 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2024-04-24 13:03:20
合計ジャッジ時間 6,454 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 33 ms
10,368 KB
testcase_07 AC 40 ms
10,368 KB
testcase_08 AC 145 ms
10,368 KB
testcase_09 AC 120 ms
10,496 KB
testcase_10 AC 123 ms
10,496 KB
testcase_11 AC 178 ms
10,496 KB
testcase_12 AC 194 ms
10,496 KB
testcase_13 AC 262 ms
10,496 KB
testcase_14 AC 164 ms
10,368 KB
testcase_15 AC 157 ms
10,368 KB
testcase_16 AC 159 ms
10,368 KB
testcase_17 AC 301 ms
10,368 KB
testcase_18 AC 376 ms
10,368 KB
testcase_19 AC 362 ms
10,368 KB
testcase_20 AC 330 ms
10,496 KB
testcase_21 AC 180 ms
10,368 KB
testcase_22 AC 179 ms
10,496 KB
testcase_23 AC 15 ms
10,368 KB
testcase_24 AC 413 ms
10,368 KB
testcase_25 AC 426 ms
10,368 KB
testcase_26 AC 390 ms
10,368 KB
testcase_27 AC 426 ms
10,368 KB
testcase_28 AC 411 ms
10,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

void Z_algorithm(char S[], int Z[])
{
	int i, j, k, l;
	for (l = 0; S[l] != 0; l++);
	Z[0] = l;
	for (i = 1, j = 0; i < l; i++) {
		for (; i + j < l && S[i+j] == S[j]; j++);
		Z[i] = j;
		if (j == 0) continue;
		
		for (k = 1; k < j && k + Z[k] < j; k++) Z[i+k] = Z[k];
		i += k - 1;
		j -= k;
	}
}

const int inf = -1;
int leaf[200001];

typedef struct {
	int left, right, max;
} seg_node;

void init_node(seg_node v[], int k, int l, int r)
{
	v[k].left = l;
	v[k].right = r;
	v[k].max = inf;
	if (l < r) {
		init_node(v, k << 1, l, (l + r) / 2);
		init_node(v, (k << 1) ^ 1, (l + r) / 2 + 1, r);
	} else leaf[l] = k;
}

void update_node(seg_node v[], int k, int x)
{
	int i, j = leaf[k];
	v[j].max = x;
	for (i = j >> 1; i > 0; j = i, i >>= 1) v[i].max = (v[j].max > v[j^1].max)? v[j].max: v[j^1].max;
}

int BS_left(seg_node v[], int k, int l, int r, int x)
{
	int tmp;
	if (v[k].max < x || v[k].right < l || v[k].left > r) return r + 1;
	else if (v[k].left == v[k].right) return v[k].left;
	else {
		tmp = BS_left(v, k << 1, l, r, x);
		if (tmp <= r) return tmp;
		else return BS_left(v, (k << 1) ^ 1, l, r, x);
	}
}

void chmin(int* a, int b)
{
	if (*a > b) *a = b;
}

int main()
{
	int i, N;
	char S[200001];
	scanf("%s", S);
	for (N = 0; S[N] != 0; N++);
	
	int Z[200001];
	seg_node v[524288];
	Z_algorithm(S, Z);
	init_node(v, 1, 0, N - 1);
	for (i = 0; i < N; i++) update_node(v, i, Z[i]);
	
	int j, k, tmp = 0, dp[200001] = {};
	for (i = 2; i < N; i++) {
		chmin(&tmp, dp[i]);
		for (j = i, k = tmp + 1; 1; ) {
			j = BS_left(v, 1, j, N - 1, i);
			if (j == N) break;
			else j += i;
			k -= i - 1;
			chmin(&(dp[j]), k);
		}
	}
	chmin(&tmp, dp[N]);
	printf("%d\n", N + tmp);
	fflush(stdout);
	return 0;
}
0