题解:CF1975G Zimpha Fan Club

· · 题解

前置芝士

NTT 优化通配符字符串匹配,本人属实菜,这里只给一个第二篇题解的链接,顺便给一个第二篇题解的评论:

简洁明了,比第一篇一堆废话强多了。赞了!

好的,现在我假设您会了。

正题

此题比普通通配符多一个 *。而 * 能干的事情非常多,所以考虑先没有 * 的和都有 * 的。

没有 * 的显然,都有 * 的发现只要匹配 * 的前缀和后缀,因为珂以用中间的 * 匹配中间的字符:

然后就是有一个串有 * 的,不妨令 s 有 *。首先与上面一样需要匹配前后缀,并顺便删掉,然后考虑将 s 拆成许多个没有 * 的字符串 s_1, s_2, \cdots, s_k 与 * 组成(我的代码要判 * 相邻的情况,就这样交了 40 发 /ll)。

然后因为 t 中是没有 * 的,那么 s_1, s_2, \cdots, s_k 一定是 t 中的不相交且按顺序排列的子串,而又因为相邻的 s 中有 *,于是条件充分。

因此珂以直接用 NTT 优化通配符匹配找到最左子串,因为显然是越左边条件越松,直接维护一个当前左端点 L 即可。

直接做就是高贵的 \Theta(nm \log n),但是非常浪费。于是考虑 s_i 匹配的时候先找 2|s_i|-1 个位置(即 |s_i| 个开头),失配就让左端点 L \leftarrow L + |s_i|,否则与上面一样。

现在时间就为 \Theta(n \log n)。然后此题凉心出题人给出了 n, m \le 2 \times 10^6 的好限制,所以不能预处理 \omega。而且出题人还顺便贴心的卡了下模数,所以需要 MTT,FTT 随机赋权。

#include <bits/stdc++.h>

#define i16 short
#define i32 signed
#define i64 long long
#define i128 __int128_t

#define u16 unsigned short
#define u32 unsigned
#define u64 unsigned long long
#define u128 __uint128_t

#define f32 float
#define f64 double

template < typename T > inline const T max(const T x, const T y) { return (x > y) ? (x) : (y); }
template < typename T > inline const T min(const T x, const T y) { return (x < y) ? (x) : (y); }
template < typename T > inline const T abs(const T x) { return (x < 0) ? (-x) : x; }

#define ll long long
// #define int long long
// #define int __int128_t

