BZOJ2780——[Spoj]8093 Sevenk Love Oimaster

0、题意:给定N个原始字符串S,M次查询某个特殊的字符串S’在多少个原始串中出现过。

1、分析:这个题我们第一感觉就是可以用后缀自动机来搞,然后我们发现不是本质不同的字串。。求出现过的次数,也就是说多次出现只算一次。。。然后我们依旧用建立后缀自动机,然后我们观察到询问是可以离线的。。然后冷静一下QAQ……好了。。询问可以离线后,我们对这个树形结构求一下dfs序,然后我们就可以把树上的询问变成一个序列的区间查询,然后就变成了BZOJ1878HH的项链。。具体怎么搞呢?我们可以将询问排序,然后离线的扫一遍,记录一下x的颜色的上一次出现位置,然后转移就好

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

inline int read(){
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while('0' <= ch && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

struct Edge{
    int u, v, next;
} G[400010];
int head[200010], tot;
char str[500010];
int first[200010], val[200010], nxt[200010], num;
int C[200010];
int n, m;
int pre[200010];

inline void ues(int u, int v){
    num ++;
    val[num] = v;
    nxt[num] = first[u];
    first[u] = num;
}

inline void add(int x, int y){
    G[++ tot] = (Edge){x, y, head[x]};
    head[x] = tot;
}

map<int , int> :: iterator it;

int vis[200010];
int last, cnt, p, np, q, nq;
int len[200010], fa[200010];
map<int , int> tranc[200010];
int right[200010], c[200010], od[200010];
int tim;
int ans[200010];

inline void insert(int c, int w){
    p = last;
    if(tranc[p][c] != 0){
        q = tranc[p][c];
        if(len[q] == len[p] + 1) last = tranc[p][c];
        else{
            nq = ++ cnt; len[nq] = len[p] + 1;
            for(it = tranc[q].begin(); it != tranc[q].end(); it ++){
                tranc[nq][it -> first] = it -> second;
            }
            fa[nq] = fa[q];
            fa[q] = nq;
            while(tranc[p][c] == q){
                tranc[p][c] = nq;
                p = fa[p];
            }
            last = nq;
        }
    }
    else{
        last = np = ++ cnt;
        vis[np] = 1;
        len[np] = len[p] + 1;
        tranc[cnt].clear();
        right[np] = 1;
        while(!tranc[p][c] && p) tranc[p][c] = np, p = fa[p];
        if(!p) fa[np] = 1;
        else{
            q = tranc[p][c];
            if(len[q] == len[p] + 1) fa[np] = q;
            else{
                nq = ++ cnt; len[nq] = len[p] + 1;
                for(it = tranc[q].begin(); it != tranc[q].end(); it ++){
                    tranc[nq][it -> first] = it -> second;
                }
                fa[nq] = fa[q];
                fa[q] = fa[np] = nq;
                while(tranc[p][c] == q){
                    tranc[p][c] = nq;
                    p = fa[p];
                }
            }
        }
        last = np;
    }
    ues(last, w);
}

inline void init(){
    memset(head, -1, sizeof(head));
    for(int i = 1; i <= cnt; i ++){
        add(fa[i], i);
    }
}

inline void update(int x, int d){
    if(x == 0) return;
    for(; x <= cnt; x += (x & -x)){
        C[x] += d;
    }
}

inline int sum(int x){
    int ret = 0;
    for(; x > 0; x -= (x & -x)){
        ret += C[x];
    }
    return ret;
}

inline void dfs(int x){
    int t = ++ tim;
    for(int i = first[x]; i; i = nxt[i]){
        update(pre[val[i]], -1);
        update(t, 1);
        pre[val[i]] = t;
    }
    for(int i = head[x]; i != -1; i = G[i].next){
        dfs(G[i].v);
    }
    ans[x] = sum(tim) - sum(t - 1);
}

int query(char *s)
{
    int st = 1;
    while(*s != '\0')
        st = tranc[st][*s], s ++;
    return st;
}  

int main(){
    last = cnt = 1;
    n = read(); m = read();
    for(int i = 1; i <= n; i ++){
        last = 1;
        scanf("%s", str + 1);
        int L = strlen(str + 1);
        for(int j = 1; j <= L; j ++){
            insert(str[j], i);
        }
    }
    init();
    dfs(1);
    for(int i = 1; i <= m; i ++){
        scanf("%s", str);
        printf("%d\n", ans[query(str)]);
    }
    return 0;
}
上一篇:【BZOJ 3512】 DZY Loves Math IV(杜教筛+记忆化搜索)


下一篇:rsa special