結果
問題 | No.2626 Similar But Different Name |
ユーザー | 👑 ygussany |
提出日時 | 2024-02-09 22:24:38 |
言語 | C (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 157 ms / 3,000 ms |
コード長 | 5,760 bytes |
コンパイル時間 | 496 ms |
コンパイル使用メモリ | 34,688 KB |
実行使用メモリ | 76,364 KB |
最終ジャッジ日時 | 2024-09-28 15:38:23 |
合計ジャッジ時間 | 4,179 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
16,120 KB |
testcase_01 | AC | 3 ms
16,248 KB |
testcase_02 | AC | 3 ms
16,124 KB |
testcase_03 | AC | 3 ms
16,116 KB |
testcase_04 | AC | 3 ms
16,244 KB |
testcase_05 | AC | 3 ms
14,212 KB |
testcase_06 | AC | 3 ms
16,128 KB |
testcase_07 | AC | 3 ms
16,260 KB |
testcase_08 | AC | 3 ms
16,264 KB |
testcase_09 | AC | 3 ms
14,224 KB |
testcase_10 | AC | 7 ms
28,572 KB |
testcase_11 | AC | 7 ms
28,568 KB |
testcase_12 | AC | 6 ms
28,436 KB |
testcase_13 | AC | 6 ms
28,576 KB |
testcase_14 | AC | 6 ms
26,532 KB |
testcase_15 | AC | 5 ms
28,452 KB |
testcase_16 | AC | 6 ms
28,440 KB |
testcase_17 | AC | 6 ms
28,572 KB |
testcase_18 | AC | 157 ms
76,364 KB |
testcase_19 | AC | 17 ms
26,712 KB |
testcase_20 | AC | 14 ms
26,904 KB |
testcase_21 | AC | 13 ms
25,436 KB |
testcase_22 | AC | 149 ms
74,732 KB |
testcase_23 | AC | 152 ms
74,344 KB |
testcase_24 | AC | 149 ms
74,308 KB |
testcase_25 | AC | 149 ms
73,212 KB |
testcase_26 | AC | 144 ms
74,292 KB |
testcase_27 | AC | 152 ms
74,496 KB |
testcase_28 | AC | 147 ms
74,360 KB |
testcase_29 | AC | 148 ms
75,272 KB |
testcase_30 | AC | 148 ms
74,024 KB |
testcase_31 | AC | 148 ms
75,116 KB |
testcase_32 | AC | 147 ms
75,120 KB |
testcase_33 | AC | 149 ms
74,824 KB |
testcase_34 | AC | 148 ms
75,172 KB |
testcase_35 | AC | 150 ms
73,400 KB |
testcase_36 | AC | 150 ms
74,588 KB |
testcase_37 | AC | 148 ms
75,020 KB |
ソースコード
#include <stdio.h> void Z_algorithm(char S[], int Z[]) { int i, j, k, l; for (l = 0; S[l] != 0; l++); Z[0] = l; for (i = 1, j = 0; i < l; i++) { for (; i + j < l && S[i+j] == S[j]; j++); Z[i] = j; if (j == 0) continue; for (k = 1; k < j && k + Z[k] < j; k++) Z[i+k] = Z[k]; i += k - 1; j -= k; } } long long div_mod(long long x, long long y, long long z) { if (x % y == 0) return x / y; else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y; } #define NTT_MAX 22 #define NTT_d_MAX (1 << NTT_MAX) const int Mod = 998244353, bit[24] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608}, bit_inv[24] = {1, 499122177, 748683265, 873463809, 935854081, 967049217, 982646785, 990445569, 994344961, 996294657, 997269505, 997756929, 998000641, 998122497, 998183425, 998213889, 998229121, 998236737, 998240545, 998242449, 998243401, 998243877, 998244115, 998244234}, root[24] = {1, 998244352, 911660635, 372528824, 929031873, 452798380, 922799308, 781712469, 476477967, 166035806, 258648936, 584193783, 63912897, 350007156, 666702199, 968855178, 629671588, 24514907, 996173970, 363395222, 565042129, 733596141, 267099868, 15311432}, root_inv[24] = {1, 998244352, 86583718, 509520358, 337190230, 87557064, 609441965, 135236158, 304459705, 685443576, 381598368, 335559352, 129292727, 358024708, 814576206, 708402881, 283043518, 3707709, 121392023, 704923114, 950391366, 428961804, 382752275, 469870224}; void NTT_inline(int kk, int a[], int x[]) { int h, hh, i, ii, j, jj, k, l, r = bit[kk], d = bit[kk-1], tmpp, cur, prev; int *pi, *pii, *pj, *pjj; static int y[2][NTT_d_MAX]; long long tmp; for (i = 0; i < r; i++) y[0][i] = a[i]; for (k = 1, kk--, cur = 1, prev = 0; kk >= 0; k++, kk--, cur ^= 1, prev ^= 1) { for (h = 0, tmp = 1; h << (kk + 1) < r; h++, tmp = tmp * root[k] % Mod) { for (hh = 0, pi = &(y[cur][h<<kk]), pii = pi + d, pj = &(y[prev][h<<(kk+1)]), pjj = pj + bit[kk]; hh < bit[kk]; hh++, pi++, pii++, pj++, pjj++) { tmpp = tmp * (*pjj) % Mod; *pi = *pj + tmpp; if (*pi >= Mod) *pi -= Mod; *pii = *pj - tmpp; if (*pii < 0) *pii += Mod; } } } for (i = 0; i < r; i++) x[i] = y[prev][i]; } void NTT_reverse_inline(int kk, int a[], int x[]) { int h, hh, i, ii, j, jj, k, l, r = bit[kk], d = bit[kk-1], tmpp, cur, prev; int *pi, *pii, *pj, *pjj; static int y[2][NTT_d_MAX]; long long tmp; for (i = 0; i < r; i++) y[0][i] = a[i]; for (k = 1, kk--, cur = 1, prev = 0; kk >= 0; k++, kk--, cur ^= 1, prev ^= 1) { for (h = 0, tmp = 1; h << (kk + 1) < r; h++, tmp = tmp * root_inv[k] % Mod) { for (hh = 0, pi = &(y[cur][h<<kk]), pii = pi + d, pj = &(y[prev][h<<(kk+1)]), pjj = pj + bit[kk]; hh < bit[kk]; hh++, pi++, pii++, pj++, pjj++) { tmpp = tmp * (*pjj) % Mod; *pi = *pj + tmpp; if (*pi >= Mod) *pi -= Mod; *pii = *pj - tmpp; if (*pii < 0) *pii += Mod; } } } for (i = 0; i < r; i++) x[i] = y[prev][i]; } #define NTT_THR 70 // Compute c[0-dc] = a[0-da] * b[0-db] (naive) void FPS_prod_naive(int da, int db, int dc, int a[], int b[], int c[]) { int i, j, sa, sb; static int supp_a[NTT_d_MAX], supp_b[NTT_d_MAX]; static long long tmp[NTT_d_MAX]; for (i = 0, sa = 0; i <= da; i++) if (a[i] != 0) supp_a[sa++] = i; for (i = 0, sb = 0; i <= db; i++) if (b[i] != 0) supp_b[sb++] = i; for (i = 0; i <= dc; i++) tmp[i] = 0; for (i = 0; i < sa; i++) for (j = 0; j < sb && supp_a[i] + supp_b[j] <= dc; j++) tmp[supp_a[i] + supp_b[j]] += (long long)a[supp_a[i]] * b[supp_b[j]] % Mod; for (i = 0; i <= dc; i++) c[i] = tmp[i] % Mod; } // Compute c[0-dc] = a[0-da] * b[0-db] (NTT) void FPS_prod_NTT(int da, int db, int dc, int a[], int b[], int c[]) { int i, k; static int aa[NTT_d_MAX], bb[NTT_d_MAX], cc[NTT_d_MAX]; for (k = 0; bit[k] <= da + db; k++); for (i = 0; i <= da; i++) aa[i] = a[i]; for (i = da + 1; i < bit[k]; i++) aa[i] = 0; for (i = 0; i <= db; i++) bb[i] = b[i]; for (i = db + 1; i < bit[k]; i++) bb[i] = 0; static int x[NTT_d_MAX], y[NTT_d_MAX], z[NTT_d_MAX]; NTT_inline(k, aa, x); if (db == da) { for (i = 0; i <= da; i++) if (a[i] != b[i]) break; if (i <= da) NTT_inline(k, bb, y); else for (i = 0; i < bit[k]; i++) y[i] = x[i]; } else NTT_inline(k, bb, y); for (i = 0; i < bit[k]; i++) z[i] = (long long)x[i] * y[i] % Mod; NTT_reverse_inline(k, z, cc); for (i = 0; i <= dc; i++) c[i] = (long long)cc[i] * bit_inv[k] % Mod; } // Compute c[0-dc] = a[0-da] * b[0-db] void FPS_prod(int da, int db, int dc, int a[], int b[], int c[]) { int i, sa, sb; if (da > dc) da = dc; if (db > dc) db = dc; for (i = 0, sa = 0; i <= da && sa <= NTT_THR; i++) if (a[i] != 0) sa++; for (i = 0, sb = 0; i <= db && sb <= NTT_THR; i++) if (b[i] != 0) sb++; if (sa <= NTT_THR || sb <= NTT_THR) FPS_prod_naive(da, db, dc, a, b, c); else FPS_prod_NTT(da, db, dc, a, b, c); } int main() { int N, M, K; char S[500001], T[500001]; scanf("%d %d %d", &N, &M, &K); scanf("%s", S); scanf("%s", T); int i, Z[1000001]; char R[1000001]; for (i = 0; i < M; i++) R[i] = (T[i] >= 'A' && T[i] <= 'Z')? T[i]: T[i] - 'a' + 'A'; for (i = 0; i < N; i++) R[i+M] = (S[i] >= 'A' && S[i] <= 'Z')? S[i]: S[i] - 'a' + 'A'; R[N+M] = 0; Z_algorithm(R, Z); int a[500001] = {}, b[500001] = {}, c[1000001]; for (i = 0; i < N; i++) a[i] = (S[i] >= 'A' && S[i] <= 'Z')? 1: Mod - 1; for (i = 0; i < M; i++) b[M-i-1] = (T[i] >= 'A' && T[i] <= 'Z')? 1: Mod - 1; FPS_prod(N - 1, M - 1, N + M - 2, a, b, c); for (i = 0; i <= N + M - 2; i++) if (c[i] >= 1000000) c[i] -= Mod; int ans = 0; for (i = 0; i <= N - M; i++) if (Z[i+M] >= M && c[i+M-1] < M && c[i+M-1] >= M - K * 2) ans++; printf("%d\n", ans); fflush(stdout); return 0; }