const i32 N = 5e6;
const i32 mod = 998244353, g = 3, inv2 = mod + 1 >> 1;
inline i32 ca(i32 x) { return x + (x >> 31 & mod); }
inline void sw(i32& x, i32& y) { x ^= y ^= x ^= y; }
typedef std::vector < i32 > poly;
i32 R[N];
inline i32 ksm(i32 x, i32 y, i32 res = 1)
{
    while (y)
        if (y & 1) res = 1ll * res * x % mod, y >>= 1, x = 1ll * x * x % mod;
        else y >>= 1, x = 1ll * x * x % mod;
    return res;
}
inline void NTT_init(i32 n)
{
    i32 k = 1; while ((1 << k) < n) k++;
    for (i32 i = 0; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << k - 1);
}
inline void NTT(i32 n, poly& a, bool type)
{
    a.resize(n);
    for (i32 i = 0; i < n; i++)
        if (i < R[i]) sw(a[i], a[R[i]]);
    i32 tmp = 1;
    for (i32 deep = 1, m = 2, len = 1; m <= n; deep++, m <<= 1, len <<= 1)
    {
        tmp = 1ll * tmp * inv2 % mod;
        i32 w = ksm(g, (mod - 1) >> deep);
        if (!type) w = ksm(w, mod - 2);
        for (i32 pos = 0; pos < n; pos += m)
            for (i32 i = pos, j = pos + len, tmp, wn = 1; i < pos + len; i++, j++, wn = 1ll * wn * w % mod)
                tmp = 1ll * wn * a[j] % mod,
                a[j] = ca(a[i] - tmp),
                a[i] = ca(a[i] + tmp - mod);
    }
    if (!type) for (i32 i = 0; i < n; i++) a[i] = 1ll * a[i] * tmp % mod;
}
i32 pp[26];
inline poly Mat(char* s1, char* s2, i32 n, i32 m, char sp)
{
#define ____calc(x) ((x == sp) ? 0 : (pp[x - 'a']))
    i32 limit = 1;
    while (limit < (n + n)) limit <<= 1;
    poly a(n), b(m), res(limit);
    NTT_init(limit);

    for (i32 i = 0; i < n; i++)
        a[i] = 1ll * ____calc(s1[i]) * ____calc(s1[i]) % mod * ____calc(s1[i]) % mod;
    for (i32 i = 0; i < m; i++)
        b[i] = ____calc(s2[m - i - 1]);
    NTT(limit, a, 1), NTT(limit, b, 1);
    for (i32 i = 0; i < limit; i++) res[i] = 1ll * a[i] * b[i] % mod;

    a.resize(n), b.resize(m);
    for (i32 i = 0; i < n; i++)
        a[i] = ____calc(s1[i]);
    for (i32 i = 0; i < m; i++)
        b[i] = 1ll * ____calc(s2[m - i - 1]) * ____calc(s2[m - i - 1]) % mod * ____calc(s2[m - i - 1]) % mod;
    NTT(limit, a, 1), NTT(limit, b, 1);
    for (i32 i = 0; i < limit; i++) res[i] = ca(res[i] + 1ll * a[i] * b[i] % mod - mod);

    a.resize(n), b.resize(m);
    for (i32 i = 0; i < n; i++)
        a[i] = 1ll * ____calc(s1[i]) * ____calc(s1[i]) % mod;
    for (i32 i = 0; i < m; i++)
        b[i] = 1ll * ____calc(s2[m - i - 1]) * ____calc(s2[m - i - 1]) % mod;
    NTT(limit, a, 1), NTT(limit, b, 1);
    for (i32 i = 0; i < limit; i++) res[i] = ca(res[i] - 2ll * a[i] * b[i] % mod);

    NTT(limit, res, 0); a.clear();
    for (i32 i = m - 1; i < n; i++) if (!res[i]) a.emplace_back(i - m + 1);
    return a;
}
i32 n, m;
char s1[N], s2[N];
inline bool equiv(char a, char b) { return a == '-' || b == '-' || a == b; }
namespace SpecialA
{
    inline bool check()
    {
        for (i32 i = 0; i < n; i++) if (s1[i] == '*') return false;
        for (i32 i = 0; i < m; i++) if (s2[i] == '*') return false;
        return true;
    }
    inline void solov()
    {
        if (n ^ m) return std::cout << "No\n", void();
        for (i32 i = 0; i < n; i++)
            if (!equiv(s1[i], s2[i])) return std::cout << "No\n", void();
        std::cout << "Yes\n";
    }
};
namespace SpecialB
{
    inline bool check()
    {
        i32 cnt = 0; for (i32 i = 0; i < n; i++) if (s1[i] == '*') cnt++; if (!cnt) return false; cnt = 0;
        for (i32 i = 0; i < m; i++) if (s2[i] == '*') cnt++; if (!cnt) return false; return true;
    }
    inline void solov()
    {
        i32 L1 = 0, R1 = n - 1, L2 = 0, R2 = m - 1;
        while (s1[L1] ^ '*') L1++; while (s1[R1] ^ '*') R1--;
        while (s2[L2] ^ '*') L2++; while (s2[R2] ^ '*') R2--;
        L1 = min(L1, L2);
        R1 = (n - R1 > m - R2) ? (R2 - m + n) : (R1);
        for (i32 i = 0; i < L1; i++) if (!equiv(s1[i], s2[i])) return std::cout << "No\n", void();
        for (i32 i = R1 + 1; i < n; i++) if (!equiv(s1[i], s2[i - n + m])) return std::cout << "No\n", void();
        std::cout << "Yes\n";
    }
};
namespace SpecialC
{
    inline bool check()
    {
        i32 cnt = 0; for (i32 i = 0; i < n; i++) if (s1[i] == '*') cnt++;
        if (!cnt)
        {
            for (i32 i = 0; i < max(n, m); i++) s1[i] ^= s2[i] ^= s1[i] ^= s2[i];
            n ^= m ^= n ^= m;
        }
        return true;
    }
    i32 p[N], pi;
    inline void solov()
    {
        for (i32 i = 0; i < n; i++)
            if (s1[i] == '*') p[++pi] = i;
        i32 L = 0, R = n - 1;
        while (s1[L] != '*') L++;
        while (s1[R] != '*') R--;
        if (L + n - 1 - R > m) return std::cout << "No\n", void();  //前后缀比另一个串总长还长
        for (i32 i = 0; i < L; i++) if (!equiv(s1[i], s2[i])) return std::cout << "No\n", void();
        for (i32 i = R + 1; i < n; i++) if (!equiv(s1[i], s2[i - n + m])) return std::cout << "No\n", void();
        m = R - n + m, n = R;
        for (i32 i = 1; i < pi; i++)
        {
            if (p[i] == p[i + 1] - 1) continue;
            while (1)
            {
                poly ans = Mat(s2 + L, s1 + p[i] + 1, min(2 * p[i + 1] - 2 * p[i] - 3, m - L + 1), p[i + 1] - p[i] - 1, '-');
                L += p[i + 1] - p[i] - 1;
                if (!ans.empty()) { L += ans[0]; break; }
                if (L > m) return std::cout << "No\n", void();
            }
        }
        std::cout << "Yes\n";
    }
};
std::mt19937 rnd(time(0));
inline void ___()
{
    for (i32 i = 0; i < 26; i++) pp[i] = (rnd() % (mod - 1) + 1);   //随机赋权
    std::ios::sync_with_stdio(false), std::cin.tie(), std::cout.tie();
    std::cin >> n >> m >> s1 >> s2;
    if (SpecialA::check()) return SpecialA::solov();    //没有*
    if (SpecialB::check()) return SpecialB::solov();    //均有*
    if (SpecialC::check()) return SpecialC::solov();    //otherwise
}
signed main()
{
    i32 T = 1; //std::cin >> T;
    while (T--) ___();
}