P4173 残缺的字符串

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P4173 残缺的字符串相关的知识,希望对你有一定的参考价值。

P4173 残缺的字符串

题意:

有A,B两个串,每个串都有通配符,问A为模板串,对于 B 的每一个位置 i,从这个位置开始连续 m 个字符形成的子串是否可能与 A 串完全匹配?

题解:

我们定义两个字符串S,T的距离为:
dis(S,T)= ∑ i = 1 n − 1 ( S i − T i ) 2 ∗ S i ∗ T i \\sum_{i=1}^{n-1}(S_{i}-T_{i})^2*S_{i}*T_{i} i=1n1(SiTi)2SiTi
当T中以i结尾的串与S能匹配的条件为:
d i s ( S , T i − m + 1 , i ) = 0 dis(S,T_{i-m+1,i})=0 dis(S,Tim+1,i)=0
f i = ∑ j = 0 i ( S j − T i − j ) 2 ∗ S j ∗ T i − j = ∑ j = 0 i S j 3 ∗ T i − j − 2 ∗ ∑ j = 0 i S j 2 T i − j 2 + ∑ j = 0 i S j ∗ T i − j 3 f_{i}=\\sum_{j=0}^{i}(S_{j}-T_{i-j})^2*S_{j}*T_{i-j}=\\sum_{j=0}^{i}S_{j}^{3}*T_{i-j}-2*\\sum_{j=0}^{i}S_{j}^{2}T_{i-j}^{2}+\\sum_{j=0}^{i}S_{j}*T_{i-j}^{3} fi=j=0i(SjTij)2SjTij=j=0iSj3Tij2j=0iSj2Tij2+j=0iSjTij3
我的板子
人傻了

这里贴的是别人的板子,开氧过了,不开80

代码:

// Problem: P4173 残缺的字符串
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4173
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// Data:2021-08-24 00:29:28
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
    x= 0;
    char c= getchar();
    bool flag= 0;
    while (c < '0' || c > '9')
        flag|= (c == '-'), c= getchar();
    while (c >= '0' && c <= '9')
        x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
    if (flag)
        x= -x;
    read(Ar...);
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
    startTime= clock();
    freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
#define MAXN 2000005
#define reg register
#define inl inline
#define db double
#define eps 1e-6
using namespace std;
const int Mod= 998244353;
const db Pi= acos(-1.0);
struct Complex
{
    db x, y;
    friend Complex operator+(const Complex& a, const Complex& b)
    {
        return ((Complex){a.x + b.x, a.y + b.y});
    }
    friend Complex operator-(const Complex& a, const Complex& b)
    {
        return ((Complex){a.x - b.x, a.y - b.y});
    }
    friend Complex operator*(const Complex& a, const Complex& b)
    {
        return ((Complex){a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x});
    }
    friend Complex operator*(const Complex& a, const db& val)
    {
        return ((Complex){a.x * val, a.y * val});
    }
} f[MAXN], g[MAXN], p[MAXN];
int n, m, lim= 1, maxn, rev[MAXN], a[MAXN], b[MAXN];
char S[MAXN], T[MAXN];
bool used[MAXN];
vector<int> v;
inl void FFT(reg Complex* A, reg int opt)
{
    for (reg int i= 0; i < lim; i++)
        if (i < rev[i])
            swap(A[i], A[rev[i]]);
    for (reg int mid= 1; mid < lim; mid<<= 1) {
        reg Complex Wn= ((Complex){cos(Pi / (db)mid), (db)opt * sin(Pi / (db)mid)});
        for (reg int j= 0; j < lim; j+= (mid << 1)) {
            reg Complex W= ((Complex){1, 0});
            for (reg int k= 0; k < mid; k++, W= W * Wn) {
                reg Complex x= A[j + k], y= W * A[j + k + mid];
                A[j + k]= x + y;
                A[j + k + mid]= x - y;
            }
        }
    }
}
int main()
{
    scanf("%d %d", &m, &n);
    scanf("%s", T + 1);
    scanf("%s", S + 1);
    for (reg int i= 1; i <= m; i++)
        if (T[i] != '*')
            a[i - 1]= T[i] - 'a' + 1;
    for (reg int i= 1; i <= n; i++)
        if (S[i] != '*')
            b[i - 1]= S[i] - 'a' + 1;
    while (lim <= (n + m)) {
        lim<<= 1;
        maxn++;
    }
    for (reg int i= 0; i < lim; i++)
        rev[i]= ((rev[i >> 1] >> 1) | ((i & 1) << maxn - 1));
    reverse(a, a + m);
    for (reg int i= 0; i < m; i++)
        f[i]= ((Complex){a[i] * a[i] * a[i], 0});
    for (reg int i= 0; i < n; i++)
        g[i]= ((Complex){b[i], 0});
    FFT(f, 1);
    FFT(g, 1);
    for (reg int i= 0; i < lim; i++)
        p[i]= p[i] + f[i] * g[i];
    for (reg int i= 0; i < lim; i++)
        f[i]= g[i]= ((Complex){0, 0});
    for (reg int i= 0; i < m; i++)
        f[i]= ((Complex){a[i] * a[i], 0});
    for (reg int i= 0; i < n; i++)
        g[i]= ((Complex){b[i] * b[i], 0});
    FFT(f, 1);
    FFT(g, 1);
    for (reg int i= 0; i < lim; i++)
        p[i]= p[i] - f[i] 以上是关于P4173 残缺的字符串的主要内容,如果未能解决你的问题,请参考以下文章

P4173 残缺的字符串

P4173 残缺的字符串(FFT)

P4173 残缺的字符串 fft

[Luogu P4173]残缺的字符串 ( 数论 FFT)

luogu P4173 残缺的字符串 FFT

洛谷 - P4173 残缺的字符串(多项式匹配字符串-NTT)