题目大意:求一篇论文中每个单词分别在论文中出现多少次。

本题用AC自动机太慢,应该用Fail树将AC自动机中所有的Fail指针反向得到一个新树,这就是Fail树。对长度为x的字符串a和长度为y的字符串b,如果a是b的子串,则a可能与位于b[0,a],b[0,a+1],b[0,a+2]...b[0,y]中的后缀相等。根据fail指针的定义,只要沿着反向Fail边走,走到的节点所代表的字符串必然存在与a(前缀)相等的后缀。因此,一遍DFS,返回加上子节点的总Cnt值的当前节点的Cnt值,即可。注意,Trie树中,有些节点是多个字符串公用的,因此每次构造Trie树时,都要对每个节点的Cnt++,以等价于此处存在多个字符串。

#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std; const int MAX_CHAR = , MAX_LEN = 1e6 + , MAX_STR = ; struct FailTree
{
#define Root _nodes[0]
#define Org(x) x - 'a' struct Node;
struct Edge; struct Node
{
Node *Next[MAX_CHAR], *Fail;
int Cnt;
Edge *Head;
Node() :Cnt(), Fail(NULL), Head(NULL) { memset(Next, NULL, sizeof(Next)); }
};
vector<Node*> _nodes, Tail; struct Edge
{
Node *To;
Edge *Next;
Edge(Node *to, Edge *next):To(to),Next(next){}
};
vector<Edge*> _edges; FailTree()
{
_nodes.push_back(new Node());
} void AddEdge(Node *from, Node *to)
{
Edge *e = new Edge(to, from->Head);
from->Head = e;
_edges.push_back(e);
} Node *BuildTrie(char *s)
{
int len = strlen(s);
Node *cur = Root;
for (int i = ; i < len; i++)
{
if (!cur->Next[Org(s[i])])
_nodes.push_back(cur->Next[Org(s[i])] = new Node());
cur = cur->Next[Org(s[i])];
cur->Cnt++;
}
return cur;
} void Insert(char *s)
{
Tail.push_back(BuildTrie(s));
} void SetFail()
{
static queue<Node*> q;
q.push(Root);
while (!q.empty())
{
Node *cur = q.front();
q.pop();
for (int i = ; i < MAX_CHAR; i++)
{
if (cur->Next[i])
{
Node *temp = cur->Fail;
while (temp)
{
if (temp->Next[i])
{
cur->Next[i]->Fail = temp->Next[i];
AddEdge(temp->Next[i], cur->Next[i]);
break;
}
temp = temp->Fail;
}
if (!temp)
{
cur->Next[i]->Fail = Root;
AddEdge(Root, cur->Next[i]);
}
q.push(cur->Next[i]);
}
}
}
} int Dfs(Node *u)
{
for (Edge *e = u->Head; e; e = e->Next)
u->Cnt += Dfs(e->To);
return u->Cnt;
}
}g; int main()
{
#ifdef _DEBUG
freopen("c:\\noi\\source\\input.txt", "r", stdin);
#endif
int tot;
char s[MAX_LEN];
scanf("%d", &tot);
for(int i=; i<tot; i++)
{
scanf("%s", s);
g.Insert(s);
}
g.SetFail();
g.Dfs(g.Root);
for (int i = ; i < tot; i++)
printf("%d\n", g.Tail[i]->Cnt);
return ;
}

或者不用反向Fail指针也可以,站在后缀上去找其所包含的前缀。这样编程复杂度低一些。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cassert>
#include <cmath>
#include <algorithm>
using namespace std; const int MAX_CHAR = , MAX_NODE = 5e5 + , MAX_LEN = 1e6 + ; struct Node
{
int Sum, Id, Cnt;
Node *Fail;
Node *Next[MAX_CHAR];
}Nodes[MAX_NODE];
int Nodes_Cnt = ;
char P[MAX_LEN];
Node *WordNode[MAX_NODE]; int Ord(char c)
{
return c - 'a';
} Node *NewNode()
{
return ++Nodes_Cnt + Nodes;
} Node *Root()
{
return Nodes + ;
} void BuildTrie(char *s, int id)
{
Node *cur = Root();
int len = strlen(s);
for (int i = ; i < len; i++)
{
if (cur->Next[Ord(s[i])])
cur = cur->Next[Ord(s[i])];
else
cur = cur->Next[Ord(s[i])] = NewNode();
}
cur->Sum++;
cur->Id = id;
WordNode[id] = cur;
} void SetFail()
{
queue<Node*> q;
q.push(Root());
while (!q.empty())
{
Node *cur = q.front();
q.pop();
for (int i = ; i < MAX_CHAR; i++)
{
if (cur->Next[i])
{
Node *temp = cur->Fail;
while (temp)
{
if (temp->Next[i])
{
cur->Next[i]->Fail = temp->Next[i];
break;
}
temp = temp->Fail;
}
if (!temp)
{
cur->Next[i]->Fail = Root();
}
q.push(cur->Next[i]);
}
}
}
} int Dfs1(Node *cur)
{
int cnt = cur->Sum;
for (int i = ; i < MAX_CHAR; i++)
if (cur->Next[i])
cnt += Dfs1(cur->Next[i]);
for (Node *temp = cur; temp != Root(); temp = temp->Fail)
if (temp->Sum)
temp->Cnt+=cnt;
//cur->Cnt += cnt;
return cnt;
} int main()
{
//freopen("c:\\noi\\source\\input.txt", "r", stdin);
int totP;
scanf("%d", &totP);
for (int i = ; i < totP; i++)
{
scanf("%s", P);
BuildTrie(P, i);
}
SetFail();
Dfs1(Root());
for (int i = ; i < totP; i++)
printf("%d\n", WordNode[i]->Cnt);
return ;
}

