結果

問題 No.1695 Mirror Mirror
ユーザー 👑 ygussanyygussany
提出日時 2021-09-27 19:20:18
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,591 bytes
コンパイル時間 1,049 ms
コンパイル使用メモリ 30,416 KB
実行使用メモリ 10,792 KB
最終ジャッジ日時 2023-08-28 16:06:27
合計ジャッジ時間 3,968 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,988 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
7,988 KB
testcase_03 AC 2 ms
8,060 KB
testcase_04 AC 2 ms
7,956 KB
testcase_05 AC 2 ms
8,108 KB
testcase_06 AC 2 ms
7,992 KB
testcase_07 AC 2 ms
7,932 KB
testcase_08 AC 2 ms
7,960 KB
testcase_09 AC 2 ms
8,016 KB
testcase_10 AC 2 ms
8,044 KB
testcase_11 AC 2 ms
7,868 KB
testcase_12 AC 2 ms
8,108 KB
testcase_13 AC 2 ms
8,012 KB
testcase_14 AC 2 ms
8,068 KB
testcase_15 AC 2 ms
8,056 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
8,108 KB
testcase_18 AC 2 ms
8,084 KB
testcase_19 AC 2 ms
10,036 KB
testcase_20 AC 2 ms
8,052 KB
testcase_21 AC 11 ms
10,792 KB
testcase_22 AC 9 ms
9,236 KB
testcase_23 AC 9 ms
8,952 KB
testcase_24 AC 7 ms
8,368 KB
testcase_25 AC 6 ms
8,560 KB
testcase_26 AC 2 ms
8,052 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 5 ms
4,412 KB
testcase_29 AC 2 ms
8,064 KB
testcase_30 AC 7 ms
8,680 KB
testcase_31 AC 9 ms
8,964 KB
testcase_32 AC 5 ms
10,220 KB
testcase_33 AC 10 ms
8,676 KB
testcase_34 AC 7 ms
9,556 KB
testcase_35 AC 10 ms
8,892 KB
testcase_36 AC 11 ms
9,136 KB
testcase_37 AC 5 ms
8,504 KB
testcase_38 AC 8 ms
9,132 KB
testcase_39 AC 5 ms
8,532 KB
testcase_40 AC 8 ms
10,188 KB
testcase_41 AC 9 ms
10,196 KB
testcase_42 AC 8 ms
10,024 KB
testcase_43 AC 7 ms
8,832 KB
testcase_44 AC 7 ms
8,844 KB
testcase_45 AC 6 ms
10,140 KB
testcase_46 AC 7 ms
8,880 KB
testcase_47 AC 7 ms
8,640 KB
testcase_48 AC 9 ms
9,964 KB
testcase_49 AC 9 ms
9,244 KB
testcase_50 AC 5 ms
4,720 KB
testcase_51 AC 6 ms
4,924 KB
testcase_52 AC 4 ms
5,244 KB
testcase_53 AC 4 ms
4,384 KB
testcase_54 AC 4 ms
4,392 KB
testcase_55 AC 6 ms
8,560 KB
testcase_56 AC 5 ms
8,536 KB
testcase_57 AC 5 ms
8,696 KB
testcase_58 AC 8 ms
9,068 KB
testcase_59 AC 7 ms
9,032 KB
testcase_60 AC 6 ms
8,428 KB
testcase_61 AC 4 ms
8,204 KB
testcase_62 AC 8 ms
10,228 KB
testcase_63 AC 8 ms
9,152 KB
testcase_64 AC 2 ms
8,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

// rad[i] = max radius of palindrome with center between s[i-1] and s[i]
void Manacher_even(int N, char s[], int rad[])
{
	int i, j, l, r;
	for (i = 1, rad[0] = 0, rad[1] = 0, rad[N] = 0; i < N; i = r) {
		for (l = i - rad[i] - 1, r = i + rad[i]; l >= 0 && r < N && s[l] == s[r]; l--, r++);
		rad[i] = r - i;
		for (l = i - 1, r = i + 1; r - i < rad[i] && l > 0 && r < N; l--, r++) {
			if (l - rad[l] == i - rad[i]) {
				rad[r] = i + rad[i] - r;
				break;
			} else if (l - rad[l] > i - rad[i]) rad[r] = rad[l];
			else rad[r] = i + rad[i] - r;
		}
	}
}

void max_palindrome_naive(int N, char s[], int rad[])
{
	int i, l, r;
	for (i = 1, rad[0] = 0, rad[N] = 0; i < N; i++) {
		for (l = i - 1, r = i; l >= 0 && r < N && s[l] == s[r]; l--, r++);
		rad[i] = r - i;
	}
}

int main()
{
	int i, j, N, M, rad[500001];
	char S[500001], T[500001];
	scanf("%d %d", &N, &M);
	scanf("%s", S);
	scanf("%s", T);
	Manacher_even(M, T, rad);
	if (M % 2 != 0 || rad[M/2] != M / 2) {
		printf("-1\n");
		fflush(stdout);
		return 0;
	}
	
	int dist[500001] = {}, q[2000001], head = 1000000, tail = 1000000;
	for (i = 0; i < N && i < M && S[i] == T[i]; i++);
	for (j = 0; j < N && j < M && S[N-j-1] == T[M-j-1]; j++);
	if (i < j) i = j;
	if (i == M) i--;
	dist[i] = 1;
	q[tail++] = i;
	for (; head < tail; head++) {
		i = q[head];
		if (i > 0 && (dist[i-1] == 0 || dist[i-1] > dist[i])) {
			dist[i-1] = dist[i];
			q[head--] = i - 1;
		}
		j = i + rad[i];
		if (dist[j] == 0) {
			dist[j] = dist[i] + 1;
			q[tail++] = j;
		}
	}
	printf("%d\n", dist[M] - 1);
	fflush(stdout);
	return 0;
}
0