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
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.
题意:给出一个主串,和n个模式串,求模式串在主串中出现次数(允许覆盖或不允许覆盖)。
思路:改板子。。
//hdu2222
#include
#include
#include
#include
#define TREE_WIDTH 26
#define nullptr 0
using namespace std;
struct Node
{
int end; //重复的模式串个数(此题不需要)
int end1; //允许覆盖的匹配结果数
int end2; //不允许覆盖的匹配结果数
int lastvis; //上次匹配位置的最后一个字母位置
int dep; //模式串长
Node * fail;
Node * next[TREE_WIDTH];
Node()
{
this->end = 0;
this->end1 = 0;
this->end2 = 0;
this->lastvis=-1;
this->dep=0;
this->fail = nullptr;
for (int i = 0; i < TREE_WIDTH; i++)
this->next[i] = nullptr;
}
};
class AC
{
private:
Node * root;
public:
AC();
~AC();
void destroy(Node * t);
void add(char * s); //添加模式串
void build_fail_pointer(); //建立失配指针
int ac_automaton(char * t);
int ac_find_appear_time(char*s,int op);
};
AC::AC()
{
root = new Node;
}
AC::~AC()
{
destroy(root);
}
void AC::destroy(Node * t)
{
for (int i = 0; i < TREE_WIDTH; i++)
if (t->next[i])
destroy(t->next[i]);
delete t;
}
void AC::add(char * s)
{
Node * t = root;
while (*s)
{
if (t->next[*s - 'a'] == nullptr)
t->next[*s - 'a'] = new Node;
t->next[*s-'a']->dep=t->dep+1;
t = t->next[*s - 'a'];
s++;
}
t->end++; // 假设单词可重复
}
void AC::build_fail_pointer()
{
queue Q;
for (int i = 0; i < TREE_WIDTH; i++)
{
if (root->next[i])
{
Q.push(root->next[i]);
root->next[i]->fail = root;
}
}
Node * parent = nullptr;
Node * son = nullptr;
Node * p = nullptr;
while (!Q.empty())
{
parent = Q.front();
Q.pop();
for (int i = 0; i < TREE_WIDTH; i++)
{
if (parent->next[i])
{
Q.push(parent->next[i]);
son = parent->next[i];
p = parent->fail;
while (p)
{
if (p->next[i])
{
son->fail = p->next[i];
break;
}
p = p->fail;
}
if (!p) son->fail = root;
}
}
}
}
int AC::ac_automaton(char * t)
{
int ans = 0;
int pos;
Node * pre = root;
Node * cur = nullptr;
int len=0; //到达匹配串的位置长度
while (*t)
{
pos = *t - 'a';
if (pre->next[pos])
{
cur = pre->next[pos];
while (cur != root)
{
if (cur->end >0) //此处一个模式串结尾
{
ans += cur->end;
cur->end1++; //允许覆盖
if(cur->lastvis==-1||cur->lastvis+cur->dep<=len) //不允许覆盖
{
cur->end2++;
cur->lastvis=len;
}
}
cur = cur->fail;
}
pre = pre->next[pos];
t++;
len++;
}
else
{
if (pre == root)
{
t++;
len++;
}
else
pre = pre->fail;
}
}
return ans;
}
int AC::ac_find_appear_time(char*s,int op)
{
Node * t = root;
while (*s)
{
t = t->next[*s - 'a'];
s++;
}
if(op==0)return t->end1;
else return t->end2;
}
int main()
{
int n;
char s[100010][15];
int op[100010];
char t[100010];
int cas=0;
while(~scanf("%s",t))
{
memset(s,0,sizeof(s));
memset(op,0,sizeof(op));
AC tree;
scanf("%d",&n);
for(int i=0;i