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 ...
随机推荐
- JFinal向客户端渲染图片的方法
JFinal提供了好几种方便的render但是不知道为啥就是没有提供直接渲染图片的render,如果我们直接在Controller的方法中往输入流中写的话是还是会有默认的render生效的,比如下面这 ...
- Entity Framework(EF的Database First方法)
EntityFramework,是Microsoft的一款ORM(Object-Relation-Mapping)框架.同其它ORM(如,NHibernate,Hibernate)一样, 一是为了使开 ...
- MacOS Safari 中 button 不能使用 text-gradient
@mixin text-gradient ($deg: 90deg, $from: $gradientFrom, $to: $gradientEnd) { background-image: line ...
- Linux下多路径multipath配置【转】
一.multipath在redhat 6.2中的基本配置: 1. 通过命令:lsmod |grep dm_multipath 检查是否正常安装成功.如果没有输出说明没有安装那么通过yum功能安装一下 ...
- ahttp
# -*- coding: utf-8 -*- # @Time : 2018/8/20 14:35 # @Author : cxa # @File : chttp.py # @Software: Py ...
- Java network programming-guessing game
猜数字游戏 游戏的规则如下: 当客户端第一次连接到服务器端时,服务器端生产一个[0,50]之间的随机数字,然后客户端输入数字来猜该数字,每次客户端输入数字以后,发送给服务器端,服务器端判断该客户端发送 ...
- ansible报错Aborting, target uses selinux but python bindings (libselinux-python) aren't installed
报错内容: TASK [activemq : jvm configuration] ********************************************************** ...
- golang之结构体和方法
结构体的定义 结构体是将零个或者多个任意类型的命令变量组合在一起的聚合数据类型.每个变量都叫做结构体的成员. 其实简单理解,Go语言的结构体struct和其他语言的类class有相等的地位,但是GO语 ...
- 实现celery中出现拥挤队列时,及时发邮件通知
里面有几个常用的功能,以后值得借鉴. 如获取脚本目录,IP,获取shell返回值,发送邮件等.. 上午写完,中午测试,下午上线~~ #!/usr/bin/env python # -*- coding ...
- 解决类似 /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found 的问题
https://itbilu.com/linux/management/NymXRUieg.html