Codeforces Round #146 (Div. 1) C. Cyclical Quest——后缀自动机

题意:给定一个母串,每次询问一个子串或把子串的前一部分移到末尾形成的串在母串中出现的次数。

处理方法都是很常见的。对询问中的每个子串,设长度为len。复制一份放到其末尾位置并把最后一个字符去掉。再拿这个串和母串的自动机匹配,对于询问串的每一个位置,记录该位置往前能匹配的长度,如果长度大于等于len,则通过pre指针找到从这个位置往前len个字符之间的串在自动机中对应的状态。并将答案加上该状态出现的次数。还要注意,因为复制后的询问串中会有一些长度为len的相同子串出现在不同的位置,那么在匹配的过程中就有可能重复计算,拿我们就在每个状态上加上标记,如果标记过就不再计算。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lng long long
using namespace std;

const int maxn = 3000000 + 10;
int str[maxn], len;
struct suffixautomaton
{
    int ch[maxn][30], pre[maxn], val[maxn];
    int c[maxn], top[maxn], flag[maxn];
    int num[maxn], sz, last;
    
    void init()
    {
        pre[0] = -1; last = 0; sz = 1;
        memset(ch[0], 0, sizeof(ch[0]));
        memset(num, 0, sizeof(num));
        memset(flag, 0, sizeof(flag));
    }
    
    void insert(int x)
    {
        int p = last, np = sz++;
        last = np;
        memset(ch[np], 0, sizeof(ch[np]));
        val[np] = val[p] + 1;
        while(p != -1 && ch[p][x] == 0)
        {
            ch[p][x] = np;
            p = pre[p];
        }
        if(p == -1) pre[np] = 0;
        else 
        {
            int q = ch[p][x];
            if(val[q] == val[p] + 1) pre[np] = q;
            else
            {
                int nq = sz++;
                memcpy(ch[nq], ch[q], sizeof(ch[q]));
                val[nq] = val[p] + 1;
                pre[nq] = pre[q];
                pre[q] = pre[np] = nq;
                while(p != -1 && ch[p][x] == q) { ch[p][x] = nq; p = pre[p]; }
            }
        }
    }
    
    void calc()
    {
        memset(c, 0, sizeof(c));
        for(int i = 0; i < sz; ++i) c[val[i]] += 1;
        for(int i = 1; i <= len; ++i) c[i] += c[i - 1];
        for(int i = 0; i < sz; ++i) top[--c[val[i]]] = i;
        for(int i = 0; ; i = ch[i][str[val[i]]])
        {
            num[i] = 1;
            if(val[i] == len) break;
        }
        for(int i = sz - 1; i > 0; --i)
            num[pre[top[i]]] += num[top[i]];
    }
    
    lng query(char * s, int c, int tmplen)
    {
        lng ans = 0;
        int u = 0, l = 0;
        for(int i = 0; s[i]; ++i)
        {
            int x = s[i] - 'a';
            if(ch[u][x]) { u = ch[u][x]; l++; }
            else
            {
                while(u != -1 && ch[u][x] == 0) u = pre[u];
                if(u == -1) u = 0, l = 0;
                else { l = val[u] + 1; u = ch[u][x]; }
            }
            if(l >= tmplen)
            {
                int v = u;
                while(val[pre[v]] >= tmplen) v = pre[v];
                if(flag[v] != c) { flag[v] = c; ans += num[v]; }
            }
        }
        return ans;
    }
    
}sam;

char s[maxn];

int main()
{
    scanf("%s", s); len = strlen(s); sam.init();
    for(int i = 0; i < len; ++i) { str[i] = s[i] - 'a'; sam.insert(str[i]); }
    sam.calc();
    int q; scanf("%d", &q);
    while(q--)
    {
        scanf("%s", s);
        int tmp = strlen(s);
        for(int i = 0; i < tmp - 1; ++i) s[tmp + i] = s[i];
        s[2 * tmp - 1] = '\0';
        cout << sam.query(s, q + 1, tmp) << "\n";
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值