洛谷P3002 [USACO10DEC]恐吓信Threatening Letter

链接

点击跳转

题解

农夫约翰好恶趣味啊qwq

这题就直接拿第二个串在第一个串的后缀自动机上跑,每次跑到 1 1 就给计数器 + 1 +1

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct SAM
{
    int tot, las, ch[maxn<<1][26], fa[maxn<<1], len[maxn<<1], pref[maxn<<1];
    int* operator[](int u){return ch[u];}
    void init()
    {
        int i;
        rep(i,tot)cl(ch),fa[i]=len[i]=pref[i]=0;
        tot=las=1;
    }
    void append(int c)
    {
        int p(las);
        len[las=++tot]=len[p]+1;
        pref[las]=1;
        for(;p and !ch[p][c];p=fa[p])ch[p][c]=las;
        if(!p)fa[las]=1;
        else
        {
            int q=ch[p][c];
            if(len[q]==len[p]+1)fa[las]=q;
            else
            {
                int qq=++tot;
                memcpy(ch[qq],ch[q],sizeof(ch[q]));
                fa[qq]=fa[q];
                len[qq]=len[p]+1;
                fa[q]=fa[las]=qq;
                for(;ch[p][c]==q;p=fa[p])ch[p][c]=qq;
            }
        }
    }
    int mov(int p, int c){return max(1,ch[p][c]);}
}sam;
ll n, m, p, ans;
char t[85];
int main()
{
    ll i;
    sam.init();
    n = read(), m = read();
    while(n)
    {
        cl(t);
        scanf("%s",t+1);
        auto len = strlen(t+1);
        rep(i,len)sam.append(t[i]-'A');
        n -= len;
    }
    ll p = 1;
    while(m)
    {
        cl(t);
        scanf("%s",t+1);
        auto len = strlen(t+1);
        rep(i,len)
        {
            p = sam.mov(p,t[i]-'A');
            if(p==1)ans++, p = sam.mov(p,t[i]-'A');
        }
        m -= len;
    }
    printf("%lld",ans+1);
    return 0;
}
发布了863 篇原创文章 · 获赞 72 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/FSAHFGSADHSAKNDAS/article/details/103826941
今日推荐