BZOJ3172 单词 Fail树的更多相关文章

  1. 【BZOJ2905】背单词 fail树+DFS序+线段树

    [BZOJ2905]背单词 Description 给定一张包含N个单词的表,每个单词有个价值W.要求从中选出一个子序列使得其中的每个单词是后一个单词的子串,最大化子序列中W的和. Input 第一行 ...

  2. bzoj 3172: [Tjoi2013]单词 fail树

    题目大意: 一篇论文是由许多单词组成,现在想知道每个单词分别在论文中出现多少次. 题解: 我们首先考虑fail指针的含义 如果fail[x] = y,那么我们就知道y作为x的后缀在x中出现了一次 所以 ...

  3. BZOJ3172[Tjoi2013]单词——AC自动机(fail树)

    题目描述 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. 输入 第一个一个整数N,表示有多少个单词,接下来N行每行一个单词.每个 ...

  4. BZOJ3172 & 洛谷3966 [Tjoi2013]单词 【fail树】

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 4293  Solved: 2083 [Submit][Stat ...

  5. [Bzoj3172][Tjoi2013]单词(fail树)

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

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

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

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

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

  8. BZOJ2905: 背单词 AC自动机+fail树+线段树

    $zjq$神犇一眼看出$AC$自动机 $Orz$ 直接就讲做法了 首先对每个串建出$AC$自动机 将$fail$树找到 然后求出$dfs$序 我们发现一个单词 $S_i$是$S_j$的子串当且仅当$S ...

  9. 【洛谷】3966:[TJOI2013]单词【AC自动机】【fail树】

    P3966 [TJOI2013]单词 题目描述 小张最近在忙毕设,所以一直在读论文.一篇论文是由许多单词组成但小张发现一个单词会在论文中出现很多次,他想知道每个单词分别在论文中出现了多少次. 输入输出 ...

随机推荐

  1. Java 开源博客 Solo 1.2.0 发布 - 一键启动

    Solo 1.2.0 正式发布了,感谢一直以来关注 B3log 开源的朋友! 在这个版本中,我们引入了一个新的特性 -- 独立模式: 不需要安装数据库.Servlet 容器 只需要安装好 Java 环 ...

  2. php正则表达式应用

    正则表达式 1.替换“/\d/”,“#”,$str:正则表达式\d 数字,替换为#,字符串 $str = "2hello 5li 6lei"; echo preg_replace( ...

  3. MSP430之software development flow

    MSP430 software development flow. 1) The shaded portion highlights the most common development path; ...

  4. 09--c++ 类的继承与派生

    c++ 类的继承与派生   一.基本概念 1.类的继承,是新的类从已有类那里得到已有的特性.或从已有类产生新类的过程就是类的派生.原有的类称为基类或父类,产生的新类称为派生类或子类.   2.派生类的 ...

  5. 【sqli-labs】 less5 GET - Double Injection - Single Quotes - String (双注入GET单引号字符型注入)

    双注入查询可以查看这两篇介绍 https://www.2cto.com/article/201302/190763.html https://www.2cto.com/article/201303/1 ...

  6. Redis 之list链表结构及命令详解

    1.lpush  key   value   从左放一个值 2.rpush  key   value  从右放一个值 3.lrange  key  start   stop  获取链表数据(start ...

  7. ffmpeg中关于EAGAIN的理解及非阻塞IO

    ffmpeg为在linux下开发的开源音视频框架,所以经常会碰到很多错误(设置errno),其中EAGAIN是其中比较常见的一个错误(比如用在非阻塞操作中).  try again,从字面上来看,是提 ...

  8. PAT_A1148#Werewolf - Simple Version

    Source: PAT 1148 Werewolf - Simple Version (20 分) Description: Werewolf(狼人杀) is a game in which the ...

  9. eas之获取单据编码规则

    //获取单据编码规则  /*** @Title: getNumber* @Description: TODO(获取单据编码规则)*               <p>* @date 201 ...

  10. UVA1339 - Ancient Cipher 【字符串+排序】【紫书例题4.1】

    题意:给定两个字符串,你可以替换或者置换,替换是指可以将相同的字母替换为任意一个字母,而置换是指将字母替换为下一个,如A替换B,B替换为C,,,Z替换为A.你需要判断是否可以通过一系列操作使两个字符串 ...