后缀数组模板
来源:互联网 发布:java中decode函数 编辑:程序博客网 时间:2024/06/06 11:44
刘汝佳的代码会RE!!!被坑了3小时!
以下模板自带注释
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<vector>using namespace std;#define maxn 100005#define rank RANKint n;char str[maxn];int cnt[maxn];int t1[maxn],t2[maxn];int sa[maxn];int rank[maxn];int height[maxn];void build(){int *x=t1,*y=t2;int m=26;for(int i=0;i<n;i++) cnt[x[i]=(str[i]-'a')]++;for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];for(int i=n-1;i>=0;i--) sa[--cnt[x[i]]]=i;for(int k=1;k<=n;k<<=1){int p=0;for(int i=n-1;i>=n-k;i--) y[p++]=i;for(int i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;//y[i]:position of the suffix having the i-th "next k num",similar to samemset(cnt,0,sizeof(cnt));for(int i=0;i<p;i++) cnt[x[y[i]]]++;for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];for(int i=p-1;i>=0;i--) sa[--cnt[x[y[i]]]]=y[i];//give a higher rank to suffix with larger second keyword when having same first oneswap(x,y);//this time,y stores the informationp=0;for(int i=0;i<n;i++)x[sa[i]]= (!i||y[sa[i]]!=y[sa[i-1]]||y[sa[i]+k]!=y[sa[i-1]+k]) ? \p++:p-1;if(p>=n) break;m=p;}for(int i=0;i<n;i++) rank[sa[i]]=i;int k=0;for(int i=0;i<n;i++){if(k) k--;if(rank[i]==0) continue;int j=sa[rank[i]-1];while(str[i+k]==str[j+k]) k++;height[rank[i]]=k;}}int main(){ //freopen(".in","r",stdin); scanf("%d",&n); scanf("%s",str); build(); for(int i=0;i<n;i++) printf("%d ",sa[i]); putchar('\n'); for(int i=0;i<n;i++) printf("%d ",rank[i]+1); putchar('\n'); for(int i=1;i<n;i++) printf("%d ",height[i]);}
1 0
- 【后缀数组】后缀数组模板
- 后缀数组【模板】
- 【后缀数组模板】
- 后缀数组模板
- 后缀数组模板
- 【模板】后缀数组
- 后缀数组模板
- 后缀数组模板
- 后缀数组模板
- 【省选】【后缀数组】模板
- 后缀数组 模板
- 后缀数组 模板
- 后缀数组模板
- 后缀数组模板
- 后缀数组模板
- 后缀数组模板
- 后缀数组模板
- 模板-后缀数组
- [hadoop]hadoop eclipse plugin安装与配置(四)
- Android使用Cordova框架开发Android Hybrid App
- Best Time to Buy and Sell Stock III
- (二)Hyper如何对DataBlock中的数据进行索引
- 将pgm图片使用Sequence file存储到hdfs,并读取为Mat数组格式
- 后缀数组模板
- java编写网页爬虫(分页——插入数据库——导出数据)
- Sublime Text 3 常用插件以及安装方法
- 定制自己的报表!7款实用开源报表工具
- UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)
- Java经典类库-Guava中的函数式编程讲解
- LeetCode 142. Linked List Cycle II(循环链表)
- Android使用ActionBar搭建Android UI框架
- iOS压缩图片