博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ac自动机
阅读量:4480 次
发布时间:2019-06-08

本文共 4119 字,大约阅读时间需要 13 分钟。

给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过。

#include 
const int maxn = 3*1e6+5, M = 1e6+7;using namespace std;int siz, cur[maxn];bool flag[maxn];struct ac{ struct Sta{ int son[26], fail; int cnt, rr; }sta[maxn]; void init(){ siz = 1; } void insert(char *S, int tot){ int len = strlen(S); int now = 0; for(int i = 0; i < len; i++){ char c = S[i]; if(sta[now].son[c-'a'] == 0) sta[now].son[c-'a'] = siz++; now = sta[now].son[c-'a']; } sta[now].cnt++; }; void build(){ queue
Q; Q.push(0); while(!Q.empty()){ int u = Q.front(); Q.pop(); for(int i = 0; i < 26; i++){ if(sta[u].son[i]){ if(u == 0)sta[sta[u].son[i]].fail = 0; else sta[sta[u].son[i]].fail = sta[sta[u].fail].son[i]; Q.push(sta[u].son[i]); } else sta[u].son[i] = sta[sta[u].fail].son[i]; } } } int match(char *S){ int now = 0, res = 0; int len = strlen(S); for(int i = 0; i < len; i++){ char c = S[i]; now = sta[now].son[c - 'a']; for(int t = now; t && !flag[t]; t = sta[t].fail) res += sta[t].cnt, flag[t] = 1; } return res; } int Query(int t){ return sta[cur[t]].rr; }}Tr;char a[maxn][55];char s[M];char S[M];int main(){ int n ,m; scanf("%d", &n); Tr.init(); for(int i = 1; i <= n; i++){ // scanf("\n"); scanf("%s", s); Tr.insert(s, i); } Tr.build(); // scanf("\n"); scanf("%s", S); printf("%d\n", Tr.match(S)); return 0;}

有 NN 个由小写字母组成的模式串以及一个文本串 TT 。每个模式串可能会在文本串中出现多次。你需要找出哪些模式串在文本串 TT 中出现的次数最多。

#include 
const int maxn = 3*1e6+5, M = 1e6+7;using namespace std;int siz;bool flag[maxn];struct Result{ int sum , id;}ans[M];struct ac{ struct Sta{ int son[26], fail; int end, rr; }sta[maxn]; void clear(int x){ memset(sta[x].son, 0, sizeof(sta[x].son)); sta[x].end = sta[x].fail = 0; } void insert(char *S, int tot){ int len = strlen(S); int now = 0; for(int i = 0; i < len; i++){ char c = S[i]; if(sta[now].son[c-'a'] == 0){ sta[now].son[c-'a'] = ++siz; clear(siz); } now = sta[now].son[c-'a']; } sta[now].end = tot; }; void build(){ queue
Q; Q.push(0); while(!Q.empty()){ int u = Q.front(); Q.pop(); for(int i = 0; i < 26; i++){ if(sta[u].son[i]){ if(u == 0)sta[sta[u].son[i]].fail = 0; else sta[sta[u].son[i]].fail = sta[sta[u].fail].son[i]; Q.push(sta[u].son[i]); } else sta[u].son[i] = sta[sta[u].fail].son[i]; } } } void match(char *S){ int now = 0, res = 0; int len = strlen(S); for(int i = 0; i < len; i++){ char c = S[i]; now = sta[now].son[c - 'a']; for(int t = now; t; t = sta[t].fail) ans[sta[t].end].sum++; } }}Tr;char a[M][75];char S[M];bool cmp(Result a, Result b){ return a.sum == b.sum ? a.id < b.id : a.sum > b.sum;}int main(){ int n ,m; while(scanf("%d", &n) == 1){ if(!n)break; siz = 0; for(int i = 1; i <= n; i++){ ans[i].id = i; ans[i].sum = 0; } Tr.clear(0); for(int i = 1; i <= n; i++){ scanf("%s", a[i]); Tr.insert(a[i], i); } Tr.build(); scanf("%s", S); Tr.match(S); sort(ans+1, ans+1+n, cmp); printf("%d\n", ans[1].sum); for(int i = 1; i <= n; i++){ if(ans[i].sum < ans[1].sum)break; printf("%s\n", a[ans[i].id]); } } return 0;}

 

转载于:https://www.cnblogs.com/EdSheeran/p/9216762.html

你可能感兴趣的文章
课堂final发布
查看>>
解锁scott用户
查看>>
多态的理解
查看>>
AspNet Core 发布到Linux系统和发布IIS 注意项
查看>>
Windows添加.NET Framework 3.0 NetFx3 失败 - 状态为:0x800f0950
查看>>
隐藏显示终端的光标(shell echo,linux c printf)
查看>>
SQL Server 存储过程
查看>>
JSP 标准标签库(JSTL)(JSP Standard Tag Library)
查看>>
导入项目遇到的问题: Some projects cannot be imported because they already exist in the workspace....
查看>>
华为:字符集合
查看>>
面向对象 【抽象类】【接口】【构造函数】【静态】
查看>>
结构化方法与面向对象方法的比较
查看>>
影响各类服务器性能瓶颈的因素【转】
查看>>
Jenkins
查看>>
jboss5 启动时报HsqlException:length must be specified in type definition:VARBINARY错误
查看>>
转载:让理科生沉默,让文科生流泪的综合题
查看>>
程序员7-2007-2010
查看>>
Android分类前言
查看>>
oracle中字符串的大小比较,字符串与数字的比较和运算
查看>>
2018年东北农业大学春季校赛 wyh的矩阵
查看>>