ZOJ - 3228 Searching the String (AC自己主动机)

Description

Little jay really hates to deal with string. But moondy likes it very much, and she‘s so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay another problem, poor jay finally broke out and cried, " Who
can help me? I‘ll bg him! "

So what is the problem this time?

First, moondy gave jay a very long string A. Then she gave him a sequence of very short substrings, and asked him to find how many times each substring appeared in string A. What‘s more, she would denote whether or not founded appearances
of this substring are allowed to overlap.

At first, jay just read string A from begin to end to search all appearances of each given substring. But he soon felt exhausted and couldn‘t go on any more, so he gave up and broke out this time.

I know you‘re a good guy and will help with jay even without bg, won‘t you?

Input

Input consists of multiple cases( <= 20 ) and terminates with end of file.

For each case, the first line contains string A ( length <= 10^5 ). The second line contains an integer N ( N <= 10^5 ), which denotes the number of queries. The next N lines, each with an integer type and a string a (
length <= 6 ), type = 0 denotes substring a is allowed to overlap and type = 1 denotes not. Note that all input characters are lowercase.

There is a blank line between two consecutive cases.

Output

For each case, output the case number first ( based on 1 , see Samples ).

Then for each query, output an integer in a single line denoting the maximum times you can find the substring under certain rules.

Output an empty line after each case.

Sample Input

ab
2
0 ab
1 ab

abababac
2
0 aba
1 aba

abcdefghijklmnopqrstuvwxyz
3
0 abc
1 def
1 jmn

Sample Output

Case 1
1
1

Case 2
3
2

Case 3
1
1
0

题意:先给你一个字符串,然后给你若干个子串,0代表能够重叠,1代表不能重叠,求出如今母串的次数。

思路:AC自己主动机,多一个推断的是假设这个子串与上次出现的次数大于子串长度的话。就代表这次不是重叠的了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
typedef long long ll;
using namespace std;
const int maxn = 600010;

int wordend[100010];
struct Trie {
    int nxt[maxn][26], fail[maxn], deep[maxn];
    int root, sz;
    int cnt[maxn][2], last[maxn];

    int newNode() {
        for (int i = 0; i < 26; i++)
            nxt[sz][i] = -1;
        deep[sz++] = 0;
        return sz - 1;
    }

    void init() {
        sz = 0;
        root = newNode();
    }

    void insert(char str[], int num) {
        int u = root;
        for (int i = 0; str[i]; i++) {
            int tmp = str[i] - ‘a‘;
            if (nxt[u][tmp] == -1)
                nxt[u][tmp] = newNode();
            deep[nxt[u][tmp]] = deep[u] + 1;
            u = nxt[u][tmp];
        }
        wordend[num] = u;
    }

    void build() {
        queue<int> q;
        fail[root] = root;
        int u = root;
        for (int i = 0; i < 26; i++) {
            if (nxt[u][i] == -1)
                nxt[u][i] = root;
            else {
                fail[nxt[u][i]] = root;
                q.push(nxt[u][i]);
            }
        }

        while (!q.empty()) {
            u = q.front();
            q.pop();
            for (int i = 0; i < 26; i++) {
                if (nxt[u][i] == -1)
                    nxt[u][i] = nxt[fail[u]][i];
                else {
                    fail[nxt[u][i]] = nxt[fail[u]][i];
                    q.push(nxt[u][i]);
                }
            }
        }
    }

    void query(char *buf) {
        for (int i = root; i < sz; i++) {
            cnt[i][0] = 0;
            cnt[i][1] = 0;
            last[i] = -1;
        }

        int u = root;
        for (int i = 0; buf[i]; i++) {
            u = nxt[u][buf[i]-‘a‘];
            int tmp = u;
            while (tmp != root) {
                cnt[tmp][0]++;
                if (i - last[tmp] >= deep[tmp]) {
                    cnt[tmp][1]++;
                    last[tmp] = i;
                }
                tmp = fail[tmp];
            }
        }
    }
} ac;

char buf[100010], word[10];
int type[100010];

