結果

問題 No.263 Common Palindromes Extra
ユーザー bal4ubal4u
提出日時 2019-08-16 23:48:21
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 2,269 bytes
コンパイル時間 1,287 ms
コンパイル使用メモリ 31,176 KB
実行使用メモリ 76,048 KB
最終ジャッジ日時 2023-10-24 08:14:31
合計ジャッジ時間 2,636 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,884 KB
testcase_01 AC 2 ms
5,836 KB
testcase_02 AC 2 ms
5,836 KB
testcase_03 AC 6 ms
7,892 KB
testcase_04 AC 22 ms
7,904 KB
testcase_05 AC 19 ms
7,892 KB
testcase_06 AC 4 ms
7,888 KB
testcase_07 AC 36 ms
45,416 KB
testcase_08 AC 32 ms
45,416 KB
testcase_09 AC 57 ms
76,048 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 17 ms
5,836 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'ins':
main.c:11:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   11 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:19:16: note: in expansion of macro 'gc'
   19 |         do c = gc(), f[c]++, *s++ = c;
      |                ^~

ソースコード

diff #

// 263 Common Palindromes Extra
// AOJ 2292 Common Palindromes
// 2019.8.16 bal4u

#include <stdio.h>
#include <string.h>

typedef long long ll;

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int ins(int *f, char *s)  // 文字列の入力 スペース以下の文字で入力終了
{
	char *p = s; int c;
	do c = gc(), f[c]++, *s++ = c;
	while (c > ' ');
	*--s = 0;
	return s - p;
}

#define MAX 1000010
//// 回文木の構築
typedef struct { int par, son[30], suffix_link, len, cnt; } NODE;
NODE node[MAX]; int sz = 2;
int last;
char S[MAX]; int W;  // 文字列

void init_palindromic_tree() { // 回文木の初期化
	node[0].suffix_link = -1, node[0].len = -1;
}

void add_palindromic_tree(int idx) { // 文字を回文木に追加。文字位置 S[idx]
	char c = S[idx]; int a = S[idx] - '>';
	int t = last;

	while (1) {
		if (idx-1-node[t].len >= 0 && S[idx-1-node[t].len] == c) break;
		t = node[t].suffix_link;
	}

	if (node[t].son[a] > 0) {
		last = node[t].son[a], node[last].cnt++;
		return;
	}

	last = sz++;
	node[t].son[a] = last;

	node[last].len = node[t].len + 2, node[last].par = t, node[last].cnt = 1;

	if (node[last].len == 1) node[last].suffix_link = 1;
	else while (1) {
		t = node[t].suffix_link;
		if (idx-1-node[t].len >= 0 && S[idx-1-node[t].len] == c) {
			node[last].suffix_link = node[t].son[a];
			break;
		}
	}
}

char *ss; int ws;
char *tt; int wt; 
int f[2][MAX];
int as[128], at[128];

int main()
{
	int i, t; ll ans;
	
	ws = ins(as, ss = S);
	S[ws] = '>', S[ws+1] = '@';
	wt = ins(at, tt = ss + ws + 2);
	W = ws + wt + 2;
	
	int u, v; t = 0;
	for (i = 'A'; i <= 'Z'; i++) {
		if (as[i]) u++;
		if (at[i]) v++;
		if (as[i] & at[i]) t = 1;
	}
	if (t == 0) { puts("0"); return 0; }
	if (u == 1 && v == 1 && ws == wt) {
		printf("%lld\n", (ll)ws*(ws+1)*(2*ws+1)/6);
		return 0;
	}
	
	init_palindromic_tree();
	for (i = 0; i < ws; i++) {
		add_palindromic_tree(i);
		f[0][last]++;
	}
	add_palindromic_tree(i++), add_palindromic_tree(i++);
	for ( ; i < W; i++) {
		add_palindromic_tree(i);
		f[1][last]++;
	}

	ans = 0;
	for (i = sz-1; i > 1; i--) {
		ans += (ll)f[0][i] * f[1][i];
		f[0][node[i].suffix_link] += f[0][i];
		f[1][node[i].suffix_link] += f[1][i];
	}
	printf("%lld\n",ans);
    return 0;
}
0