結果
| 問題 |
No.263 Common Palindromes Extra
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-08-16 23:52:39 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 37 ms / 2,000 ms |
| コード長 | 2,276 bytes |
| コンパイル時間 | 523 ms |
| コンパイル使用メモリ | 31,360 KB |
| 実行使用メモリ | 41,024 KB |
| 最終ジャッジ日時 | 2024-09-23 02:37:12 |
| 合計ジャッジ時間 | 1,524 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 12 |
コンパイルメッセージ
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;
| ^~
ソースコード
// 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 = 0, v = 0; 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;
}
bal4u