收获: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的更多相关文章

  1. [BZOJ 3172] [Tjoi2013] 单词 【AC自动机】

    题目链接:BZOJ - 3172 题目分析: 题目要求求出每个单词出现的次数,如果把每个单词都在AC自动机里直接跑一遍,复杂度会很高. 这里使用AC自动机的“副产品”——Fail树,Fail树的一个性 ...

  2. BZOJ 3172([Tjoi2013]单词-后缀数组第一题+RMQ)

    3172: [Tjoi2013]单词 Time Limit: 10 Sec   Memory Limit: 512 MB Submit: 268   Solved: 145 [ Submit][ St ...

  3. BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3198  Solved: 1532[Submit][Status ...

  4. bzoj 3172 后缀数组|AC自动机

    后缀数组或者AC自动机都可以,模板题. /************************************************************** Problem: 3172 Us ...

  5. BZOJ 3172 单词(ac自动机)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3172 题意:给出n个单词.输出每个单词在所有单词中一共出现多少次? 思路:首先将所有单词 ...

  6. bzoj 3172 [Tjoi2013]单词(fail树,DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3172 [题意] 题目的意思是这样的,给若干个单词,求每个单词在这一堆单词中的出现次数. ...

  7. ●BZOJ 3172 [Tjoi2013]单词

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3172 题解: 把单词逐个接起来,中间用互不相同的字符连接,并记录下每个单词的首字母在串中的位 ...

  8. BZOJ 3172 [Tjoi2013]单词 AC自动机Fail树

    题目链接:[http://www.lydsy.com/JudgeOnline/problem.php?id=3172] 题意:给出一个文章的所有单词,然后找出每个单词在文章中出现的次数,单词用标点符号 ...

  9. bzoj 3172: [Tjoi2013]单词 AC自动机

    3172: [Tjoi2013]单词 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

随机推荐

  1. JavaScript 使用闭包保护变量 防止污染

    使用JavaScript编写插件或团队协作时,可使用闭包来解决此类以下两个问题: 1.定义过多全局变量,可能会造成全局变量命名冲突: 2.在插件内定义变量,需要保护该变量不被轻易修改: 优点:可以把局 ...

  2. 生成验证码tp

    js里拼接随机数 页面上链接 去掉后缀名

  3. 47、求1+2+3+...+n

    一.题目 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 二.解法 public class Solut ...

  4. $fhqTreap$

    - $fhqTreap$与$Treap$的差异 $fhqTreap$是$Treap$的非旋版本,可以实现一切$Treap$操作,及区间操作和可持久化 $fhqTreap$非旋关键在于分裂与合并$(Sp ...

  5. 到底什么是Upnp?[转载]

    本文出自:http://www.cnblogs.com/nehu/archive/2006/05/13/399342.html 解释一. 准确地说,UPnP(Universal Plug and Pl ...

  6. acm专题--并查集

    题目来源:http://hihocoder.com/problemset/problem/1066 #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256M ...

  7. (转载)使用curl 和 libjson 完成联网和数据解析

    转载地址:http://my.oschina.net/cocosgame/blog/71181 libjson 编译和使用 - 3. libjson的C接口 API http://blog.csdn. ...

  8. Asp.net vNext 学习之路(二)

    View component(视图组件)应该是MVC6 新加的一个东西,类似于分部视图.本文将演示在mvc 6中 怎么添加视图组件以及怎么在视图中注入一个服务. 本文包括以下内容: 1,创建一个新的a ...

  9. 解决xshell4中文乱码

    前言 在公司看到别人使用xshell后,由于之前一直使用SecureCRT所以心里一直痒痒的想换下xshell试下.于是在昨天晚上使用xshell,却被中文乱码折腾了很长时间以至于想放弃,最终灵光一现 ...

  10. logstash通过tcp收集日志

    (1)标准输入输出tcp模块 1.修改配置文件 #vim /etc/logstash/conf.d/tcp.conf input { tcp { port => "5600" ...