spoj705:http://www.spoj.com/problems/SUBST1/

题意:和spoj694一样,只是数据范围变大了。

题解:同spoj694.

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
char str[maxn];
int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];
int cmp(int* r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
//n为字符串长度,m为字符的取值范围,r为字符串。后面的j为每次排序时子串的长度
void DA(int* r,int* sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
///对R中长度为1的子串进行基数排序
for(i=; i<m; i++)wn[i]=;
for(i=; i<n; i++)wn[x[i]=r[i]]++;
for(i=; i<m; i++)wn[i]+=wn[i-];
for(i=n-; i>=; i--)sa[--wn[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++)wn[i]=;
for(i=; i<n; i++)wn[wv[i]]++;
for(i=; i<m; i++)wn[i]+=wn[i-];
for(i=n-; i>=; i--)sa[--wn[wv[i]]]=y[i];
///当p=n的时候,说明所有串都已经排好序了
///在第一次排序以后,rank数组中的最大值小于p,所以让m=p
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;
}
///后缀数组 计算height数组
/**
height数组的值应该是从height[1]开始的,而且height[1]应该是等于0的。
原因是,+因为我们在字符串后面添加了一个0号字符,所以它必然是最小的
一个后缀。而字符串中的其他字符都应该是大于0的(前面有提到,使用倍
增算法前需要确保这点),所以排名第二的字符串和0号字符的公共前缀
(即height[1])应当为0.在调用calheight函数时,要注意height数组的范
围应该是[1..n]。所以调用时应该是calheight(r,sa,n)
而不是calheight(r,sa,n+1)。*/
int rank[maxn],height[maxn];
void calheight(int* r,int* sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++)rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; r[i+k]==r[j+k]; k++);
return;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char str[];
scanf("%s",str);
int n=strlen(str);
for(int i=;i<n;i++) a[i]=(int)str[i];
a[n]=;
DA(a,sa,n+,);
calheight(a,sa,n);
int sum=;
for(int i=;i<=n;i++)
sum+=n-sa[i]-height[i];
printf("%d\n",sum);
}
return ;
}

New Distinct Substrings的更多相关文章

  1. 后缀数组---New Distinct Substrings

    Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...

  2. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

  3. 后缀数组:SPOJ SUBST1 - New Distinct Substrings

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  4. DISUBSTR - Distinct Substrings

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  5. 705. New Distinct Substrings spoj(后缀数组求所有不同子串)

    705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...

  6. 【SPOJ】Distinct Substrings(后缀自动机)

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...

  7. 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)

    [SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...

  8. Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))

    Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...

  9. SPOJ Distinct Substrings【后缀数组】

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  10. SPOJ 694&&SPOJ705: Distinct Substrings

    DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其 ...

随机推荐

  1. Hibernate占位符警告:use named parameters or JPA-style positional parameters instead.

    Hibernate占位符警告:use named parameters or JPA-style positional parameters instead. >>>>> ...

  2. CSS画三角形引发的一些思考

      今天刷知乎时看到了一个问题,有谁能详细讲一下css如何画出一个三角形?怎么想都想不懂? - 知乎.很巧,刚入前端坑的我前不久也遇到过这个问题,今天再来谈一谈这个问题则是因为知乎的一些答案引发了我的 ...

  3. volatile用处说明

      在JDK1.2之前,Java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程环境下volatile关键字的使用变得非常重要. 在当前 ...

  4. WisDom.Net 框架设计(六) license

    WisDom.Net-license 1.为啥要用license    license (许可证) 顾名思义就是说我的软件只能给在指定的机器上使用.毕竟很多项目都不是免费的,(说句题外话,其实我用的也 ...

  5. [Mime] MimeEntity--MimeEntity Mime实体帮助类 (转载)

    点击下载 MimeEntity.rar 这个类是关于Mime实体的类看下面代码吧 /// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 /// 联系方式 ...

  6. C#基础加强

    1.代码规范 -命名规范:定义的变量或者方法名要有意义. 1.骆驼命名 :第一个单词首字母小写,之后的单词首字母大写 userName.userPassword.realName…(普通变量(局部变量 ...

  7. java开发规范总结_代码注释规范

    规范需要平时编码过程中注意,是一个慢慢养成的好习惯 1.基本规则 1.注释应该使代码更加清晰易懂   2.注释要简单明了,只要提供能够明确理解程序所必要的信息就可以了.如果注释太复杂说明程序需要修改调 ...

  8. ios动力特效,最重要的一点 属性保持(写了动力特效但是没效果的原因就在这里)

    @property (nonatomic, strong) UIDynamicItemBehavior *square1PropertiesBehavior; @property (nonatomic ...

  9. IOS-图片上传到服务器

    //获取document 路径- (NSString *)getDocumentPath{    NSArray *paths = NSSearchPathForDirectoriesInDomain ...

  10. js Module模式

    // 创建一个立即调用的匿名函数表达式// return一个变量,其中这个变量里包含你要暴露的东西// 返回的这个变量将赋值给counter,而不是外面声明的function自身 var counte ...