ZOJ 3228 Searching the String(AC自动机统计可重叠和不可重叠的单词情况)

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 Nlines, 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

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

 题意:

给定一个长度为N(N <= 105)的目标串,然后再给定M(M <= 105)个长度不大于6的字符串,问这些字符串在目标串的出现次数(分可重叠和不可重叠两种)。

 题解:

我们对于每一个单词节点先设上cnt[N][2],表示可重叠cnt[][0]和不可重叠cnt[][1]的情况个数(这样做可以处理模式串有重复的情况)

我们首先肯定将M个串作为模式串建立AC自动机,对于可重叠的情况直接询问即可。

对于不可重叠的情况,我们需要记录每个串的长度Len和之前这个串匹配到的最大位置Pos,对于当前位置Pos1,如果Pos+ Len <= Pos1,那么认为和之前的一次匹配没有重叠,计数累加cnt[i][1]++,并且更新Pos = Pos1。

然后,我们在字典树上查询cnt即可。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
 
const int maxnode=610000;
const int sigma_size=26;
int ans=0;
struct AC_Automata
{
    int ch[maxnode][sigma_size];
    int val[maxnode];   // 以单词结尾的个数
    int f[maxnode];     // fail函数
    int last[maxnode];  // last[i]=j表j节点表示的单词是i节点单词的后缀,且j节点是单词节点
    int sz;
    int cnt[maxnode][2];//非单词节点vis=0,单词节点vis=1.如果用find找到了单词i节点,那么vis=0.
    int pos[maxnode],len[maxnode];
    //初始化0号根节点的相关信息
    void init()
    {
        sz=1;
        memset(ch[0],0,sizeof(ch[0]));
        val[0]=0;
        last[0]=f[0]=0;
        memset(cnt,0,sizeof(cnt));
        memset(pos,0,sizeof(pos));
        memset(len,0,sizeof(len));
    }
 
    //insert负责构造ch与val数组
    //插入字符串,v必须非0表示一个单词节点
    void insert(char *s)
    {
        int n=strlen(s),u=0;
        for(int i=0; iq;
        last[0]=f[0]=0;
        for(int i=0; i0
    void print(int i,int pos1)
    {
        if(val[i])
        {
        	cnt[i][0]++;
        	if(pos[i]+len[i]<=pos1) ///判断是否有重叠
			{
				pos[i]=pos1;
				cnt[i][1]++;
			}
            print(last[i],pos1);
        }
    }
 
    // 在s中找出 出现了哪几个模板单词
    void find(char *s)
    {
        int n=strlen(s),j=0;
        for(int i=0; i

 

你可能感兴趣的:(模板,字符串——AC自动机)