用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 的由大小写英 ...
随机推荐
- (四)pdf的构成之文件体(树图)
pdf的文件体类似于一个大树 有个根对象(catalog),该对象中保存着PDF的很多基本信息,并通过间接引用,辐射到所有的间接对象. (下图是大概的树形状)
- redis订阅与发布系统
一.概述 1.redis通过publish.subscribe等命令实现了订阅与发布模式. 2.这个功能提供两种信息机制,分别是订阅/发布到频道和订阅/发布到模式. 二.频道的订阅与信息发送 1.re ...
- Java8一Lambda与函数式接口
关于Lambda表示在工作学习中会经常用到,但并没有全面的去了解.在这里做一个较为详细的记录供以后学习查阅.主要参考Java 8 Lambda 表达式 引言 Java8之前,我们在使用Runnale创 ...
- 温故知新-网络基础1-ip地址计算
网络地址换算 1.网络地址分类 A类:0.0.0.0到127.255.255.255 255.0.0.0/8 B类:128.0.0.0到191.255.255.255 255.255.0.0/16 C ...
- Docker2 docker commit方法镜像制作
一.前期准备 1.下载一个centos镜像,进入容器,安装wget docker pull centos docker run -it centos bash [root@web1 ~]# docke ...
- WebSocket简单使用
很简单就不写太多介绍了,仅展示服务端代码!! Configure在Startup类的方法中添加WebSockets中间件 app.UseWebSockets(); 可以配置以下设置: KeepAliv ...
- Eclipse开发环境(二):配置
二.高级设置 1. 工作空间 在打开软件时,会提示用户选择工作空间,之后在Eclipse中创建的项目都会保存在这个工作空间(目录)下面. 在此把工作空间定为D:\workspaces\eclipse- ...
- python day4 元组/字典/集合类知识点补充
目录 python day4 元组/字典/集合类知识点补充 1. 元组tuple知识点补充 2. 字典dict的知识点补充 3. 基本数据类型set 4. 三元运算,又叫三目运算 5. 深复制浅复制 ...
- SpringBoot中LocalDatetime作为参数和返回值的序列化问题
欢迎访问我的个人网站 https://www.zhoutao123.com 本文原文地址 https://www.zhoutao123.com/#/blog/article/59 LocalDatet ...
- LeetCode算法01 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...