int main() {
    int n, cas = 1;
    while (scanf("%s", buf) != EOF) {
        scanf("%d", &n);
        ac.init();
        for (int i = 0; i < n; i++) {
            scanf("%d%s", &type[i], word);
            ac.insert(word, i);
        }
        ac.build();
        ac.query(buf);
        printf("Case %d\n", cas++);
        for (int i = 0; i < n; i++)
            printf("%d\n", ac.cnt[wordend[i]][type[i]]);
        printf("\n");
    }
    return 0;
}
时间: 2024-10-15 05:06:48

ZOJ - 3228 Searching the String (AC自己主动机)的相关文章

zoj 3228 Searching the String(AC自动机)

题目连接:zoj 3228 Searching the String 题目大意:给定一个字符串,然后现在有N次询问,每次有一个type和一个子串,问说子串在字符串中出现几次,type 为0时为可重叠,为1时为不可重叠. 解题思路:不过没有type=1的限制,那么就是普通的AC自动机匹配问题,对于不可重叠问题,可以对于每个节点记录 一下上一次匹配到的pos,用当前匹配的i减掉pos看有没有超过长度,有超过即为合法匹配,否则忽略. 题目中有很多相同的子串,一开始我用jump数组用类似链表的形式记录每

ZOJ 3228 Searching the String AC自动机的不重复匹配

这个判断方法真的没想到... 对于在S中匹配M,如果M上一次的匹配位置pre与这一次的匹配位置now满足now-pre >= M.length,则加1. 这个判断太跳了233 . #include <iostream> #include<time.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<string> #include<map&

ZOJ 3228 Searching the String (AC自己主动机)

题目链接:Searching the String 解析:给一个长串.给n个不同种类的短串.问分别在能重叠下或者不能重叠下短串在长串中出现的次数. 能重叠的已经是最简单的AC自己主动机模板题了. 不能重叠的记录一下每一个匹配的串的起始位置保证不重叠就可以. AC代码: #include <bits/stdc++.h> using namespace std; struct Trie{ int next[600010][26], fail[600010], deep[600010]; int r

ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)

题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到bcd[i][j]表示状态i,加了数字j以后到达的状态.为-1表示不能转移 然后就是数位DP了 注意记录为0的状态 AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <algo

zoj 3228 - Searching the String

题目:给你一个长目标串str,和一些模式串,求每个串出现的次数.模式串有可覆盖和不可覆盖两种. 分析:字符串,多串匹配,AC自动机.由于数据有可能不允许相交,所以记录上一个的结束位置. 题目的数据比较猥琐,可能相同,采用一个 Fath域记录第一次出现的 id,计算一次就可以了. 说明:注意数据有相同的串:该题可以使用 RK算法+二分. #include <stdio.h> #include <stdlib.h> #include <string.h> char Word

Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)

标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp[i][j][k] 表示如今构造到了长度 i . 此时的我们把当前字符放在j节点.而且满足了k状态.k是一个10位的2进制状态压缩. 注意这道题上有坑就是一个序列可能有多个权值. 所以不能直接赋值.须要用位或. #include <cstdio> #include <iostream>

zoj 3430 Detect the Virus(AC自己主动机)

Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his computer is extremely slow. After several hours' work, he finally found that it was a virus that made his poor computer slow and the virus was activated

字符串算法之 AC自己主动机

近期一直在学习字符串之类的算法,感觉BF算法,尽管非常easy理解,可是easy超时,全部就想学习其它的一些字符串算法来提高一下,近期学习了一下AC自己主动机.尽管感觉有所收获,可是还是有些朦胧的感觉,在此总结一下,希望大家不吝赐教. 一.AC自己主动机的原理: Aho-Corasick automaton.该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之中的一个. 一个常见的样例就是给出N个单词,在给出一段包括m个字符的文章,让你找出有多少个单词在这文章中出现过,.要搞懂AC自己主动

【UVA】1449-Dominating Patterns(AC自己主动机)

AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge&Itemid=8&page=show_problem&problem=4195" style="font-size:13.3333330154419px; margin:0px; padding:0px; color:rgb(153,0,0); text-decoratio