版本比较
2024-12-23 基础字符串算法
正文
增加 16 行删除 16 行
58 行未改动5960616263636465666729 行未改动979899100101101102103104105229 行未改动全部展开33533633733833933934034134234313 行未改动35735835936036136136236336436536636636736836937051 行未改动42242342442542642642742842943042 行未改动473474475476477477478479480481285 行未改动全部展开76776876977077177177277377477598 行未改动87487587687787887887988088188288388488588688688788888989025 行未改动91691791891992092192092192292392492512 行未改动93893994094194294294394494594613 行未改动960961962963964964965966967968148 行未改动全部展开11171118111911201121112111221123112411251126112611271128112911306 行未改动58 行未改动
> II [NOIP2020 字符串匹配](https://www.luogu.com.cn/problem/P7114)
> 给定一个字符串 $S$,求其可以被分解为多少个形如 $(AB)^rC$ 的形态。其中 $F(A)<F(C)$,$F(S)$ 表示字符串 $S$ 中出现奇数次的字符的数量。> >> $T$ 组询问,$1\le T \le 5,1 \le |S| \le 2^{20}$。
这里给出一个用字符串哈希做到 $O(T(n\ln n+n\log{26})$ 的做法。线性做法在后面会提到。
29 行未改动
貌似是最直接的一种应用。
> III [LG2580 于是他错误的点名开始了](https://www.luogu.com.cn/problem/P2580)> >> 给你 $n$ 个字符串以及 $m$ 个询问,每次询问需要回答:如果该询问的字符串没有在 $n$ 个字符串里面出现过,输出“WRONG”,如果该询问的字符串出现过而且是第一次询问,输出“OK”,如果该询问的字符串出现过但不是第一次询问,输出“REPEAT”。
重复的可以考虑在trie上打标记。
229 行未改动全部展开
[code](https://www.luogu.com.cn/record/36844339)
> XVI PKUSC2023 D1T1> >> 给定 $S,T$,对于每个 $i$ 求 $S_i$ 换为 $T_i$ 后 $S$ 的最长 Border。
枚举答案,如果 $S[:i]$ 和 $S[|S|-i+1:]$ 有大于等于三位不同,那么不可能成为答案。如果有两位不同,那一定是替换重叠的一位。如果只有一位不同就替换那一位即可。判断有几位不同使用 lcp 即可。
13 行未改动$R_i$ 的增加和 $mx$ 的增加是同时进行的,所以算法复杂度是 $O(n)$。
> XVII LG6216 回文匹配>> >
> XIX CF30E Tricky and Clever Password>> >
> XX CF17E Palisection>> 给定一个长度为 $n$ 的小写字母串。问你有多少对相交的回文子 串(包含也算相交)。51 行未改动#### 2.2 构造
**定理1**
> $|s|$ 本质不同的回文子串的个数是 $O(|s|)$ 的! > $|s|$ 本质不同的回文子串的个数是 $O(|s|)$ 的!
证明
考虑在 $s$ 后加一个字符 $c$ 形成新的字符串 $sc$,考虑其回文后缀的开始位置有 $l_1,l_2...l_k$,从前往后排序。考虑回文串的性质,$l_i,i>1$ 一定是 $s[l_1...|sc|]$ 的后缀,又其是回文串,所以其一定在前面作为前缀倒着出现过。又 $s[l_i...|sc|]$ 也是一个回文串,所以其倒着正着无所谓,那么它就一定在前面出现过。所以每加一个字符只会增加一个回文串。证毕!42 行未改动> 给定一个字符串 $s$。保证每个字符为小写字母。对于 $s$ 的每个位置,请求出以该位置结尾的回文子串个数。强制在线。$1\le |s|\le 5\times 10^5$
容易发现以某个位置为结尾的回文串为 $s[1...i]$ 的所有回文后缀。也就是不停跳 $fail$ 上的所有节点。所以答案为 $fail$ 链的长度(除去两个根)。
[[APIO2014]回文串](https://www.luogu.com.cn/problem/P3649) [[APIO2014]回文串](https://www.luogu.com.cn/problem/P3649)
> 给你一个字符串,求其所有回文子串出现次数$\times$长度的最大值。$1 \leq |s| \leq 3 \times 10^5$。
首先建出 $s$ 的回文树,然后对于每一个回文子串,记录 $cnt$ 为它出现的次数。285 行未改动全部展开int main() { scanf("%d%s", &len, s + 1); build(); for (int i = 1; i <= len; i++) insert(s[i] - 'a', i); printf("%d", ans); printf("%d", ans); return 0;}```
98 行未改动 while (s[n] != s[n - pt[l].len - 1] || pt[l].len + 2 > pt[v].len / 2) l = pt[l].fail; pt[v].half = pt[l].go[c]; } else pt[v].half = pt[v].fail; pt[p].go[c] = v; dp[v] = pt[v].len; dp[v] = pt[v].len; if (pt[v].len % 2 == 0) { dp[v] = min(dp[v], dp[p] + 1); dp[v] = min(dp[v], dp[pt[v].half] + 1 + pt[v].len / 2 - pt[pt[v].half].len); ans = min(ans, len - pt[v].len + dp[v]); } } lst = pt[p].go[c];
}int main() { scanf("%d", &t); while (t--) {25 行未改动using namespace std;typedef long long ll;struct node{ int go[26], len, fail;}pt[2][500010];//0:A ; 1:B int Len[2], tot[2], lst[2], ans_len, ans_num;//长度 个数 }pt[2][500010];//0:A ; 1:Bint Len[2], tot[2], lst[2], ans_len, ans_num;//长度 个数char s[2][500010];void build(int id) { s[id][0] = -1; pt[id][++tot[id]].len = -1;12 行未改动 lst[id] = pt[id][p].go[c];}void init(int id) { for (int i = 1; i <= Len[id]; i++) {insert(id, s[id][i] - 'a', i); }void solve(int x, int y) { //第一棵树中的编号,第二棵树中的编号 //第一棵树中的编号,第二棵树中的编号 if (pt[0][x].len > ans_len) ans_len = pt[0][x].len, ans_num = 1; else if (pt[0][x].len == ans_len) ans_num++; for (int i = 0; i < 26; i++) if (pt[0][x].go[i] && pt[1][y].go[i]) solve(pt[0][x].go[i], pt[1][y].go[i]);}13 行未改动typedef long long ll;struct node{ int go[26], len, fail; ll cnt;}pt[2][50010];//0:A ; 1:B }pt[2][50010];//0:A ; 1:Bint Len[2], tot[2], lst[2];char s[2][50010];ll ans;void build(int id) {148 行未改动全部展开 pre[i] = i - 2; mark[i] = 0; } }
} if (f[Len] >= 0x3f3f3f3f) { puts("-1"); return 0; } } printf("%d\n", f[Len]); for (int i = Len; i; i = pre[i]) { if (mark[i]) printf("%d %d\n", pre[i] / 2 + 1, i / 2);6 行未改动