浅谈字符串
尝试写出最通俗的字符串学习笔记。
哈希
单哈希
别急。
cout << (lint)1e9 + 123 << endl;
cout << (lint)1e15 + 37 << endl;
cout << 29*(1ll<<57) + 1 << endl;
cout << __LONG_LONG_MAX__ << endl;
kmp
给定主串
s 和模式串t ,求t 在s 中出现的位置和t 所有非空前缀的最长 border 长度。
\texttt{solve}
对于
设
我们定义
暴力匹配主串
考虑
当
例如对于
不难发现本质是将
- 匹配到
i=j=3 :
- 此时发现最长可以跳的位置为
1 :
- 跳然后继续匹配:
- 发现刚好完全匹配:
- 输出后跳到
\text{next} 继续匹配:
现在只需要考虑如何求
对于
- 若
t_{i+1}=t_{j+1} ,令\text{next}_{t,j+1}\gets i+1 ,跳出循环; - 若
i=0\land t_{i+1}\ne t_{j+1} ,令\text{next}_{t,j+1}\gets0 ,跳出循环; - 令
i\gets\text{next}_{t,i} ,跳到第一步操作。
这个跳
例如
可以自己手模一下更多例子便于理解。
\texttt{prove}
因为求 border 相当于自己和自己匹配,所以我们只需要证明一个即可。
解法正确性证明
匹配到
我们可以使用反证法。我们钦定现在
如果存在
当我们匹配到中间,我们可以把
时间复杂度证明
单看
考虑
所以匹配是
总时间复杂度
\texttt{code}
::::info[
#include <bits/stdc++.h>
// #define int long long
#define lint long long
#define endl '\n'
using namespace std;
const int N = 1e6+5;
int n,m;
string s,t;
int nxt[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("app.in","r",stdin);
// freopen("app.out","w",stdout);
cin >> s >> t;
n = s.size(),s = "-" + s;
m = t.size(),t = "-" + t;
for(int j = 1; j < m; j++){
int i = nxt[j];
while(true){
if(t[i+1] == t[j+1]){
nxt[j+1] = i+1;
break;
}
if(!i) break;
i = nxt[i];
}
}
int j = 0;
for(int i = 0; i < n; i++){
while(true){
if(s[i+1] == t[j+1]) break;
if(!j) break;
j = nxt[j];
}
if(s[i+1] == t[j+1]) j++;
if(j == m){
cout << i-j+2 << endl; // 匹配成功的是 (i+1,j)
j = nxt[j];
}
}
for(int i = 1; i <= m; i++)
cout << nxt[i] << " ";
cout << endl;
return 0;
}
::::
manacher(马拉车)
求
s 的最长回文串。
\texttt{solve}
先来启发一下思路。
正确性是极其显然的,我们需要证明的是时间复杂度。
我们主要考虑的是
否则会和对称性矛盾(若
综上,时间复杂度为
\texttt{code}
::::info[
#include <bits/stdc++.h>
// #define int long long
#define lint long long
#define endl '\n'
using namespace std;
const int N = 1.1e7+5;
int n,p[N<<1];
string s,t = ".&";
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("app.in","r",stdin);
// freopen("app.out","w",stdout);
cin >> s;
n = s.size();
for(int i = 0; i < n; i++){
t.push_back(s[i]);
t.push_back('-');
}
t[t.size()-1] = '#';
n = t.size()-1,s = t;
// n = s.size();
// for(int i = 0; i < n; i++){
// t += s[i];
// t += "-";
// }
// t[t.size()-1] = '#';
// n = t.size()-1,s = t;
// int idx = s.size() - 1;
// while(idx > 0){
// s.insert(idx,"-");
// idx--;
// }
// s = "&" + s + "#";
// n = s.size(),s = "." + s;
int mid = 1,r = 1;
for(int i = 2; i < n; i++){
if(i > r){
int x,y;
x = y = i;
while(s[x-1] == s[y+1]){
p[i]++;
x--,y++;
}
mid = i,r = y;
}else if(mid < i && i <= r){
p[i] = p[(mid << 1) - i];
if(i + p[i] == r){
int x = i-p[i],y = i+p[i];
while(s[x-1] == s[y+1]){
p[i]++;
x--,y++;
}
mid = i,r = y;
}
else if(i + p[i] > r) p[i] = r - i;
}
}
int ans = 1;
for(int i = 1; i <= n; i++)
ans = max(ans,p[i] + (s[i+p[i]] != '-'));
cout << ans << endl;
return 0;
}
::::
\texttt{tips}
代码中写了三种预处理
有很多 string 函数包括 + 这个运算符本质是
Trie 树(字典树)
我们钦定一个虚构的根,建一个二十六叉树。插入一个字符串
这样我们可以大幅度的利用字符串集的公共前缀来优化查找效率。
最优当然是直接用链式前向星存图,这里介绍一种类似的但是相对好理解的存图方式。
我们用一个 vector 套结构体,结构体里面开一个长度为
新塞进一个节点
我们发现,并不一定字符串的结尾都是叶子结点(例如
我们钦定
解法正确性证明
不妨思考一下对于一个串
我们设
证明很简单,我们设第
同理可得
综上,
即
那么
这么看,求
时间复杂度证明
没什么好证明的。建图是
\texttt{code}
注意到我把根设为了
注意到 border 不能为本身,所以求
::::info[
#include <bits/stdc++.h>
// #define int long long
#define lint long long
#define uint unsigned int
#define endl '\n'
using namespace std;
const int N = 1e6+5;
struct node{
int ne,to;
}e[N<<1];
int cntE,pre[N];
int n,m,p,q;
string s;
int nxt[N],lb[N];
int fa[N],dep[N];
inline void add(int x,int y){
e[++cntE] = (node){pre[x],y};
pre[x] = cntE;
}
inline void dfs(int x,int father){
fa[x] = father,dep[x] = dep[father] + 1;
int p = lb[father],q = lb[p];
lb[x] = (dep[fa[x]]-dep[p]==dep[p]-dep[q] ? q : fa[x]);
for(int i = pre[x]; i; i=e[i].ne){
int y = e[i].to;
if(y != father) dfs(y,x);
}
}
inline int lca(int x,int y){
x = fa[x],y = fa[y];
if(dep[x] < dep[y]) swap(x,y);
while(dep[x] > dep[y])
x = (dep[lb[x]]>=dep[y] ? lb[x] : fa[x]);
while(x != y){
if(lb[x] == lb[y]) x = fa[x],y = fa[y];
else x = lb[x],y = lb[y];
}
return x;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
// freopen("app.in","r",stdin);
// freopen("app.out","w",stdout);
cin >> s;
n = s.size();
s = "-" + s;
for(int i = 2; i <= n; i++){
nxt[i] = nxt[i-1];
while(true){
if(s[nxt[i]+1] == s[i]){
nxt[i]++;
break;
}
if(!nxt[i]) break;
nxt[i] = nxt[nxt[i]];
}
}
for(int i = 1; i <= n; i++){
add(i+1,nxt[i]+1);
add(nxt[i]+1,i+1);
}
dfs(1,0);
cin >> m;
while(m--){
cin >> p >> q;
cout << lca(p+1,q+1)-1 << endl;
}
return 0;
}
::::
AC 自动机
\texttt{case 1}
给定主串
s 和若干模式串\{t_i\} ,求多少个模式串在主串中出现过。