ZOJ 3228 Searching the String (AC自动机)

来源:互联网 发布:淘宝客链接手机端转换 编辑:程序博客网 时间:2024/05/18 07:16

题意:

给你一个模板串, 和n 个要匹配的串, 匹配串有两种类型, 第一种 可以在模板串中 重叠 出现, 另一种不可以重叠, 问每个串的两种形式 所出现的数量。

思路:

很明显ac自动机。

我们先把所有匹配串插到自动机中, 第一种很简单 ,可以重叠出现, 直接循环一边模板串, 不断的走fail 指针 , 找到一个 加一个就行了。

不可重叠也很简单, 直接在开一个last数组 ,表示这个单词 上一次出现是啥时候, 只要两次位置差不小于单词长度即可。

一开始给每个字典树的节点 开了个vector, 记录这个节点中都有哪些输入的单词, 但是T了。

不如 把所有情况都查找一边,  无论输入的单词是哪种类型, 我们把两种类型都查找一遍即可。  只是走一遍ac自动机而已。

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int maxn = 100000 + 10;const int maxt = maxn * 6;char s[maxn];int type[maxn]; /// 每个输入的单词  的类型 0/1int ans[maxt][2];struct Trie{    int L, root;    int next[maxt][26];    int fail[maxt];    int last[maxt]; ///记录每个单词 上一次出现是啥时候    int d[maxt]; /// 记录字典树中的节点 到根的距离(即单词的长度)    int flag[maxt]; /// 是否是单词    int pos[maxt]; /// 每个输入的单词 在字典树中的位置。    void init(){        L = 0;        root = newnode();    }    int newnode(){        for (int i = 0; i < 26; ++i){            next[L][i] = -1;        }        d[L] = 0;        flag[L] = 0;        ans[L][0] = ans[L][1] = 0;        return L++;    }    void insert(char* s, int id){        int len = strlen(s);        int nod = root;        for (int i = 0; i < len; ++i){            int id = s[i] - 'a';            if (next[nod][id] == -1){                next[nod][id] = newnode();            }            d[next[nod][id] ] = d[nod] + 1;            nod = next[nod][id];        }        pos[id] = nod;        flag[nod] = 1;    }    void bfs(){        fail[root] = root;        queue<int>q;        for (int i = 0; i < 26; ++i){            if (next[root][i] == -1){                next[root][i] = root;            }            else {                fail[next[root][i] ] = root;                q.push(next[root][i]);            }        }        while(!q.empty()){            int u = q.front(); q.pop();            for (int i = 0; i < 26; ++i){                if (next[u][i] == -1){                    next[u][i] = next[fail[u] ][i];                }                else {                    fail[next[u][i] ] = next[fail[u] ][i];                    q.push(next[u][i]);                }            }        }    }    void solve(char* s,int n){        int len = strlen(s);        int nod = root;        for (int i = 0; i < L; ++i){            last[i] = -1;        }        for (int i = 0; i < len; ++i){            int id = s[i] - 'a';            nod = next[nod][id];            int tmp = nod;            while(tmp != root){                if (flag[tmp]){                    ans[tmp][0]++;                    if (last[tmp] == -1 || i - last[tmp] >= d[tmp]){                        last[tmp] = i;                        ans[tmp][1]++;                    }                }                tmp = fail[tmp];            }        }        for (int i = 0; i < n; ++i){            printf("%d\n", ans[pos[i] ][type[i] ]);        }    }}ac;char word[7];int main(){    int ks = 0;    while(~scanf("%s", s)){        int n;        scanf("%d",&n);        ac.init();        memset(ans, 0, sizeof ans);        for (int i = 0; i < n; ++i){            scanf("%d%s", &type[i], word);            ac.insert(word, i);        }        ac.bfs();        printf("Case %d\n", ++ks);        ac.solve(s, n);        printf("\n");    }    return 0;}



Searching the String

Time Limit: 7 Seconds      Memory Limit: 129872 KB

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 typeand 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

ab20 ab1 ababababac20 aba1 abaabcdefghijklmnopqrstuvwxyz30 abc1 def1 jmn

Sample Output

Case 111Case 232Case 3110

Hint

In Case 2,you can find the first substring starting in position (indexed from 0) 0,2,4, since they're allowed to overlap. The second substring starts in position 0 and 4, since they're not allowed to overlap.

For C++ users, kindly use scanf to avoid TLE for huge inputs.


Author: LI, Jie
Source: ZOJ Monthly, July 2009
Submit    Status


Searching the String

Time Limit: 7 Seconds      Memory Limit: 129872 KB

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 typeand 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

ab20 ab1 ababababac20 aba1 abaabcdefghijklmnopqrstuvwxyz30 abc1 def1 jmn

Sample Output

Case 111Case 232Case 3110

Hint

In Case 2,you can find the first substring starting in position (indexed from 0) 0,2,4, since they're allowed to overlap. The second substring starts in position 0 and 4, since they're not allowed to overlap.

For C++ users, kindly use scanf to avoid TLE for huge inputs.


Author: LI, Jie
Source: ZOJ Monthly, July 2009
Submit    Status