正文
增加 22 行删除 22 行
34 行未改动3536373839404142434 行未改动484950515253545556575859606162636455 行未改动12012112212312412512612712815 行未改动14414514614714814915015115215315415515615715815916016116216316416516616716816917017117217378 行未改动25225325425525625725825926048 行未改动30931031131231331431531631762 行未改动3803813823833843853863873883893907 行未改动39839940040140240340440540630 行未改动43743843944044144244344444532 行未改动4784794804814824834844854864874884894906 行未改动49749849950050150250350450550650750850951051151251339 行未改动553554555556557558559560561238 行未改动全部展开34 行未改动Trie 构建这部分的代码很简单,每次给出一个单词添加到 Trie 里面就可以了。```cppstatic const int SIGMA=; // 最多有多少个分支static const int MAXN=; // 所有模式串的总长度之和(最多分支节点) + 1(根节点)struct Node {int nxt[SIGMA]; // 该节点的子节点,没有是 04 行未改动fail=0;}};vector<Node> tr;void init() {tr.reserve(MAXN);tr.push_back(Node()); // root = 0}static int id(char c) { // 用于返回处理后的 id (把边权从其他类型转到编号)return c-'A'; // 这是个例子,将 'A'~'Z' 转换到编号 0~25,方便 nxt 存储}void insert(string s) { // 添加一个模式串到 Trieint u=0;55 行未改动if (v) {tr[v].fail=0; // 初始化q.push(v);} else {tr[0].nxt[c]=0;}}while (!q.empty()) {15 行未改动到这里,一个基础的 AC 自动机模板就已经完成了,我们先把它封装一下。```cppstruct ACor {static const int SIGMA=::SIGMA;static const int MAXN=::MAXN; // 所有模式串的总长度之和 + 1(根节点)struct Node {int nxt[SIGMA];int fail;Node() {memset(nxt,0,sizeof(nxt));fail=0;}};vector<Node> tr;ACor() {tr.reserve(MAXN);tr.push_back(Node()); // root=0}static int id(int c) {return c;}void insert(string s) {int u=0;78 行未改动这时候我们需要在每个节点添加一个参数 `vector<int> out`,存储当前节点可以匹配到的模式串 ID。```cppstruct Node {int nxt[SIGMA];int fail;vector<int> out;Node() {48 行未改动tr[v].fail=tr[tr[u].fail].nxt[c];tr[v].out.insert(tr[v].out.end(),tr[tr[v].fail].out.begin(),tr[tr[v].fail].out.end());q.push(v);} else {tr[u].nxt[c]=tr[tr[u].fail].nxt[c];}}}}62 行未改动因为我们要记录每个节点的真实贡献,所以我们要在结构体中新加一个变量 `mcnt`;因为我们要记录每个节点的入度,所以我们要在结构体中新加一个变量 `rd`:```cppstruct Node {int nxt[SIGMA];int fail;vector<int> out;int mcnt;int rd;7 行未改动}};```每个经过的点都要让真实贡献 `mcnt++`。```cppvoid assign(const string& s) {int u=0;30 行未改动tr[v].fail=tr[tr[u].fail].nxt[c];tr[tr[v].fail].rd++; // v -> tr[v].failq.push(v);} else {tr[u].nxt[c]=tr[tr[u].fail].nxt[c];}}}}32 行未改动到这里,我们已经彻底掌握 AC 自动机**查询模式串出现次数**的题型了,我们把这个题型的最终模板放一下:```cppstruct ACor {static const int SIGMA=::SIGMA;static const int MAXN=::MAXN; // 所有模式串的总长度之和+1(根节点)struct Node {int nxt[SIGMA];int fail;vector<int> out;int mcnt;int rd;6 行未改动rd=0;}};vector<Node> tr;ACor() {tr.reserve(MAXN);tr.push_back(Node()); // root=0}static int id(char c) { // 用于返回nxt_idreturn -----; // TODO 转义}void insert(string s,int _id) {int u=0;39 行未改动tr[v].fail=tr[tr[u].fail].nxt[c];tr[tr[v].fail].rd++; // v -> tr[v].failq.push(v);} else {tr[u].nxt[c]=tr[tr[u].fail].nxt[c];}}}}238 行未改动全部展开34 行未改动3536373839404142434 行未改动484950515253545556575859606162636455 行未改动12012112212312412512612712815 行未改动14414514614714814915015115215315415515615715815916016116216316416516616716816917017117217378 行未改动25225325425525625725825926048 行未改动30931031131231331431531631762 行未改动3803813823833843853863873883893907 行未改动39839940040140240340440540630 行未改动43743843944044144244344444532 行未改动4784794804814824834844854864874884894906 行未改动49749849950050150250350450550650750850951051151251339 行未改动553554555556557558559560561238 行未改动全部展开34 行未改动Trie 构建这部分的代码很简单,每次给出一个单词添加到 Trie 里面就可以了。```cppstatic const int SIGMA=; // 最多有多少个分支static const int MAXN=; // 所有模式串的总长度之和(最多分支节点) + 1(根节点)struct Node {int nxt[SIGMA]; // 该节点的子节点,没有是 04 行未改动fail=0;}};vector<Node> tr;void init() {tr.reserve(MAXN);tr.push_back(Node()); // root = 0}static int id(char c) { // 用于返回处理后的 id (把边权从其他类型转到编号)return c-'A'; // 这是个例子,将 'A'~'Z' 转换到编号 0~25,方便 nxt 存储}void insert(string s) { // 添加一个模式串到 Trieint u=0;55 行未改动if (v) {tr[v].fail=0; // 初始化q.push(v);} else {tr[0].nxt[c]=0;}}while (!q.empty()) {15 行未改动到这里,一个基础的 AC 自动机模板就已经完成了,我们先把它封装一下。```cppstruct ACor {static const int SIGMA=::SIGMA;static const int MAXN=::MAXN; // 所有模式串的总长度之和 + 1(根节点)struct Node {int nxt[SIGMA];int fail;Node() {memset(nxt,0,sizeof(nxt));fail=0;}};vector<Node> tr;ACor() {tr.reserve(MAXN);tr.push_back(Node()); // root=0}static int id(int c) {return c;}void insert(string s) {int u=0;78 行未改动这时候我们需要在每个节点添加一个参数 `vector<int> out`,存储当前节点可以匹配到的模式串 ID。```cppstruct Node {int nxt[SIGMA];int fail;vector<int> out;Node() {48 行未改动tr[v].fail=tr[tr[u].fail].nxt[c];tr[v].out.insert(tr[v].out.end(),tr[tr[v].fail].out.begin(),tr[tr[v].fail].out.end());q.push(v);} else {tr[u].nxt[c]=tr[tr[u].fail].nxt[c];}}}}62 行未改动因为我们要记录每个节点的真实贡献,所以我们要在结构体中新加一个变量 `mcnt`;因为我们要记录每个节点的入度,所以我们要在结构体中新加一个变量 `rd`:```cppstruct Node {int nxt[SIGMA];int fail;vector<int> out;int mcnt;int rd;7 行未改动}};```每个经过的点都要让真实贡献 `mcnt++`。```cppvoid assign(const string& s) {int u=0;30 行未改动tr[v].fail=tr[tr[u].fail].nxt[c];tr[tr[v].fail].rd++; // v -> tr[v].failq.push(v);} else {tr[u].nxt[c]=tr[tr[u].fail].nxt[c];}}}}32 行未改动到这里,我们已经彻底掌握 AC 自动机**查询模式串出现次数**的题型了,我们把这个题型的最终模板放一下:```cppstruct ACor {static const int SIGMA=::SIGMA;static const int MAXN=::MAXN; // 所有模式串的总长度之和+1(根节点)struct Node {int nxt[SIGMA];int fail;vector<int> out;int mcnt;int rd;6 行未改动rd=0;}};vector<Node> tr;ACor() {tr.reserve(MAXN);tr.push_back(Node()); // root=0}static int id(char c) { // 用于返回nxt_idreturn -----; // TODO 转义}void insert(string s,int _id) {int u=0;39 行未改动tr[v].fail=tr[tr[u].fail].nxt[c];tr[tr[v].fail].rd++; // v -> tr[v].failq.push(v);} else {tr[u].nxt[c]=tr[tr[u].fail].nxt[c];}}}}238 行未改动全部展开