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;
}

BZOJ2780——[Spoj]8093 Sevenk Love Oimaster的更多相关文章

  1. BZOJ2780 [Spoj]8093 Sevenk Love Oimaster 【广义后缀自动机】

    题目 Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXun was dat ...

  2. BZOJ2780: [Spoj]8093 Sevenk Love Oimaster(广义后缀自动机,Parent树,Dfs序)

    Description Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXu ...

  3. Bzoj2780: [Spoj]8093 Sevenk Love Oimaster

    题目 传送门 Sol 就是广义\(sam\) 然后记录下每个状态属于哪些串,开\(set\)维护 \(parent\)树上启发式合并一下就好了 # include <bits/stdc++.h& ...

  4. 【BZOJ2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机

    [BZOJ2780][Spoj]8093 Sevenk Love Oimaster Description Oimaster and sevenk love each other.     But r ...

  5. 三种做法:BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster

    目录 题意 思路 AC_Code1 AC_Code2 AC_Code3 参考 @(bzoj 2780: [Spoj]8093 Sevenk Love Oimaster) 题意 链接:here 有\(n ...

  6. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

    全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...

  7. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster [广义后缀自动机]

    JZPGYZ - Sevenk Love Oimaster     Oimaster and sevenk love each other.       But recently,sevenk hea ...

  8. bzoj 2780 [Spoj]8093 Sevenk Love Oimaster

    LINK:Sevenk Love Oimaster 询问一个模式串在多少个文本串中出现过. 考虑广义SAM 统计这种数量问题一般有三种做法. 一种 暴力bitset 这道题可能可以过? 一种 暴力跳p ...

  9. 【刷题】BZOJ 2780 [Spoj]8093 Sevenk Love Oimaster

    Description Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXu ...

随机推荐

  1. centos7 + VMware Workstation Pro

    centos7 + VMware Workstation Pro安装 centos 7 镜像文件 下载地址https://www.centos.org/download/ 笔者是使用的DVD ISO, ...

  2. IBatis学习

    (1)建立 SqlMap.config文件 <?xml version="1.0" encoding="utf-8" ?> <sqlMapCo ...

  3. my.conf 配置编码为utf-8

    MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码) MySQL的默认编码是Latin1,不支持中文,那么如何修改MySQL的默认编码呢,下面以设置UTF-8为例来说明. M ...

  4. UnityShader:HSV(色相,饱和度,亮度)转换

    http://blog.csdn.net/costfine/article/details/46930473 发现其实美术调整颜色的时候大部分都是调整的HSV,因为可以方便的分别调整色相(hue).饱 ...

  5. Quartz.NET总结(二)CronTrigger和Cron表达式

    Quartz.NET的任务调度,主要就是依靠CronTrigger和Cron表达式.Cron是已经在UNIX存在了很长一段时间,它有着强大和可靠的调度能力.CronTrigger类也正是是基于Cron ...

  6. flask安装及第一个程序

    1.flask是一个轻量级的python web框架 ·1.Flask 依赖两个外部库: Jinja2 模板引擎和 Werkzeug WSGI 套件 ·2.安装: # easy_install fla ...

  7. 为什么要使用class.forname在DriverManager.getConnection之前

    JDBC在getConnection之前为什么要调用Class.forName 获取一个数据库连接的通用模板如下: String driver = "oracle.jdbc.OracleDr ...

  8. ] 解决myeclipse中新建javaweb工程,无法使用Web App Libraries问题

    ] 解决myeclipse中新建javaweb工程,无法使用Web App Libraries问题 标签: myeclipsejavawebWeb App Libraries 2013-10-16 1 ...

  9. xheditor编辑器的使用

    xheditor编辑器的使用 一个博客.cms网站都一定会用到一个html编辑器,刚好xmfdsh在做网站时候需要用到这类编辑器,在对比了之后,发现其实差不了多少,刚好一个不错的friend在用xhe ...

  10. jQuery checkbox 所有 全选、全不选、是否选中等

    下面是网络收集: jquery判断checked的三种方法:.attr('checked):   //看版本1.6+返回:”checked”或”undefined” ;1.5-返回:true或fals ...