spoj694--Distinct Substrings
个人第一道后缀数组题目。
对于每一个后缀suffix(i),都有len-sa[i]个前缀(也即有len-sa[i]个不同的字符串),其中与排名前一位的后缀有height[i]个共同的前缀,最后所得到的新的字符串个数为len-sa[i]-height[i].因此这题只要求出sa以及height即可求得答案。
#include<stdio.h>
#include<string.h>
#define maxn 100010
int wa[maxn];
int wb[maxn];
int wv[maxn];
int wsa[maxn];
int ranks[maxn],height[maxn]; void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=;i<=n;i++) ranks[sa[i]]=i;
for(i=;i<n ;height[ranks[i++]]=k)
for(k?k--:,j=sa[ranks[i]-];r[i+k]==r[j+k];k++);
return;
} int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}; void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=;i<m;i++) wsa[i]=;
for(i=;i<n;i++) wsa[x[i]=r[i]]++;
for(i=;i<m;i++) wsa[i]+=wsa[i-];
for(i=n-;i>=;i--) sa[--wsa[x[i]]]=i;
for(j=,p=;p<n;j*=,m=p)
{
for(p=,i=n-j;i<n;i++) y[p++]=i;
for(i=;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=;i<n;i++) wv[i]=x[y[i]];
for(i=;i<m;i++) wsa[i]=;
for(i=;i<n;i++) wsa[wv[i]]++;
for(i=;i<m;i++) wsa[i]+=wsa[i-];
for(i=n-;i>=;i--) sa[--wsa[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return;
} int r[maxn];
int sa[maxn];
int main()
{
char str[maxn];
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
memset(r, ,sizeof(r));
memset(sa, , sizeof(sa));
int len = strlen(str);
for (i = ; i < len; ++i)
{
r[i] = (int)str[i];
}
da(r, sa, len + , );
calheight(r, sa, len);
int sum = ;
for(i=;i<len;i++)
sum += len-sa[i+]-height[i+];
printf("%d\n",sum);
}
return ;
}
spoj694--Distinct Substrings的更多相关文章
- Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))
Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...
- spoj694 DISUBSTR - Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ694 New Distinct Substrings
New Distinct Substrings 题目大意 给定一个字符串,求本质不同的子串个数 题解 SA常见思想:每一个子串都是某个后缀的前缀 考虑每一个后缀的贡献,首先他拥有n - sa[i]个( ...
- 后缀数组---New Distinct Substrings
Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- 后缀数组:SPOJ SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- DISUBSTR - Distinct Substrings
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- 705. New Distinct Substrings spoj(后缀数组求所有不同子串)
705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
随机推荐
- centOS+DJango+mysql_nginx部署流程记录
安装Python3.6.2: https://www.jianshu.com/p/7a76bcc401a1 安装MySQL: https://www.cnblogs.com/luohanguo/p/9 ...
- 使用mysql服务实现负载均衡
mysql 长连接(tcp协议)模拟负载均衡 1.存在member,member的port与mysql服务port一致为3306 2.vip绑定浮动ip 3.开启mysql服务: #service m ...
- JavaScript一个简单的图片切换布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 笔记||Pyhton3进阶之多线程原理
# 多线程 # 一个进程相当于一个或多个线程 # 当没有多线程编程时,一个进程也是一个主线程 # 但有多线程编程时,一个进程包含多个线程,包括主线程 # 使用线程 可以实现程序的并发 # python ...
- winform跳转到bs
private void button7_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("http ...
- Html转图片 -- wkhtmltox
关于wkhtmltox,是一个可以把HTML转换为图片和pdf的工具. 不多介绍了,详见官网 https://wkhtmltopdf.org/ PHP 扩展 https://github.com/kr ...
- ORM常用字段及查询
目录 ORM常用字段及参数 创建表 ORM常用字段 ORM字段参数 ORM表关系创建 ForeignKey OneToOneField ManyToManyField 多对多三种创建方式 单表查询 q ...
- 有关vector元素的取地址
1--原则上,最好不要对vector的元素取地址,除非所有的vector元素已经填充完毕,这样vector的元素不会发生位置移动,地址才不会变,这样才能确保取得的地址的有效性.PS:即使在可以用已经分 ...
- Kubernetes——滚动更新和数据管理
k8s——滚动更新滚动更新就是一次只更新一小部分副本,更新成功之后再更新更多的副本,最终完成所有副本的更新.滚动更新最大的好处是零停机,整个更新的过程中始终有副本运行,从而保证了业务的连续性.kube ...
- IAR 设置问题
IAR 设置问题 1.worksplace 的设置问题:edit configuration 这样,每个工程都可以有一个workplace,方便切换. 2.文件夹包含问题 3.包含.dat文件问题 . ...