AC自动机模板
2015-08-11 23:45
330 查看
AC自动机模板……
/*
* AC自动机模板
* 使用方法:
* 1、init() : 初始化函数
* 2、insert(str) : 插入字符串函数
* 3、build() : 构建ac自动机
* 4、query(str) : 返回出现的字符串个数
*
* 使用需注意事项:
* 1、注意输入的字符的范围,需对Next和其二维大小及相关参数进行更改
* 2、注意Next、Fail和End数组的大小,防止超内存过数组越界
* 3、根据实际情况对模板中“ buf[i] - 'a' ” 进行更改,否则可能会数组越界
* 此模板默认相关设置:
* 1、短字符串总长度不超过500000
* 2、输入字符串的内容只由小写字母a~z构成
* 3、query()函数只统计匹配的个数
* PS:上述都需根据需要自己更改!!!
*/
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
int Next[500010][26], Fail[500010], End[500010];
int root, L;
int newnode() {
for (int i = 0; i < 26; i++)
Next[L][i] = -1;
End[L++] = 0;
return L - 1;
}
void init() {
L = 0;
root = newnode();
}
void insert(char buf[]) {
int len = strlen(buf);
int now = root;
for (int i = 0; i < len; i++) {
if (Next[now][buf[i] - 'a'] == -1)
Next[now][buf[i] - 'a'] = newnode();
now = Next[now][buf[i] - 'a'];
}
End[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]);
}
}
}
}
int query(char buf[]) {
int len = strlen(buf);
int now = root;
int res = 0;
for (int i = 0; i < len; i++) {
now = Next[now][buf[i] - 'a'];
int temp = now;
while ( temp != root ) {
res += End[temp];
End[temp] = 0;
temp = Fail[temp];
}
}
return res;
}
int main() {
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
return 0;
}
/*
* AC自动机模板
* 使用方法:
* 1、init() : 初始化函数
* 2、insert(str) : 插入字符串函数
* 3、build() : 构建ac自动机
* 4、query(str) : 返回出现的字符串个数
*
* 使用需注意事项:
* 1、注意输入的字符的范围,需对Next和其二维大小及相关参数进行更改
* 2、注意Next、Fail和End数组的大小,防止超内存过数组越界
* 3、根据实际情况对模板中“ buf[i] - 'a' ” 进行更改,否则可能会数组越界
* 此模板默认相关设置:
* 1、短字符串总长度不超过500000
* 2、输入字符串的内容只由小写字母a~z构成
* 3、query()函数只统计匹配的个数
* PS:上述都需根据需要自己更改!!!
*/
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
int Next[500010][26], Fail[500010], End[500010];
int root, L;
int newnode() {
for (int i = 0; i < 26; i++)
Next[L][i] = -1;
End[L++] = 0;
return L - 1;
}
void init() {
L = 0;
root = newnode();
}
void insert(char buf[]) {
int len = strlen(buf);
int now = root;
for (int i = 0; i < len; i++) {
if (Next[now][buf[i] - 'a'] == -1)
Next[now][buf[i] - 'a'] = newnode();
now = Next[now][buf[i] - 'a'];
}
End[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]);
}
}
}
}
int query(char buf[]) {
int len = strlen(buf);
int now = root;
int res = 0;
for (int i = 0; i < len; i++) {
now = Next[now][buf[i] - 'a'];
int temp = now;
while ( temp != root ) {
res += End[temp];
End[temp] = 0;
temp = Fail[temp];
}
}
return res;
}
int main() {
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
return 0;
}
相关文章推荐
- 数据库链接字符串查询网站
- 动易2006序列号破解算法公布
- Flex字符串比较 还有Flex字符串操作
- Ruby实现的矩阵连乘算法
- Ruby中创建字符串的一些技巧小结
- ASP下经常用的字符串等函数参考资料
- 将字符串小写转大写并延时输出的批处理代码
- 将字符串转换成System.Drawing.Color类型的方法
- C#插入法排序算法实例分析
- Lua源码中字符串类型的实现
- Lua性能优化技巧(四):关于字符串
- 字符串聚合函数(去除重复值)
- Ruby中的字符串编写示例
- 总结的5个C#字符串操作方法分享
- sqlserver中求字符串中汉字的个数的sql语句
- sql server字符串非空判断实现方法
- VBS的字符串及日期操作相关函数
- C#实现将千分位字符串转换成数字的方法
- jquery 删除字符串最后一个字符的方法解析
- 超大数据量存储常用数据库分表分库算法总结