正文
增加 35 行删除 8 行
17 行未改动1819202122232425262728293031323334353637383940413 行未改动45464748495051525354552 行未改动585960616263646566676869707172737475767712 行未改动90919293949596979814 行未改动17 行未改动由于 PAM 结点只增不减(保证 `fail` 指针有效及结点复用),删除时采用状态回滚策略,通过维护历史栈恢复上一状态。### 复杂度分析- 时间复杂度:$O(Q)$。PAM 的 `get_fail` 跳转次数均摊为 $O(1)$,但存在单次 $O(Q)$ 的最坏情况。本题包含撤销操作,而撤销仅回滚状态指针、不触发 `get_fail` 也不新增结点,因此单次操作的时间复杂度上界仍为 $O(Q)$(可通过计算 `get_fail` 全局跳转次数验证),整体均摊复杂度保持 $O(1)$。- **空间复杂度**:$O(Q)$。PAM 结点数最多为 $Q+2$,栈空间也为 $O(Q)$。## 代码实现```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]; // 以该节点结尾的回文后缀个数int nextt[N][AL];vector<int> lh; // last历史栈,回滚用vector<int> th; // tp历史栈,回滚用vector<int> ch;int s[N];3 行未改动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);}12 行未改动return tp;}};PAM p;signed main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int q;cin >> q;14 行未改动17 行未改动181920212223242526272829303132333435363738394041423 行未改动4647484950515253545556575859606162632 行未改动6667686970717273747576777879808182838485868788899091929394959697989910010110210310412 行未改动11711811912012112212312412514 行未改动17 行未改动由于 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];3 行未改动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);}12 行未改动return tp;}};PAM p;signed main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int q;cin >> q;14 行未改动