SPOJ 694 DISUBSTR - Distinct Substrings
思路
求本质不同的子串个数,总共重叠的子串个数就是height数组的和
总子串个数-height数组的和即可
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
const int MAXN = 100000;
using namespace std;
int height[MAXN],sa[MAXN],ranks[MAXN],barrel[MAXN],n;
struct Node{
int pos,r[2];
}midx[MAXN],x[MAXN];
char s[MAXN];
int c_sort(int n,int lim){
for(int i=0;i<2;i++){
memset(barrel,0,sizeof(barrel));
for(int j=1;j<=n;j++)
barrel[x[j].r[i]]++;
for(int j=1;j<=lim;j++)
barrel[j]+=barrel[j-1];
for(int j=n;j>=1;j--)
midx[barrel[x[j].r[i]]--]=x[j];
for(int j=1;j<=n;j++)
x[j]=midx[j];
}
ranks[x[1].pos]=1;
int cnt=1;
for(int i=2;i<=n;i++){
if(x[i].r[0]==x[i-1].r[0]&&x[i].r[1]==x[i-1].r[1])
ranks[x[i].pos]=cnt;
else
ranks[x[i].pos]=++cnt;
}
return cnt;
}
void cal_sa(int n){
for(int i=1;i<=n;i++)
x[i]=(Node){i,s[i],0};
int cnt=c_sort(n,1500);
for(int i=1;cnt<n;i<<=1){
for(int j=1;j<=n;j++)
x[j]=(Node){j,(i+j<=n)?ranks[i+j]:0,ranks[j]};
cnt=c_sort(n,cnt);
}
for(int i=1;i<=n;i++)
sa[ranks[i]]=i;
for(int i=1,j=0,k;i<=n;height[ranks[i++]]=j)
for(j?j--:0,k=sa[ranks[i]-1];s[i+j]==s[j+k];j++);
}
void init(void){
memset(sa,0,sizeof(sa));
memset(height,0,sizeof(height));
memset(ranks,0,sizeof(ranks));
memset(x,0,sizeof(x));
memset(midx,0,sizeof(midx));
}
signed main(){
int T;
scanf("%lld",&T);
while(T--){
init();
scanf("%s",s+1);
n=strlen(s+1);
cal_sa(n);
int sum=0;
for(int i=2;i<=n;i++)
sum+=height[i];
printf("%lld\n",(n+1)*n/2-sum);
}
return 0;
}
SPOJ 694 DISUBSTR - Distinct Substrings的更多相关文章
- SPOJ 694&&SPOJ705: Distinct Substrings
DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其 ...
- 【SPOJ 694】Distinct Substrings 不相同的子串的个数
不会FQ啊,没法评测啊,先存一下代码QAQ 2016-06-16神犇Menci帮我测过AC了,谢谢神犇Menci QwQ #include<cstdio> #include<cstr ...
- 【SPOJ 694】Distinct Substrings (更直接的求法)
[链接]h在这里写链接 [题意] 接上一篇文章 [题解] 一个字符串所有不同的子串的个数=∑(len-sa[i]-height[i]) [错的次数] 0 [反思] 在这了写反思 [代码] #inclu ...
- 【SPOJ 694】Distinct Substrings
[链接]h在这里写链接 [题意] 给你一个长度最多为1000的字符串 让你求出一个数x,这个x=这个字符串的不同子串个数; [题解] 后缀数组题. 把原串复制一份,加在 ...
- SPOJ 694 || 705 Distinct Substrings ( 后缀数组 && 不同子串的个数 )
题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀 ...
- DISUBSTR - Distinct Substrings
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- 后缀数组:SPOJ SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
随机推荐
- clientWidth,offsetWidth,scrollWidth区别
<html> <head> <title>clientWidth,offsetWidth,scrollWidth区别</title> </head ...
- word2vector 资料
http://blog.csdn.net/garfielder007/article/details/51345201 https://cs224d.stanford.edu/lecture_note ...
- jQuery筛选--first()和last()
first() 概述 获取匹配的第一个元素 last() 概述 获取匹配的最后个元素 <!DOCTYPE html> <html> <head> < ...
- QtCreator 调试源码
[1]安装源码 声明:要想调试进入Qt源码,必须首先保证我们安装了Qt源码.下面说明安装Qt源码注意事项. 一般安装过程(默认不安装源码): 安装源码过程(需要自己设置,点击“全选”): 综上所述:Q ...
- Python使用闭包结合配置自动生成函数
背景 在构建测试用例集时,常常需要编写一些函数,这些函数接受基本相同的参数,仅有一个参数有所差异,并且处理模式也非常相同.可以使用Python闭包来定义模板函数,然后通过参数调节来自动化生产不同的函数 ...
- Saiku + Kylin 多维分析平台探索
背景 为了应对各种数据需求,通常,我们的做法是这样的: 对于临时性的数据需求:写HQL到Hive里去查一遍,然后将结果转为excel发送给需求人员. 对于周期性的.长期性的数据需求:编写脚本,结合Hi ...
- Spring boot 问题总结
1. Spring boot 嵌入的tomcat不能启动: Unregistering JMX-exposed beans on shutdown 在官网(http://start.spring. ...
- Spring整合Shiro
apache shiro 是一个安全认证框架,和 spring security 相比,在于他使用了比较简洁易懂的 认证和授权方式.其提供的 native-session(即把用户认证后的授权信息保存 ...
- 转:Image与byte之间互转
#region<Image 与 byte之间互转> /// <summary> /// 将一个byte转换成一个Bitmap对象 /// </summary> // ...
- Vuejs vm对象详解
Vuejs vm对象详解 vue数据是怎么驱动视图的?一堆数据放在那里是不会有任何作用的,它必须通过我们的View Model(视图模型)才能操控视图. 图中的model其实就是数据,一般我们写成js ...