用SAM实现后缀排序
因为本人几乎不会后缀数组,所以遇到这种SA的模板题也要拿SAM解决。
还是有一点思维难度的。
首先按照国际惯例,建反串的SAM。
然后对于这个反串,我们考虑两个前缀哪一个字典序小:因为是串是反的,所以要从后往前比较,那么第一个不相同的字符一定是两个前缀在后缀树上的节点的lca的前一位。记其中一个节点的任意一个endpos的位置是\(End[i]\),lca的长度是\(len[x]\),那么这个字符就是\(s[n - (End[i] - len[x])]\)。
这样对于后缀树上的每一个节点,定义\(tp[i] = s[n - (End[i] - len[link[i]])] - 'a'\),然后以\(tp[i]\)为关键字进行基数排序就好了。
最后建出一棵新的树,在上面dfs一遍即可。
luogu的板儿字符集太大,会MLE,用map又TLE了,而且不想手写哈希表,于是就没放链接。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1.1e6 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen("ha.in", "r", stdin);
freopen("ha.out", "w", stdout);
#endif
}
int n;
char s[maxn];
int ans[maxn], acnt = 0;
struct Edge
{
int nxt, to;
}e[maxn << 1];
int head[maxn << 1], ecnt = -1;
In void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
}
struct Sam
{
int las, cnt;
int tra[maxn << 1][27], link[maxn << 1], len[maxn << 1], id[maxn << 1], End[maxn << 1];
In void init() {id[0] = link[las = cnt = 0] = -1;}
In void insert(int c, int x)
{
int now = ++cnt, p = las;
End[now] = len[now] = len[p] + 1, id[now] = x;
while(~p && !tra[p][c]) tra[p][c] = now, p = link[p];
if(p == -1) link[now] = 0;
else
{
int q = tra[p][c];
if(len[q] == len[p] + 1) link[now] = q;
else
{
int clo = ++cnt; id[clo] = -1;
memcpy(tra[clo], tra[q], sizeof(tra[q]));
len[clo] = len[p] + 1, End[clo] = len[q];
link[clo] = link[q], link[q] = link[now] = clo;
while(~p && tra[p][c] == q) tra[p][c] = clo, p = link[p];
}
}
las = now;
}
int tp[maxn << 1], pos[maxn << 1], buc[maxn << 1];
In void solve()
{
for(int i = 1; i <= cnt; ++i) tp[i] = s[n - End[i] + len[link[i]]] - 'a', ++buc[tp[i]];
for(int i = 1; i <= cnt; ++i) buc[i] += buc[i - 1];
for(int i = 1; i <= cnt; ++i) pos[buc[tp[i]]--] = i;
for(int i = cnt; i; --i) addEdge(link[pos[i]], pos[i]);
//按tp的权值从大到小加边,这样dfs的时候就是从小到大
}
In void dfs(int now)
{
if(~id[now]) ans[++acnt] = id[now];
forE(i, now, v) dfs(v);
}
In void _Print()
{
for(int i = 1; i <= cnt; ++i) printf("now:%d fa:%d len:%d End:%d tp:%d\n", i, link[i], len[i], End[i], tp[i]);
}
}S;
int main()
{
// MYFILE();
Mem(head, -1);
scanf("%s", s);
n = strlen(s); S.init();
for(int i = n - 1; i >= 0; --i) S.insert(s[i] - 'a', i);
S.solve(), S.dfs(0);
// S._Print();
for(int i = 1; i <= acnt; ++i) write(ans[i]), space; enter;
return 0;
}
用SAM实现后缀排序的更多相关文章
- 【sam复习】用sam实现后缀排序
没错,一定是无聊到一定境界的人才能干出这种事情. 这个无聊的zcysky已经不满足于用后缀平衡树求sa了,他想用sam试试. 我们回顾下sam的插入过程,如果我们从最后一个state沿着suffix ...
- LG3809 【模板】后缀排序
题意 题目背景 这是一道模板题. 题目描述 读入一个长度为 $ n $ 的由大小写英文字母或数字组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的 ...
- codevs1500 后缀排序
题目描述 Description 天凯是MIT的新生.Prof. HandsomeG给了他一个长度为n的由小写字母构成的字符串,要求他把该字符串的n个后缀(suffix)从小到大排序. 何谓后缀?假设 ...
- UOJ#35 后缀排序
这是一道模板题. 读入一个长度为 n 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置.位置编号为 1 到 n. 除此之外为 ...
- P3809 【模板】后缀排序
P3809 [模板]后缀排序 从这学的 后缀数组sa[i]就表示排名为i的后缀的起始位置 x[i]是第i个元素的第一关键字 y[i]表示第二关键字排名为i的数,在第一关键字中的位置 #include& ...
- 2018.11.24 loj#111. 后缀排序(后缀数组)
传送门 后缀排序模板题. 终于会后缀数组了(然而只会倍增并不会DC3DC3DC3). 在这里列举几个数组的意思: sai:sa_i:sai:当前排名第iii的后缀的起始下标. rkirk_irki ...
- uoj35 后缀排序
题目链接:http://uoj.ac/problem/35 这是一道模板题. 读入一个长度为 n 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第 ...
- codevs 1500 后缀排序
codevs 1500 后缀排序 http://codevs.cn/problem/1500/ 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 天凯是MI ...
- 洛谷:P3809 【模板】后缀排序(后缀数组模板)
P3809 [模板]后缀排序 题目链接:https://www.luogu.org/problemnew/show/P3809 题目背景 这是一道模板题. 题目描述 读入一个长度为 nn 的由大小写英 ...
随机推荐
- Feign 失败降级未生效和超时配置优先级问题
一.问题: 生产环境服务A 通过feign调用 服务B,服务A报警信息如下: 详细分析发现问题 (1)服务A调用服务B失败,未触发声明的失败降级操作 (2)同时配置ribbon和feign超时时间,优 ...
- [洛谷P3966][TJOI2013]单词
题目大意:有$n$个字符串,求每个字符串在所有字符串中出现的次数 题解:$AC$自动机,每个节点被经过时$sz$加一,每一个字符串出现次数为其$fail$树子树$sz$和 卡点:$AC$自动机根节点为 ...
- docker容器日志管理(清理)
原文:docker容器日志管理(清理) 前言 在使用docker容器时候,其日志的管理是我们不得不考虑的事情.因为docker容器的日志文件会占据大量的磁盘空间.下面介绍的就是对docker容器日志的 ...
- MVC-09安全
部分8:添加安全. MVC应用程序安全性 Models文件夹包含表示应用程序模型的类. Visual Web Developer自动创建AccountModels.cs文件,该文件包含用于应用程序认证 ...
- 隐马尔可夫模型(HMM)的分类
1.遍历型(ergodic model) 即每个状态都可以由任意一个状态演变而来,aij>0,for all i , j. 如图: 2.left-right type of HMM 每个状态只能 ...
- 【洛谷 P2633】 Count on a tree(主席树,树上差分)
题目链接 思维难度0 实现难度7 建出主席树后用两点的状态减去lca和lca父亲的状态,然后在新树上跑第\(k\)小 #include <cstdio> #include <cstr ...
- vue项目在ie中空白问题
vue项目在ie浏览器中出现空白,f12打开后发现在body下面就只有一个div盒子,因此我们可以猜测就是js没有引入导致的,所有网上看了一些相关的才知道,在ie中无法解析es6或者版本更高的语法,所 ...
- day33-python之多线程
1.多线程实例 # import threading # import time # # import threading import time class MyThread(threading.T ...
- vi / vim 基本操作
进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi n filename :打开文件,并将光标置于第n行首 vi filename :打开 ...
- HashMap,HashSet
HashMap,HashSet 摘自:https://www.cnblogs.com/skywang12345/p/3310887.html#a1 目录 一. HashMap(键值对形式存取,键 ...