洛谷 - P3966 - 单词 - AC自动机
https://www.luogu.org/problemnew/show/P3966
因为文本串就是字典本身,所以这个和平时的AC自动机不太一样。平时的query要沿着fail树把子树的出现次数依次统计。但是这个的query就是对每个字典里的字符串搞一次。所以就直接按广搜的顺序反过来树形dp统计出子树中的出现次数,直接回答。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6;
const int MAXM = 2e2;
int idx[MAXM + 5];
struct Trie {
int trie[MAXN + 5][26], fail[MAXN + 5], cnt[MAXN + 5], que[MAXN + 5];
int root, top;
int newnode() {
for(int i = 0; i < 26; i++)
trie[top][i] = -1;
cnt[top++] = 0;
return top - 1;
}
void init() {
top = 0;
root = newnode();
}
void insert(char buf[], int id) {
int len = strlen(buf);
int cur = root;
for(int i = 0; i < len; i++) {
int u = buf[i] - 'a';
if(trie[cur][u] == -1)
trie[cur][u] = newnode();
cur = trie[cur][u];
cnt[cur]++;
}
idx[id] = cur;
}
void build() {
int head = 0, tail = 0;
fail[root] = root;
for(int i = 0; i < 26; i++) {
if(trie[root][i] == -1)
trie[root][i] = root;
else {
fail[trie[root][i]] = root;
que[tail++] = trie[root][i];
}
}
while(head < tail) {
int cur = que[head++];
for(int i = 0; i < 26; i++) {
if(trie[cur][i] == -1)
trie[cur][i] = trie[fail[cur]][i];
else {
fail[trie[cur][i]] = trie[fail[cur]][i];
que[tail++] = trie[cur][i];
}
}
}
for(int i = tail - 1; i >= 0; i--) {
cnt[fail[que[i]]] += cnt[que[i]];
}
}
};
char buf[MAXN + 5];
Trie ac;
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
//freopen("Yinku.out","w",stdout);
#endif // Yinku
int n;
scanf("%d", &n);
ac.init();
for(int i = 0; i < n; i++) {
scanf("%s", buf);
ac.insert(buf, i);
}
ac.build();
for(int i = 0; i < n; i++)
printf("%d\n", ac.cnt[idx[i]]);
}
这种写法是,每次文本串遇到一个模式串,就会给这个模式串+1次出现次数。问题是怎么优化重复的文本串呢?把文本串插入另一个trie里面就可以知道他们的重复数。然后每次就给每个模式串都贡献一个重数就等价了。这样最好理解。优化的地方在于同样的文本串不用多次匹配。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6;
const int MAXM = 2e2;
int ans[MAXM + 5];
int aidx[MAXN + 5][MAXM + 5];
struct Trie {
int trie[MAXN + 5][26], fail[MAXN + 5], cnt[MAXN + 5], que[MAXN + 5];
//本题多使用的一个是文本串重复计数器tcnt,但是在这里文本一定和模式一样就可以合并到模式重复计数器cnt里,一个文本防重vis
int tcnt[MAXN + 5];
bool vis[MAXN + 5];
int root, top;
int newnode() {
for(int i = 0; i < 26; i++)
trie[top][i] = -1;
cnt[top++] = 0;
return top - 1;
}
void init() {
top = 0;
root = newnode();
}
void insert(char buf[], int id) {
int len = strlen(buf);
int cur = root;
for(int i = 0; i < len; i++) {
int u = buf[i] - 'a';
if(trie[cur][u] == -1)
trie[cur][u] = newnode();
cur = trie[cur][u];
}
cnt[cur]++;
//本题中文本可以一并插入,干脆合并成一个cnt,可以在同一个trie中,否则应该分开插入两个trie
aidx[cur][cnt[cur]-1] = id;
}
void build() {
int head = 0, tail = 0;
fail[root] = root;
for(int i = 0; i < 26; i++) {
if(trie[root][i] == -1)
trie[root][i] = root;
else {
fail[trie[root][i]] = root;
que[tail++] = trie[root][i];
}
}
while(head < tail) {
int cur = que[head++];
for(int i = 0; i < 26; i++) {
if(trie[cur][i] == -1)
trie[cur][i] = trie[fail[cur]][i];
else {
fail[trie[cur][i]] = trie[fail[cur]][i];
que[tail++] = trie[cur][i];
}
}
}
}
void query(char buf[]) {
//该文本串匹配了多少次模式串
/*int len=strlen(buf);
int cur=root;
int res=0;
for(int i=0;i<len;i++){
cur=trie[cur][buf[i]-'a'];
int temp=cur;
while(temp!=root){
//这里既然是+=意思就是有多少个模式串匹配了都算,而且多次匹配就多次算,否则每次就把他清0就可以了
res+=cnt[temp];
temp=fail[temp];
}
}
return res;*/
//该种文本串对所有模式串贡献了几次?
//插好所有文本之后得到文本的重数,对每个文本在trie中O(n)找出他的重数以及他有没有被vis
//没有被vis的话就对每个匹配的模式贡献对应的重数
int len = strlen(buf);
int cur = root;
for(int i = 0; i < len; i++) {
cur = trie[cur][buf[i] - 'a'];
}
if(vis[cur])
return;
int cf = cnt[cur];
vis[cur]=1;
cur = root;
for(int i = 0; i < len; i++) {
cur = trie[cur][buf[i] - 'a'];
int temp = cur;
while(temp != root) {
for(int j = 0; j < cnt[temp]; j++) {
ans[aidx[temp][j]]+=cf;
}
temp = fail[temp];
}
}
}
};
char buf[MAXM+5][MAXN + 5];
Trie ac;
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
//freopen("Yinku.out","w",stdout);
#endif // Yinku
int n;
scanf("%d", &n);
ac.init();
for(int i = 0; i < n; i++) {
scanf("%s", buf[i]);
ac.insert(buf[i], i);
}
ac.build();
for(int i = 0; i < n; i++) {
ac.query(buf[i]);
}
for(int i = 0; i < n; i++)
printf("%d\n", ans[i]);
}
洛谷 - P3966 - 单词 - AC自动机的更多相关文章
- 洛谷P3966 单词 [TJOI2013] AC自动机
正解:AC自动机 解题报告: 传送门! 先来提供一个40pts错解QAQ 首先看到这题就会想到AC自动机板子题2鸭!然后就照着那题的套路打一下,随便改一点儿,简单来说就是每次经过一个节点都要++,然后 ...
- 洛谷P3808 & P3796 AC自动机模板
题目:P3808:https://www.luogu.org/problemnew/show/P3808 P3796:https://www.luogu.org/problemnew/show/P37 ...
- 洛谷.3121.审查(AC自动机 链表)
题目链接 //删掉一个单词需要前移一段位置,用链表维护就好了 复杂度O(sum(len)) #include <cstdio> #include <cstring> #defi ...
- 洛谷 - P2444 - 病毒 - AC自动机
https://www.luogu.org/problemnew/show/P2444 有点恶心,不太明白fail的意义. #include<bits/stdc++.h> using na ...
- 洛谷 P1101 单词方阵
题目链接 https://www.luogu.org/problemnew/show/P1101 题目描述 给一n×n的字母方阵,内可能蕴含多个"yizhong"单词.单词在方阵中 ...
- 洛谷 P1381 单词背诵
洛谷 P1381 单词背诵 洛谷传送门 题目描述 灵梦有n个单词想要背,但她想通过一篇文章中的一段来记住这些单词. 文章由m个单词构成,她想在文章中找出连续的一段,其中包含最多的她想要背的单词(重复的 ...
- 洛谷P1808 单词分类_NOI导刊2011提高(01) 字符串排序
洛谷P1808 单词分类_NOI导刊2011提高(01) 题目描述 Oliver为了学好英语决定苦背单词,但很快他发现要直接记住杂乱无章的单词非常困难,他决定对单词进行分类. 两个单词可以分为一类当且 ...
- 洛谷P3966 [TJOI2013]单词(AC自动机)
题目描述 小张最近在忙毕设,所以一直在读论文.一篇论文是由许多单词组成但小张发现一个单词会在论文中出现很多次,他想知道每个单词分别在论文中出现了多少次. 输入输出格式 输入格式: 第一行一个整数N,表 ...
- 洛谷P3966 [TJOI2013]单词(后缀自动机)
传送门 统计单词出现次数……为啥大家都是写AC自动机的嘞……明明后缀自动机也能做的说…… 统计出现次数这个就直接按长度排序然后做个dp就好,这是SAM的板子的要求啊,不提了 然后考虑怎么让所有串之间隔 ...
随机推荐
- 二进制安装Mysql 5.6(免编译)
安装系统基础软包 yum install -y make bc perl gcc openssl openssl-devel ncurses ncurses-devel 安装方式:二进制免编译安装 查 ...
- NVIDIA GPU架构与原理分析(一)——GPU简介与主流Fermi、Kepler架构GPU概述
1 GPU简介 图形处理单元GPU英文全称Graphic Processing Unit,GPU是相对于CPU的一个概念,NVIDIA公司在1999年发布GeForce256图形处理芯片时首先提出GP ...
- [听听音乐]X-Ray Dog
X-Ray Dog是一个专门为好莱坞电影预告配乐的音乐工作室. https://music.163.com/#/artist?id=105618 有空的时候听听.
- EasyDarwin接入ffmpeg实现264转图片快照功能
本文转自:http://blog.csdn.net/cai6811376/article/details/51774467 EasyDarwin一直坚持开源精神,所以我们着手把EasyDarwin中使 ...
- Apache JServ Protocol
ajp_百度百科 https://baike.baidu.com/item/ajp/1187933 AJP(Apache JServ Protocol)是定向包协议.因为性能原因,使用二进制格式来传输 ...
- DIV+CSS常见问题的14条原因分析
当你在一个浏览器里面做好,在其他浏览器里面却完全不是那么回事情. 很多时候,我们就只是去修补下,或者利用各个浏览器对代码支持的不一致,进行针对各个浏览器进行不同的定义. 其实浏览器的不兼容,我们往 ...
- Boost 库编译总结
1. 下载boost库源码,解压缩. 2. 打开vs2010 工具栏tools 下的visual studio command prompt,运行源码目录下的bootstrap.bat,生成bjam. ...
- Linux ARM交叉编译工具链制作过程【转】
本文转载自:http://www.cnblogs.com/Charles-Zhang-Blog/archive/2013/02/21/2920999.html 一.下载源文件 源代码文件及其版本与下载 ...
- Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf —— DP
题目链接:http://codeforces.com/contest/294/problem/B B. Shaass and Bookshelf time limit per test 1 secon ...
- lvs+keepalived+nginx高性能负载均衡集群
项目发布时候,别人还能访问呢? 双机主从热备 LVS作用 LVS是一个开源的软件,可以实现传输层四层负载均衡.LVS是Linux Virtual Server的缩写,意思是Linux虚拟服务器.目前有 ...