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 ...
随机推荐
- C# 动态调取 soap 接口
调用示例 string url = "http://localhost:8080/server/PatientService.asmx"; Hashtable ht = new H ...
- 2017 NEERC
2017 NEERC Problem A. Archery Tournament 题目描述:在二维平面上,会陆续出现一些圆,以及一些询问,询问点是否在圆内,如果是,则输出那个圆,并把那个圆删掉,否则输 ...
- Linux命令参数处理 shell脚本函数getopts
getopts 命令 用途 处理命令行参数,并校验有效选项. 语法 getopts 选项字符串 名称 [ 参数 ...] 描述 getopts 的设计目标是在循环中运行,每次执行循环,getopts ...
- P1084 疫情控制
Solution 二分答案, 尽量往上跳, 不能跳到根节点. 仍然能跳的拿出来.看剩下的点没有覆盖哪个? 贪心的分配一下. Code 70 #include<iostream> #incl ...
- 字符串匹配的KMP算法(如何实现还需静下心来细看)
第一部分:KMP算法的理解(转:http://kb.cnblogs.com/page/176818/) 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB AB ...
- CSRF攻击的应对之道
CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在并未授权的情况下执 ...
- 得分(UVa1585)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- Python 中for...esle和while...else语法
Python的for...else和while...else语法,这是Python中最不常用,最为误解的语法特性之一. Python中的for.while循环都有一个可选的else分支(类似if语句和 ...
- lr场景运行报错的解决方法
- vim编码相关
与vim编码相关的四个配置: encoding:vim核心编码,所有vim交换区,信息提示区都用这个编码.打开文件的编码如果是其他编码,会自动转换为核心编码,保存时再转回文件编码. fileencod ...