結果

問題 No.2162 Copy and Paste 2
ユーザー 👑 ygussanyygussany
提出日時 2022-03-06 20:57:37
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 1,997 bytes
コンパイル時間 690 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 472,932 KB
最終ジャッジ日時 2024-04-24 13:04:11
合計ジャッジ時間 43,515 ms
ジャッジサーバーID
(参考情報)
judge3 / 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 0 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 177 ms
124,800 KB
testcase_07 AC 201 ms
135,484 KB
testcase_08 AC 797 ms
264,896 KB
testcase_09 AC 556 ms
205,832 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 288 ms
126,316 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 95 ms
88,808 KB
testcase_24 AC 191 ms
107,264 KB
testcase_25 AC 1,060 ms
208,384 KB
testcase_26 AC 293 ms
122,768 KB
testcase_27 AC 356 ms
132,864 KB
testcase_28 AC 121 ms
93,052 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;
	}
}

#define HASH 9999991
const int H_Mod = HASH;

typedef struct List {
	struct List *next;
	int x, y;
} list;

int hash_func(int x, int y)
{
	return (((long long)x << 20) + y) % H_Mod;
}

#define MAX 25000000
list *hash[HASH] = {}, hd[MAX], *p;

int main()
{
	char S[200001];
	scanf("%s", S);
	
	int i, Z[200001], Z_max[200001];
	Z_algorithm(S, Z);
	for (i = 0; S[i] != 0; i++);
	Z_max[i] = 0;
	for (i--; i >= 0; i--) Z_max[i] = (Z_max[i+1] > Z[i])? Z_max[i+1]: Z[i];
	
	int h, head, tail, x, y, d, l[200001];
	hd[0].x = 0;
	hd[0].y = 0;
	hd[0].next = hash[0];
	hash[0] = &(hd[0]);
	l[0] = 0;
	l[1] = 1;
	for (head = 0, tail = 1, d = 0; head < tail; head++) {
		x = hd[head].x;
		y = hd[head].y;
		if (head == l[d+1]) {
			d++;
			l[d+1] = tail;
		}
		if (S[x] == 0) break;
		h = hash_func(x + 1, y);
		for (p = hash[h]; p != NULL; p = p->next) if (p->x == x + 1 && p->y == y) break;
		if (p == NULL) {
			hd[tail].x = x + 1;
			hd[tail].y = y;
			hd[tail].next = hash[h];
			hash[h] = &(hd[tail++]);
			if (tail == MAX) return -1;
		}
		
		if (Z[x] >= y) {
			h = hash_func(x + y, y);
			for (p = hash[h]; p != NULL; p = p->next) if (p->x == x + y && p->y == y) break;
			if (p == NULL) {
				hd[tail].x = x + y;
				hd[tail].y = y;
				hd[tail].next = hash[h];
				hash[h] = &(hd[tail++]);
			    if (tail == MAX) return -1;
			}
		}
		
		if (Z_max[x] >= x) {
			h = hash_func(x, x);
			for (p = hash[h]; p != NULL; p = p->next) if (p->x == x && p->y == x) break;
			if (p == NULL) {
				hd[tail].x = x;
				hd[tail].y = x;
				hd[tail].next = hash[h];
				hash[h] = &(hd[tail++]);
			    if (tail == MAX) return -1;
			}
		}
	}
	printf("%d\n", d);
	fflush(stdout);
	return 0;
}
0