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 ...
随机推荐
- docker 升级后,配置 idea 连接 docker
[root@A01-R02-I188-87 ~]# docker version Client: Version: 18.06.1-ce API version: 1.24 Go version: g ...
- struts的标签
<%@ taglib uri="/struts-tags" prefix="s"%> <%@ taglib uri="/WEB-IN ...
- Warning: Permanently added the RSA host key for IP address '192.30.253.113' to the list of known hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure y
这个应该是很多github新手经常出错的问题,这个就是没有在你github上添加一个公钥. 下面就直接说步骤: 1 可以用 ssh -T git@github.com去测试一下 图上可以明显看出缺少了 ...
- Linux下文件目录权限和对应命令的总结
Linux下的权限有rwx三种,分别对应读,写,执行三种,在对文件和目录时,分别是下列含义: 对应权限的命令为: 文件: r-- cat, more, head, tail w-- echo, vi ...
- Ubuntu下安装Python3.6并在终端输入Python就能显示Python3.6
Ubuntu17.04自带Python2.7与Python3.5.3的版本,由于Python2与Python3有着一些差距可能需要安装更新Python3的版本,并且切换默认的Python解释器. ...
- highcharts自定义导出文件格式(csv) highcharts的一些使用心得
highcharts是国外的一个图表插件,包括各种数据图形展示,柱形图,线性图等等,是手机端和pc端最好的图表插件之一,相比于百度的echarts更加轻便和易懂.链接http://www.hchart ...
- Suse Linux下NTP缓慢调整配置,转载至http://www.gpstime.com.cn/
(1)系统内若有使用crontab 进行与时间调整相关的例行性工作排程,应注释掉(命令人工crontab -e修改,删除定时同步任务ntpdate -s ntpserver). (2)修改ntp配置文 ...
- Visual Tracking with Fully Convolutional Networks
http://blog.csdn.net/carrierlxksuper/article/details/48918297 传统的跟踪方法依赖低维的人工特征,但这种特征对目标的外观变化等问题不够鲁棒. ...
- Codeforces 351D Jeff and Removing Periods(莫队+区间等差数列更新)
题目链接:http://codeforces.com/problemset/problem/351/D 题目大意:有n个数,每次可以删除掉数值相同并且所在位置成等差数列的数(只删2个数或者只删1个数应 ...
- python的scrapy框架
scrapy是python中数据抓取的框架.简单的逻辑如下所示 scrapy的结构如图所示,包括scrapy engine.scheduler.downloader.spider.item pipel ...