[NOI2011]阿狸的打字机【AC自动机fail树+树状数组】

题目链接 P2414


  题目给出N个字符串,我们现在想知道的是第x个字符串在第y个字符串中出现的次数是多少次?

  求每个字符在其余字符中出现次数,想到从AC自动机上走,其实可以看作是求它的后缀的前缀中有多少个是满足的,换言之,我们可以去fail树中的父亲结点中查询。

就譬如说对于aa和aabaa两个字符串,我们搭建字典树

  并且对应上fail指针,我们可以知道,aa出现的次数为两次,分别是以整个串aabaa构成的前缀,以及以aa(aabaa的后缀)构成的前缀。

  搭建对应的fail树,我们知道每个对应的权值只能是以它的子树上的贡献,所以我们求它的子树中的某个节点的贡献,一定是它对应子树(含自己),所产生的贡献。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, len, M, tot, rid[maxN];
char s[maxN];
struct node
{
    int nex[26], val, fail, fa;
    inline void clear() { memset(nex, 0, sizeof(nex)); val = fail = fa = 0; }
}a[maxN];
inline void Insert()
{
    int root = 0;
    for(int i=0, ch; i<len; i++)
    {
        if(s[i] == 'P')
        {
            rid[++N] = root;
            continue;
        }
        if(s[i] == 'B')
        {
            root = a[root].fa;
            continue;
        }
        ch = s[i] - 'a';
        if(!a[root].nex[ch])
        {
            ++tot;
            a[tot].clear();
            a[tot].fa = root;
            a[root].nex[ch] = tot;
        }
        root = a[root].nex[ch];
    }
}
int head[maxN], cnt;
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void build_fail()
{
    queue<int> Q; Q.push(0);
    int tmp, son, p;
    while(!Q.empty())
    {
        tmp = Q.front(); Q.pop();
        for(int i=0; i<26; i++)
        {
            son = a[tmp].nex[i];
            if(son)
            {
                if(!tmp) a[son].fail = 0;
                else
                {
                    p = a[tmp].fail;
                    while(p && !a[p].nex[i]) p = a[p].fail;
                    a[son].fail = a[p].nex[i];
                }
                Q.push(son);
                addEddge(a[son].fail, son);
            }
            else a[tmp].nex[i] = a[a[tmp].fail].nex[i];
        }
    }
}
int trie[maxN] = {0};
inline void update(int x, int val) { while(x < maxN) { trie[x] += val; x += lowbit(x); } }
inline int query(int x) { int ss = 0; while(x) { ss += trie[x]; x -= lowbit(x); } return ss; }
inline int Range_Query(int x, int y) { return query(y) - query(x - 1); }
int dfn[maxN] = {0}, _Index, siz[maxN];
void dfs(int u)
{
    dfn[u] = ++_Index; siz[dfn[u]] = 1;
    for(int i=head[u], v; ~i; i=edge[i].nex) { v = edge[i].to; dfs(v); siz[dfn[u]] += siz[dfn[v]]; }
}
struct Question
{
    int x, y, id;
    Question(int a=0, int b=0, int c=0):x(a), y(b), id(c) {}
    friend bool operator < (Question e1, Question e2) { return e1.y < e2.y; }
}q[maxN];
int now_ques_ID, ans[maxN];
void dfs_Trie()
{
    int root = 0, id_N = 0;
    for(int i=0, ch; i<len; i++)
    {
        if(s[i] == 'P')
        {
            id_N++;
            while(q[now_ques_ID].y == id_N)
            {
                int x = rid[q[now_ques_ID].x];
                ans[q[now_ques_ID].id] = Range_Query(dfn[x], dfn[x] + siz[dfn[x]] - 1);
                now_ques_ID++;
            }
            continue;
        }
        if(s[i] == 'B')
        {
            update(dfn[root], -1);
            root = a[root].fa;
            continue;
        }
        ch = s[i] - 'a';
        root = a[root].nex[ch];
        update(dfn[root], 1);
    }
}
inline void init()
{
    N = tot = cnt = _Index = 0;
    now_ques_ID = 1;
    a[0].clear();
    memset(head, -1, sizeof(head));
}
int main()
{
    scanf("%s", s); len = (int)strlen(s);
    init();
    Insert();
    build_fail();
    dfs(0);
    scanf("%d", &M); int x, y;
    for(int i=1; i<=M; i++)
    {
        scanf("%d%d", &x, &y);
        q[i] = Question(x, y, i);
    }
    sort(q + 1, q + M + 1);
    dfs_Trie();
    for(int i=1; i<=M; i++) printf("%d\n", ans[i]);
    return 0;
}
/*
aaPbaaP
1
1 2
ans:2
*/

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值