結果

問題 No.1695 Mirror Mirror
ユーザー 👑 ygussanyygussany
提出日時 2021-09-27 18:44:55
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,620 bytes
コンパイル時間 227 ms
コンパイル使用メモリ 30,160 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2023-09-26 16:13:20
合計ジャッジ時間 2,989 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,040 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
7,964 KB
testcase_03 AC 2 ms
7,940 KB
testcase_04 AC 2 ms
8,052 KB
testcase_05 AC 2 ms
7,876 KB
testcase_06 WA -
testcase_07 AC 2 ms
8,068 KB
testcase_08 AC 2 ms
9,976 KB
testcase_09 AC 2 ms
7,972 KB
testcase_10 AC 2 ms
8,000 KB
testcase_11 AC 2 ms
7,972 KB
testcase_12 AC 2 ms
8,056 KB
testcase_13 AC 2 ms
7,972 KB
testcase_14 AC 2 ms
8,024 KB
testcase_15 AC 2 ms
8,040 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 2 ms
7,940 KB
testcase_18 AC 2 ms
8,040 KB
testcase_19 AC 3 ms
9,948 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 11 ms
9,684 KB
testcase_22 AC 9 ms
8,620 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 7 ms
8,456 KB
testcase_26 AC 2 ms
7,968 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 6 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 7 ms
8,544 KB
testcase_31 AC 9 ms
9,644 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 6 ms
8,588 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 10 ms
8,524 KB
testcase_43 AC 8 ms
8,880 KB
testcase_44 AC 8 ms
9,108 KB
testcase_45 AC 7 ms
8,876 KB
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 11 ms
9,680 KB
testcase_49 WA -
testcase_50 AC 6 ms
4,384 KB
testcase_51 AC 7 ms
4,912 KB
testcase_52 AC 5 ms
4,384 KB
testcase_53 AC 5 ms
4,384 KB
testcase_54 AC 5 ms
4,384 KB
testcase_55 AC 6 ms
9,044 KB
testcase_56 AC 5 ms
4,728 KB
testcase_57 AC 6 ms
8,676 KB
testcase_58 AC 8 ms
9,116 KB
testcase_59 AC 7 ms
8,632 KB
testcase_60 AC 6 ms
8,992 KB
testcase_61 AC 3 ms
4,380 KB
testcase_62 AC 8 ms
8,376 KB
testcase_63 AC 8 ms
8,384 KB
testcase_64 AC 2 ms
7,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

int lex_smaller(char s1[], char s2[])
{
	int i;
	for (i = 0; s1[i] != 0 && s2[i] != 0; i++) {
		if (s1[i] < s2[i]) return 1;
		else if (s1[i] > s2[i]) return -1;
	}
	if (s1[i] == s2[i]) return 0;
	else if (s1[i] == 0) return 1;
	else return -1;
}

// 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;
		}
	}
}

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);
	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 (M % 2 != 0 || rad[M/2] != M / 2 || (i == 0 && j == 0)) {
		printf("-1\n");
		fflush(stdout);
		return 0;
	}
	
	int dist[500001] = {}, q[1000001], head = 500000, tail = 500000;
	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];
			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