zoj 3228 Searching the String(AC自动机)
2014-11-10 20:46
232 查看
题目连接:zoj 3228 Searching the String
题目大意:给定一个字符串,然后现在有N次询问,每次有一个type和一个子串,问说子串在字符串中出现几次,type
为0时为可重叠,为1时为不可重叠。
解题思路:不过没有type=1的限制,那么就是普通的AC自动机匹配问题,对于不可重叠问题,可以对于每个节点记录
一下上一次匹配到的pos,用当前匹配的i减掉pos看有没有超过长度,有超过即为合法匹配,否则忽略。
题目中有很多相同的子串,一开始我用jump数组用类似链表的形式记录每个节点的询问id,然后在匹配过程每次都去
+1,结果一直超时,说明重复串非常多。于是我改成的在最后要输出的时候再去查找,也就是每个节点仍是一个id,其
他的相同单词节点jump全部指向id。
题目大意:给定一个字符串,然后现在有N次询问,每次有一个type和一个子串,问说子串在字符串中出现几次,type
为0时为可重叠,为1时为不可重叠。
解题思路:不过没有type=1的限制,那么就是普通的AC自动机匹配问题,对于不可重叠问题,可以对于每个节点记录
一下上一次匹配到的pos,用当前匹配的i减掉pos看有没有超过长度,有超过即为合法匹配,否则忽略。
题目中有很多相同的子串,一开始我用jump数组用类似链表的形式记录每个节点的询问id,然后在匹配过程每次都去
+1,结果一直超时,说明重复串非常多。于是我改成的在最后要输出的时候再去查找,也就是每个节点仍是一个id,其
他的相同单词节点jump全部指向id。
#include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1e5+5; const int sigma_size = 26; const int inf = 0x3f3f3f3f; struct Aho_Corasick { int sz, g[maxn * 6][sigma_size]; int tag[maxn * 6], jump[maxn]; int len[maxn], dis[maxn], c[maxn][2]; int fail[maxn * 6], last[maxn * 6]; void init(); int idx(char ch); void insert(char* str, int k); void getFail(); void match(char* str); void put(int d, int u); }AC; int N, vis[maxn]; char s[maxn]; int main () { char w[10]; int cas = 1, x; while (scanf("%s", s) == 1) { AC.init(); scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%d%s", &vis[i], w); AC.insert(w, i); } AC.getFail(); AC.match(s); printf("Case %d\n", cas++); for (int i = 1; i <= N; i++) printf("%d\n", AC.c[AC.jump[i]][vis[i]]); printf("\n"); } return 0; } void Aho_Corasick::init() { sz = 1; tag[0] = 0; memset(g[0], 0, sizeof(g[0])); memset(c, 0, sizeof(c)); memset(dis, -1, sizeof(dis)); } int Aho_Corasick::idx(char ch) { return ch - 'a'; } void Aho_Corasick::put(int d, int u) { int p = tag[u]; if (dis[p] == -1 || d - dis[p] >= len[p]) { dis[p] = d; c[p][1]++; } c[p][0]++; if (last[u]) put(d, last[u]); } void Aho_Corasick::insert(char* str, int k) { int u = 0, n = strlen(str); for (int i = 0; i < n; i++) { int v = idx(str[i]); if (g[u][v] == 0) { tag[sz] = 0; memset(g[sz], 0, sizeof(g[sz])); g[u][v] = sz++; } u = g[u][v]; } len[k] = n; if (tag[u]) jump[k] = tag[u]; else jump[k] = tag[u] = k; } void Aho_Corasick::match(char* str) { int n = strlen(str), u = 0; for (int i = 0; i < n; i++) { int v = idx(str[i]); while (u && g[u][v] == 0) u = fail[u]; u = g[u][v]; if (tag[u]) put(i, u); else if (last[u]) put(i, last[u]); } } void Aho_Corasick::getFail() { queue<int> que; for (int i = 0; i < sigma_size; i++) { int u = g[0][i]; if (u) { fail[u] = last[u] = 0; que.push(u); } } while (!que.empty()) { int r = que.front(); que.pop(); for (int i = 0; i < sigma_size; i++) { int u = g[r][i]; if (u == 0) { g[r][i] = g[fail[r]][i]; continue; } que.push(u); int v = fail[r]; while (v && g[v][i] == 0) v = fail[v]; fail[u] = g[v][i]; last[u] = tag[fail[u]] ? fail[u] : last[fail[u]]; } } }
相关文章推荐
- ZOJ 3228 Searching the String(AC自动机)
- zoj 3228 Searching the String (ac自动机)
- ZOJ 3228 Searching the String(AC自动机)
- zoj 3228 Searching the String(AC自动机基本应用)
- ZOJ 3228 Searching the String(AC自动机)
- ZOJ3228---Searching the String(AC自动机)
- ZOJ - 3228 Searching the String (AC自动机)
- ZOJ 3228 Searching the String AC自动机的不重复匹配
- ZOJ 3228 Searching the String(AC自动机)
- Searching the String - ZOJ 3228(ac自动机)
- ZOJ 3228 Searching the String(自动机)
- ZOJ 3328 Searching the String (AC自动机)
- ZOJ - 3228 Searching the String (AC自己主动机)
- zoj 3228:Searching the String
- zoj 3228 Searching the String 【AC自动机】
- 【ZOJ】3228 Searching the String
- zoj 3228 - Searching the String
- zoj 3228 Searching the String
- ZOJ 题目3228 Searching the String(AC自动机,子川匹配个数)
- zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)