您的位置:首页 > 其它

zoj 3228 Searching the String 【AC自动机】

2015-10-16 22:48 323 查看
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 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次查询,每次查询给出一个模式串和相应标记op——若op为0说明查询文本串时模式串允许重叠,若op为1说明查询时模式串不能重叠。对每次查询,输出当前模式串在文本串中出现的个数。

用last[i]记录Trie节点i在上一次匹配时所对应的字符在文本串中的位置。

用pos[i]记录Trie节点i所对应的字符在模式串中的位置。

思路:没有重叠的判断 —— 当前字符位置 - last[当前节点] <= pos[当前节点]。

在构造Trie的时候,可以记录每个串的结束点。构造好状态转移图后,每次统计节点的值。最后输出串对应结束点的值就可以了。

因为少了对pos[]的初始化,MLE到死。。。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 600000+10
#define INF 0x3f3f3f3f
using namespace std;
int ans[MAXN][2];
int node[100000+10];//记录串在Trie中的结束点
int n;
int op[100000+10];
struct Trie
{
int next[MAXN][26], fail[MAXN];
int pos[MAXN];//记录当前节点的字符在模式串的位置
int last[MAXN];//记录当前节点上一个匹配的位置
int L, root;
int newnode()
{
for(int i = 0; i < 26; i++)
next[L][i] = -1;
//End[L++] = 0;
pos[L++] = 0;//这里忘写了,MLE到死。。。
return L-1;
}
void init()
{
L = 0;
root = newnode();
}
void Insert(char *s, int id)
{
int now = root;
for(int i = 0; s[i]; i++)
{
if(next[now][s[i]-'a'] == -1)
next[now][s[i]-'a'] = newnode();
now = next[now][s[i]-'a'];
pos[now] = i+1;
}
node[id] = now;//记录串结束点
}
void Build()
{
queue<int> Q;
fail[root] = root;
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 now = Q.front();
Q.pop();
for(int i = 0; i < 26; i++)
{
if(next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
void solve(char *s)
{
memset(last, -1, sizeof(last));
memset(ans, 0, sizeof(ans));
int len = strlen(s);
int now = root;
for(int i = 0; i < len; i++)
{
now = next[now][s[i]-'a'];
int temp = now;
while(temp != root)
{
ans[temp][0]++;
if(i - last[temp] >= pos[temp])
{
ans[temp][1]++;
last[temp] = i;
}
temp = fail[temp];
}
}
}
};
Trie ac;
char str[100000+10];
char s[10];
int main()
{
int k = 1;
while(scanf("%s", str) != EOF)
{
ac.init(); scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d%s", &op[i], s);
ac.Insert(s, i);
}
ac.Build(); ac.solve(str);
printf("Case %d\n", k++);
for(int i = 0; i < n; i++)
printf("%d\n", ans[node[i]][op[i]]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: