伪人科技:SA-IS
GenshinPlayer123 · · 个人记录
闲聊
最难绷的一集。我从来没想过在会SAM的情况下需要会这个东西。
起因是我用 SAM 存1e7就爆炸了,原因是开不了1e7个umap,所以来写SA-IS。其实非常好写,只有2.7K。
顺便一提为啥 SA 这么能优化呢?因为字符集是
后缀排序有一个比较搞笑的做法,就是你直接把前
一些准备工作
假设我们要对
我们规定
为了方便起见,我们在字符串 #,定义该字符的字典序小于所有字符集中的字典序。
1. 后缀类型
我们定义后缀 # 对应的后缀,后缀类型为
我们将
以下是一个我贺的例子:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
S[i]: m m i i s s i i s s i i p p i i #
t[i]: L L S S L L S S L L S S L L L L S
引理 1.1:若
证明:首字母不同时其大小关系直接决定对应后缀的大小关系。
引理 1.2:若
证明:由于
有了这两个引理,我们就可以从后往前递推在
后缀类型,有什么用呢?请看下面的引理:
引理 1.3(后缀类型引导排序):若两个后缀
证明:我们设
这条引理告诉我们,对于相同首字母的后缀,其在
观察
2. LMS 子串
我们定义一个后缀
以下是一个我贺的例子:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
S[i]: m m i i s s i i s s i i p p i i #
t[i]: L L S S L L S S L L S S L L L L S
* * * *
我们定义一个 # 也是一个
在该例子中,所有的 iissi,iissi,iippii#,#。
引理 2.1:一个字符串中的
证明:考察 # 对应的后缀以外,每个
引理 2.2:一个字符串中的
证明:每个字符最多在两个
有了引理
在对两个
将所有
我们将所有
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
S[i]: m m i i s s i i s s i i p p i i #
t[i]: L L S S L L S S L L S S L L L L S
* * * *
新名称 2 2 1 0
S1: 2 2 1 0
引理 2.3:两个
证明:考察什么情况下两个
考察 S...SL...LS。若在 S 将是一个
否则其某一位一定后缀类型不同。根据引理 1.3我们可知相同首字母不同类型的后缀,
根据以上全部内容,易知除非两个
接下来,我们假装你已经求出了
3.从SA1诱导至SA
我称之为顶级智斗。
既然首字母相同的
之后我们惊奇的发现,所有的
我们观察
而这个过程是无需数据结构优化的。由于所有
而对于所有的
整个过程可以被描述为:
-
加入所有
SA_1 -
顺序扫描所有桶,尝试拓展
L 。 -
倒序扫描所有桶,尝试拓展
S 。
由于递归时问题规模减半,而在单层时间复杂度
使用相同的原理,在将
使用以上全部算法,我们就得到了一个时空
以下是一个能通过 【模板】后缀排序 的代码。
#include<bits/stdc++.h>
using namespace std;
const int MX = 5e6 + 7;
#define S_TYPE 1
#define L_TYPE 0
vector<int > SAIS(vector<int > s){
int len = s.size() - 1,sz = 0;
for(auto it : s)sz = max(sz,it);
vector<bool > type;type.resize(len + 1,0);
vector<int > buc,bucL,bucS;
buc.resize(sz + 1,0);bucL.resize(sz + 1,0);bucS.resize(sz + 1,0);
for(int i = 0;i <= len;i++)buc[s[i]]++;
bucL[0] = bucS[0] = 0;
for(int i = 1;i <= sz;i++){
buc[i] += buc[i - 1];
bucL[i] = buc[i - 1];
bucS[i] = buc[i] - 1;
}
type[len] = S_TYPE;
for(int i = len - 1;i >= 0;i--)type[i] = (s[i] != s[i + 1]) ? (s[i] < s[i + 1]) : type[i + 1];
vector<int > LMSpos;
for(int i = 1;i <= len;i++)
if(type[i] == S_TYPE && type[i - 1] == L_TYPE)LMSpos.push_back(i);
vector<int > SA;SA.resize(s.size(),-1);
for(auto it : LMSpos)SA[bucS[s[it]]--] = it;
for(int i = 0;i <= len;i++){
int c = SA[i] - 1;
if(c >= 0 && type[c] == L_TYPE)SA[bucL[s[c]]++] = c;
}
for(int i = 1;i <= sz;i++)bucS[i] = buc[i] - 1;
for(int i = len;i >= 0;i--){
int c = SA[i] - 1;
if(c >= 0 && type[c] == S_TYPE)SA[bucS[s[c]]--] = c;
}
vector<int > name;name.resize(len + 1,0);
int lasx = -1,namecnt = 1;
bool flg = false;
for(int i = 1;i <= len;i++){
int x = SA[i];
if(x > 0 && type[x] == S_TYPE && type[x - 1] == L_TYPE){
if(lasx >= 0){
bool equ = true;
int a = lasx,b = x;
do{
if(s[a] != s[b]){
equ = false;break;
}
if(type[a] != type[b]){equ = false;break;}
a++;b++;
}while(!(a > 0 && type[a] == S_TYPE && type[a - 1] == L_TYPE) && !(b > 0 && type[b] == S_TYPE && type[b - 1] == L_TYPE));
equ &= s[a] == s[b];
equ &= type[a] == type[b];
if(!equ)namecnt++;
}
if(lasx >= 0 && namecnt == name[lasx])flg = true;
name[lasx = x] = namecnt;
}
}
name[len] = 0;
vector<int > S1,SA1;
for(auto it : LMSpos)S1.push_back(name[it]);
if(!flg){
SA1.resize(S1.size(),0);
for(int i = 0;i < S1.size();i++)SA1[S1[i]] = i;
}
else SA1 = SAIS(S1);
bucL[0] = bucS[0] = 0;
for(int i = 1;i <= sz;i++){
bucL[i] = buc[i - 1];
bucS[i] = buc[i] - 1;
}
reverse(SA1.begin(),SA1.end());fill(SA.begin(),SA.end(),-1);
for(auto it : SA1)SA[bucS[s[LMSpos[it]]]--] = LMSpos[it];
for(int i = 0;i <= len;i++){
int c = SA[i] - 1;
if(c >= 0 && type[c] == L_TYPE)SA[bucL[s[c]]++] = c;
}
for(int i = 1;i <= sz;i++)bucS[i] = buc[i] - 1;
for(int i = len;i >= 0;i--){
int c = SA[i] - 1;
if(c >= 0 && type[c] == S_TYPE)SA[bucS[s[c]]--] = c;
}
return SA;
}
int main(){
string s;cin >> s;vector<int > chs;
for(int i = 0;i < s.size();i++)chs.push_back(s[i]);chs.push_back(0);
vector<int > sa = SAIS(chs);
for(int i = 1;i <= s.size();i++)cout << sa[i] + 1 << ' ';
cout << '\n';
}
实测在