結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,992 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
7,984 KB
testcase_03 AC 2 ms
7,944 KB
testcase_04 AC 2 ms
8,096 KB
testcase_05 AC 3 ms
8,012 KB
testcase_06 WA -
testcase_07 AC 2 ms
7,992 KB
testcase_08 AC 3 ms
7,960 KB
testcase_09 AC 2 ms
7,996 KB
testcase_10 AC 2 ms
7,972 KB
testcase_11 AC 2 ms
8,076 KB
testcase_12 AC 3 ms
7,996 KB
testcase_13 AC 2 ms
7,884 KB
testcase_14 AC 2 ms
10,048 KB
testcase_15 AC 2 ms
7,896 KB
testcase_16 AC 2 ms
7,952 KB
testcase_17 AC 2 ms
8,000 KB
testcase_18 AC 2 ms
7,976 KB
testcase_19 AC 2 ms
8,020 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 10 ms
9,620 KB
testcase_22 AC 8 ms
9,052 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 7 ms
8,424 KB
testcase_26 AC 2 ms
7,944 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 7 ms
9,072 KB
testcase_31 AC 9 ms
8,976 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 5 ms
9,160 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 10 ms
9,444 KB
testcase_43 AC 8 ms
8,836 KB
testcase_44 AC 8 ms
8,952 KB
testcase_45 AC 6 ms
8,320 KB
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 10 ms
9,824 KB
testcase_49 WA -
testcase_50 AC 7 ms
9,120 KB
testcase_51 AC 8 ms
9,280 KB
testcase_52 AC 6 ms
8,736 KB
testcase_53 AC 3 ms
4,376 KB
testcase_54 AC 2 ms
4,384 KB
testcase_55 AC 5 ms
8,504 KB
testcase_56 AC 2 ms
4,376 KB
testcase_57 AC 6 ms
8,912 KB
testcase_58 AC 8 ms
9,080 KB
testcase_59 AC 7 ms
9,184 KB
testcase_60 AC 5 ms
8,488 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 8 ms
8,632 KB
testcase_63 AC 8 ms
10,168 KB
testcase_64 WA -
権限があれば一括ダウンロードができます

ソースコード

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, k, l, r;
	for (i = 1, rad[0] = 0, rad[1] = 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;
	char S[500001], T[500001];
	scanf("%d %d", &N, &M);
	scanf("%s", S);
	scanf("%s", T);
	if (M % 2 != 0) {
		if (lex_smaller(S, T) != 0) printf("-1\n");
		else printf("0\n");
		fflush(stdout);
		return 0;
	}
	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 == 0 && j == 0) {
		printf("-1\n");
		fflush(stdout);
		return 0;
	}
	
	int rad[500001];
	Manacher_even(M, T, rad);
	
	int dist[500001] = {}, q[1000001], head = 500000, tail = 500000;
	if (i < j) i = j;
	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