bzoj 3172
收获:AC自动机定数组大小时,如果不确定,就定10^6(极限了)
/**************************************************************
Problem: 3172
User: idy002
Language: C++
Result: Accepted
Time:688 ms
Memory:321164 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define maxn 1000010
#define fprintf(...)
using namespace std; struct AC {
int son[maxn][], ntot;
int fail[maxn], last[maxn];
vector<int> vc[maxn];
int ans[]; void insert( int id, const char *P ) {
int u=;
fprintf( stderr, "insert '%s'\npath:\n", P );
while() {
fprintf( stderr, "%d ", u );
if( *P ) {
int c=(*P++)-'a';
if( !son[u][c] ) son[u][c]=++ntot;
u=son[u][c];
} else {
vc[u].push_back(id);
fprintf( stderr, "\n" );
return;
}
}
}
void build() {
fprintf( stderr, "\n\nbuild\n" );
queue<int> qu;
for( int c=; c<; c++ ) {
int v=son[][c];
if( !v ) continue;
qu.push( v );
fail[v] = last[v] = ;
}
while( !qu.empty() ) {
int u=qu.front();
qu.pop();
for( int c=; c<; c++ ) {
int v=son[u][c];
int w=fail[u];
if( !v ) continue;
while( w && !son[w][c] ) w=fail[w];
fail[v] = son[w][c];
last[v] = vc[son[w][c]].size() ? son[w][c] : last[son[w][c]];
fprintf( stderr, "fail[%d]=%d last[%d]=%d\n", v, fail[v], v, last[v] );
qu.push(v);
}
}
fprintf( stderr, "\n" );
}
void add( int u ) {
if( !u ) return;
add(last[u]);
for( int t=; t<vc[u].size(); t++ )
ans[vc[u][t]]++;
}
void search( const char *T ) {
int n=strlen(T);
int u=;
for( int i=; i<n; i++ ) {
int c=T[i]-'a';
while( u && !son[u][c] ) u=fail[u];
u=son[u][c];
if( vc[u].size() ) add(u);
else if( last[u] ) add(last[u]);
}
}
}ac; int n;
char str[]; int main() {
scanf( "%d", &n );
int len=;
for( int i=; i<=n; i++ ) {
scanf( "%s", str+len );
ac.insert( i, str+len );
len += strlen(str+len);
str[len++] = 'z'+;
}
ac.build();
ac.search( str );
for( int i=; i<=n; i++ )
printf( "%d\n", ac.ans[i] );
}
bzoj 3172的更多相关文章
- [BZOJ 3172] [Tjoi2013] 单词 【AC自动机】
题目链接:BZOJ - 3172 题目分析: 题目要求求出每个单词出现的次数,如果把每个单词都在AC自动机里直接跑一遍,复杂度会很高. 这里使用AC自动机的“副产品”——Fail树,Fail树的一个性 ...
- BZOJ 3172([Tjoi2013]单词-后缀数组第一题+RMQ)
3172: [Tjoi2013]单词 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 268 Solved: 145 [ Submit][ St ...
- BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]
3172: [Tjoi2013]单词 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3198 Solved: 1532[Submit][Status ...
- bzoj 3172 后缀数组|AC自动机
后缀数组或者AC自动机都可以,模板题. /************************************************************** Problem: 3172 Us ...
- BZOJ 3172 单词(ac自动机)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3172 题意:给出n个单词.输出每个单词在所有单词中一共出现多少次? 思路:首先将所有单词 ...
- bzoj 3172 [Tjoi2013]单词(fail树,DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3172 [题意] 题目的意思是这样的,给若干个单词,求每个单词在这一堆单词中的出现次数. ...
- ●BZOJ 3172 [Tjoi2013]单词
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3172 题解: 把单词逐个接起来,中间用互不相同的字符连接,并记录下每个单词的首字母在串中的位 ...
- BZOJ 3172 [Tjoi2013]单词 AC自动机Fail树
题目链接:[http://www.lydsy.com/JudgeOnline/problem.php?id=3172] 题意:给出一个文章的所有单词,然后找出每个单词在文章中出现的次数,单词用标点符号 ...
- bzoj 3172: [Tjoi2013]单词 AC自动机
3172: [Tjoi2013]单词 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
随机推荐
- JS设计模式——5.单体模式(用了这么久,竟全然不知!)
单体模式的优势 用了这么久的单体模式,竟全然不知!用它具体有哪些好处呢? 1.可以用它来划分命名空间(这个就是就是经常用的了) 2.利用分支技术来封装浏览器之间的差异(这个还真没用过,挺新鲜) 3.借 ...
- tf.name_scope tf.variable_scope学习
1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...
- 深入理解Spring系列之十一:SpringMVC-@RequestBody接收json数据报415
转载 https://mp.weixin.qq.com/s/beRttZyxM3IBJJSXsLzh5g 问题原因 报错原因可能有两种情况: 请求头中没有设置Content-Type参数,或Conte ...
- OGG相关操作
参数文件详解: 1)truncate ogg 进程: Manager进程:manager进程是配置在源端和目标端 Extract(抽取)进程:部署在源端,用于捕获数据表或者日志中的数据文件: Pump ...
- perl6检测网站CMS脚本(测试代码)
代码如下: use HTTP::UserAgent; use JSON::Tiny; my $check-url = 'www.baidu.com'; #say @*ARGS[0]; #检测命令行参数 ...
- 45.Jump Game II---贪心---2018大疆笔试题
题目链接 题目大意:与55题类似,只是这里要求出跳数. 法一(借鉴):贪心.cur表示当前能到达的最远距离,pre表示上一次能到达的最远距离,每到一个位置更新一次最远距离cur,如果当前位置超过了上一 ...
- HDU 3746 Cyclic Nacklace(KMP找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题目大意:给你一个字符串,求出将字符串的最少出现两次循环节需要添加的字符数. 解题思路: 这题需 ...
- Django的自带认证系统——auth模块
Django自带的用户认证 auth模块 from django.contrib import auth 备注:使用auth模块时,我们默认使用Django提供的auth_user表,创建数据时,可以 ...
- fedora安装后的配置
fedora安装后的一些配置 (mirror)源 换源 默认从fedora官网下载太慢,考虑换用国内的源(镜像站点),推荐中科大.阿里云.浙大.网易等的源. 比如我用浙大ZJU的源http://mir ...
- CSU 1416 Practical Number
原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1416 结论题,具体判断方法请点击这个网址. 筛素数是肯定的,但一开始定的范围太大了,想当 ...