当前位置:网站首页>后缀数组模版代码解析
后缀数组模版代码解析
2022-04-21 10:49:00 【Jinze_L】
今天看了后缀数组的模版代码,终于看懂了模版的每个部分所实现的功能,但是具体细节还没完全理解,看来暂时只能套模版了,下面是模版及注释;
/*
sa[i] : 表示 排在第i位的后缀 起始下标
rank[i] : 表示后缀 suffix(i)排在第几
RANK表示你排第几 SA表示排第几的是谁 (记住这个就行)
height[i] : 表示 sa[i-1] 与 sa[i] 的LCP 值,也就是排名相邻的两个后缀的最长公共前缀
h[i]: 等于Height[Rank[i]],表示 suffix(i)与其排名前一位的 LCP值
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define maxn 20010
#define ws wss
int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
//用于比较第一关键字与第二关键字,
// 比较特殊的地方是,预处理的时候,r[n]=0(小于前面出现过的字符)
void da(int *r,int *sa,int n,int m)//此处N比输入的N要多1,为人工添加的一个字符,用于避免CMP时越界
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) ws[i]=0;
for(i=0;i<n;i++) ws[x[i]=r[i]]++;
for(i=1;i<m;i++) ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;//预处理长度为1
//上面四行就是把长度为1的前缀按基数排序的过程
for(j=1,p=1;p<n;j*=2,m=p)//通过已经求出的长度J的SA,来求2*J的SA
//j是前缀长度, p是不同大小的后缀数量, m为大于所有值的值
{
for(p=0,i=n-j;i<n;i++) y[p++]=i;// 特殊处理没有第二关键字的
for(i=0;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
// 上面代码使y保存了以第二关键字排序的结果 ,以第二关键字排序结果可利用上一个sa数组
for(i=0;i<n;i++) wv[i]=x[y[i]];
for(i=0;i<m;i++) ws[i]=0;
for(i=0;i<n;i++) ws[wv[i]]++;
for(i=1;i<m;i++) ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];//以第一关键字排序,基数排序部分
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++) //交换x和y,rank值保存在x数组中,p为不同串的个数,更新名次数 组x[],注意判定相同的
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return ;
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n){ // 此处N为实际长度
int i,j,k=0; // height[]的合法范围为 1-N, 其中0是结尾加入的字符
for(i=1;i<=n;i++) rank[sa[i]]=i;// 根据SA求RANK
for(i=0;i<n;height[rank[i++]]=k)// 定义:h[i] = height[ rank[i] ]
for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);//根据 h[i] >= h[i-1]-1 来优化计算height过程
return ;
}
int main()
{
int sa[20010];
int n,i,j,k;
int t1,t2;
n=8;
int note[9]={1,1,2,1,1,1,1,2};
da(note,sa,n+1,4); //注意区分此处为n+1,因为添加了一个结尾字符用于区别比较
calheight(note,sa,n);
for(i=1;i<=n;i++)
cout<<sa[i]<<endl;
cout<<".."<<endl;
for(j=1;j<=n;j++)
cout<<height[j]<<endl;
return 0;
}
注释解释了哪个模块具体实现什么功能,这体现了c++语言的封装性,其实如果程序语句很难理解的话,权宜之计是尽快明白哪个部分实现了哪个功能,能运用些模块实现哪些操作,具体原理过程在不断运用中加深理解。
版权声明
本文为[Jinze_L]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_35739903/article/details/79243079
边栏推荐
- Basic commands of MySQL
- Mysql的基本命令使用
- 24 pictures to conquer border image
- NFT中的版权漏洞:产品设计需考虑法律层面
- 两两数之和
- 润和大禹学习方案
- 第一个GO程序
- The prospectus of quwan group is "invalid", and its TT voice has been taken off the shelf. How to achieve stable growth?
- TypeError: The view function did not return a valid response. The function either returned None 的解决
- Digital Economy & Green Innovation matching: Green Patent matching data of digital finance enterprises of 3169 companies in China, 2011-2019
猜你喜欢

uniapp 微信小程序 点击按钮调用微信支付

Digital Economy & Green Innovation matching: Green Patent matching data of digital finance enterprises of 3169 companies in China, 2011-2019

Runhe Dayu learning program

torch. autograd. Function customization

Six practices of Windows operating system security attack and defense

AcWing 1725. Team tic tac toe game (enumeration)

基于润和大禹开发板的导购系统项目方案

Android learning ① various reasons why Android cannot connect to MySQL database and Solutions

Vulnhub PRIME: 1

本地IP地址使用域名访问
随机推荐
A collection of econometric models and empirical Stata codes, with top publication examples
Mysql 002 DDL
Talk about your GC tuning ideas?
MySQL in-depth study (30): database design specification
两两数之和
L1-045 cosmic invincible greeting (5 points)
Uniapp wechat applet clicks the button to call wechat payment
安装nfs文件系统
Convbert: improving Bert with span based dynamic revolution
C语言 字符串
An error occurred while processing your request... enable the Development environment by setting ...
看完这篇 教你玩转渗透测试靶机vulnhub——DC9
[ladder race] is l3-010 a complete binary search tree
canvas 学习笔记
递归函数 C语言 题型
Go环境的搭建
Basic commands of MySQL
Browser plug-in (BD new tab) wallpaper appreciation
Recursive function C language question type
GO 使用channel进行同步 (缓冲channel)