luogu4173 incomplete string

For a class string matching with wildcards, we consider the configuration matching functions, is determined by the value of the matching position of the matching function.

First consider the problem without a wildcard: Given two strings \ (A, B \) , determines \ (B \) in a position which can be the \ (A \) match.

Apart KMP, we can also consider configured matching function to solve the matching problem, first \ (A \) series inverted simultaneously fill the ends \ (0 \) , the constructor \ (f_i = \ sum_ {j = 0} ^ i (A_i-B_ {ij of}) ^ 2 \) , then the \ (B \) in the section \ (I \) longitudinal positions ending of \ (| a | \) substring energy and \ (a \) match iff \ (F_i = 0 \) . After the resulting function expansion \ (\ sum_ {J} ^ = I 0 (A_i 2-2A_iB_ ^ B_ {ij of {+}} ^ ij of 2) \) , only one FFT needs to be able to get results.

Now consider the case where there is a wildcard, then the original definition of the matching function is obviously incomplete, consider the first \ (i \) directly counted as match-bit wildcard, it can be the first \ (i \) bit is the wildcard season \ (A_i = 0 \) , we can improve the matching function is defined as \ (F_i = \ sum_ {J} ^ = I 0 (ij of A_i-B_ {}) ^ {ij of 2A_iB_} \) .

Exhibition开后Tokuita \ (f_i = \ sum_ {j = 0} ^ i (A_i ^ 3B_ {ij} -2A_i ^ 2B_ {ij} ^ 2 + A_iB_ {ij} ^ 3) \) .

Polynomial multiplication can be done three times.

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
const int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
const int N=100000;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u];i;i=sq[i].nxt)
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

namespace My_Math{
    #define N 100000

    int fac[N+100],invfac[N+100];

    int add(int x,int y) {return x+y>=maxd?x+y-maxd:x+y;}
    int dec(int x,int y) {return x<y?x-y+maxd:x-y;}
    int mul(int x,int y) {return 1ll*x*y%maxd;}
    ll qpow(ll x,int y)
    {
        ll ans=1;
        while (y)
        {
            if (y&1) ans=mul(ans,x);
            x=mul(x,x);y>>=1;
        }
        return ans;
    }
    int inv(int x) {return qpow(x,maxd-2);}

    int C(int n,int m)
    {
        if ((n<m) || (n<0) || (m<0)) return 0;
        return mul(mul(fac[n],invfac[m]),invfac[n-m]);
    }

    int math_init()
    {
        fac[0]=invfac[0]=1;
        rep(i,1,N) fac[i]=mul(fac[i-1],i);
        invfac[N]=inv(fac[N]);
        per(i,N-1,1) invfac[i]=mul(invfac[i+1],i+1);
    }
    #undef N
}
using namespace My_Math;
struct qnode{int x,y,pre,now;}ask[2002000];
int n,m,q,tot=0,a[310][310],fa[3003000],id[310][310],ans[2002000];

int find(int x) 
{
    if (fa[x]==x) return fa[x];
    fa[x]=find(fa[x]);
    return fa[x];
}

int work(int x,int y)
{
    int cnt=1;
    rep(i,0,3)
    {
        int nx=x+dx[i],ny=y+dy[i];
        if ((nx<1) || (nx>n) || (ny<1) || (ny>m)) continue;
        if (a[x][y]==a[nx][ny])
        {
            int fx=find(id[x][y]),fy=find(id[nx][ny]);
            if (fx!=fy) {fa[fx]=fy;cnt--;}
        }
    }
    return cnt;
}

void solve1()
{
    rep(i,1,q)
    {
        if (ask[i].pre==ask[i].now) continue;
        int x=ask[i].x,y=ask[i].y;
        id[x][y]=(++tot);a[x][y]=ask[i].now;fa[tot]=tot;
        ans[i]+=work(x,y);
    }
}
    
void solve2()
{
    tot=0;
    rep(i,1,n) rep(j,1,m) {id[i][j]=(++tot);fa[tot]=tot;}
    rep(i,1,n) rep(j,1,m) work(i,j);
    per(i,q,1)
    {
        if (ask[i].pre==ask[i].now) continue;
        int x=ask[i].x,y=ask[i].y;
        id[x][y]=(++tot);fa[tot]=tot;a[x][y]=ask[i].pre;
        ans[i]-=work(x,y);
    }
}

int main()
{
    n=read();m=read();q=read();
    rep(i,1,q)
    {
        int x=read(),y=read(),w=read();
        ask[i]=(qnode){x,y,a[x][y],w};
        a[x][y]=w;
    }
    rep(i,1,n) rep(j,1,m) a[i][j]=0;
    solve1();solve2();ans[0]=1;
    rep(i,1,q) ans[i]+=ans[i-1];
    rep(i,1,q) printf("%d\n",ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/encodetalker/p/12387381.html