正文
增加 47 行删除 20 行
12348 行未改动13141516171819202122232425262728293031323334353637383940414243444546474849502 行未改动535455565758596061626364656667686970717273747576777879808182838420 行未改动105106107108109110111112## 前置知识- 回文自动机 PAM介绍详见 [OI Wiki 中的描述](https://oi-wiki.org/string/pam/),此处不作赘述。8 行未改动### 删除操作由于 PAM 结点只增不减(保证 `fail` 指针有效及结点复用),删除时采用状态回滚策略,通过维护历史栈恢复上一状态。### 空间PAM 结点数最多为 $|S| + 2$,$Q \le 10000$,数组开 `N = 10010` 足够。## 代码实现```cpp#include <bits/stdc++.h>using namespace std;#define int long long // 十年 OI 一场空,不开 long long 见祖宗const int N = 10010;const int AL = 26;struct PAM {int n, last, tot;int tp; // 回文子串总数int len[N], fail[N];int num[N]; // num[last]: 以当前位置结尾的回文子串个数int nextt[N][AL];vector<int> lh; // last 历史栈 (用于回滚)vector<int> th; // 答案 tp 历史栈 (用于回滚)vector<int> ch; // 字符栈int s[N];PAM() {init();}void init() {n = 0; last = 0; tot = 2; tp = 0;len[0] = 0; fail[0] = 1;len[1] = -1; fail[1] = 1;num[0] = num[1] = 0;s[0] = -1; // 哨兵:防止 get_fail 匹配时越界memset(nextt, 0, sizeof(nextt));lh.clear(); th.clear(); ch.clear();lh.push_back(last);th.push_back(tp);}2 行未改动x = fail[x];}return x;}void add(int c) {s[++n] = c;int cur = get_fail(last);if (!nextt[cur][c]) {int now = tot++;len[now] = len[cur] + 2;fail[now] = (len[now] == 1) ? 0 : nextt[get_fail(fail[cur])][c];nextt[cur][c] = now;num[now] = num[fail[now]] + 1; // 核心:统计回文后缀数}last = nextt[cur][c];tp += num[last]; // 累加新增贡献// 存档ch.push_back(c);lh.push_back(last);th.push_back(tp);}void remove() {if (n == 0) {return;}ch.pop_back();lh.pop_back(); // 回滚 lastth.pop_back(); // 回滚答案last = lh.back();tp = th.back();n--;}20 行未改动}return 0;}```## 复杂度分析- **时间复杂度**:$O(Q)$。PAM 的 `get_fail` 跳转次数是均摊 $O(1)$ 的,删除操作仅为栈操作 $O(1)$,不存在真正意义上的 $O(q^2)$ 最坏情况。- **空间复杂度**:$O(Q)$。PAM 结点数最多为 $Q+2$,栈空间也为 $O(Q)$。12345678 行未改动1617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162632 行未改动6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411520 行未改动136137138139## 一些题外话- upd on 2026.10.24 感谢 @lrx___ 对本文时间复杂度的更正,详见[此工单](https://www.luogu.com.cn/ticket/KJJX824215)。## 前置知识- 回文自动机 PAM介绍详见 [OI Wiki 中的描述](https://oi-wiki.org/string/pam/),此处不作赘述。8 行未改动### 删除操作由于 PAM 结点只增不减(保证 `fail` 指针有效及结点复用),删除时采用状态回滚策略,通过维护历史栈恢复上一状态。### 复杂度分析- **时间复杂度**:$O(Q \log Q)$。PAM 的 `get_fail` 跳转次数均摊为 $O(1)$,但存在单次 $O(Q)$ 的最坏情况;同时,`get_num` 通过 `quicklink` 倍增数组统计节点深度(即回文子串种类数),单次查询为 $O(\log Q)$。本题包含撤销操作,而撤销仅回滚状态指针与历史栈、不触发 `get_fail` 也不新增结点及调用 `get_num`,因此单次操作的时间复杂度上界仍为 $O(Q \log Q)$(可通过计算 `get_fail` 全局跳转次数及倍增查询开销验证),整体均摊复杂度保持 $O(\log Q)$。- **空间复杂度**:$O(Q)$。PAM 结点数最多为 $Q+2$,倍增数组 `quicklink` 与历史栈 `lh`、`th`、`ch` 的空间占用也均为 $O(Q)$($\log$ 因子视为常数),总体为线性空间。## 代码实现```cpp#include <bits/stdc++.h>using namespace std;#define int long long // 十年OI一场空,不开long long见祖宗const int N = 10010;const int AL = 26;const int LOG = 15;struct PAM {int n, last, tot;int tp; // 回文子串总数量int len[N], fail[N];int nextt[N][AL];int quicklink[LOG][N]; // 倍增数组,quicklink[i][x]表示x向上跳2^i步的fail祖先vector<int> lh; // last历史栈,回滚用vector<int> th; // tp历史栈,回滚用vector<int> ch;int s[N];PAM() {init();}void init() {n = 0; last = 0; tot = 2; tp = 0;len[0] = 0; fail[0] = 1;len[1] = -1; fail[1] = 1;s[0] = -1; // 哨兵,防止get_fail越界memset(nextt, 0, sizeof(nextt));memset(quicklink, 0, sizeof(quicklink));// 初始化倍增数组,0和1的fail都指向1(奇根)quicklink[0][0] = 1;quicklink[0][1] = 1;for (int i = 1; i < LOG; i++) {quicklink[i][0] = 1;quicklink[i][1] = 1;}lh.clear(); th.clear(); ch.clear();lh.push_back(last);th.push_back(tp);}2 行未改动x = fail[x];}return x;}int get_num(int x) {int res = 0;for (int i = LOG - 1; i >= 0; i--) {int nxt = quicklink[i][x];if (nxt != 0 && nxt != 1) {res += (1 << i);x = nxt;}}// 最后一步跳到0节点if (x != 0) {res++;}return res;}void add(int c) {s[++n] = c;int cur = get_fail(last);if (!nextt[cur][c]) {int now = tot++;len[now] = len[cur] + 2;fail[now] = (len[now] == 1) ? 0 : nextt[get_fail(fail[cur])][c];nextt[cur][c] = now;quicklink[0][now] = fail[now];for (int i = 1; i < LOG; i++) {quicklink[i][now] = quicklink[i - 1][quicklink[i - 1][now]];}}last = nextt[cur][c];tp += get_num(last);ch.push_back(c);lh.push_back(last);th.push_back(tp);}void remove() {if (n == 0) {return;}ch.pop_back();lh.pop_back();th.pop_back();last = lh.back();tp = th.back();n--;}20 行未改动}return 0;}```