个人第一道后缀数组题目。
对于每一个后缀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的更多相关文章

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

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

  2. spoj694 DISUBSTR - Distinct Substrings

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

  3. SPOJ694 New Distinct Substrings

    New Distinct Substrings 题目大意 给定一个字符串,求本质不同的子串个数 题解 SA常见思想:每一个子串都是某个后缀的前缀 考虑每一个后缀的贡献,首先他拥有n - sa[i]个( ...

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

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

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

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

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

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

  7. DISUBSTR - Distinct Substrings

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

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

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

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

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

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

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

随机推荐

  1. druid监控sql完整版

    利用Druid实现应用和SQL监控 一.关于Druid Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. D ...

  2. ios 物流时间轴,自动匹配电话号码,可点击拨打

    http://www.code4app.com/thread-27587-1-1.html 资讯时间轴(折叠/展开) http://www.code4app.com/thread-32358-1-1. ...

  3. TCP通讯代码

    服务端代码: import socket server_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 使用固定端口 server_ ...

  4. java 8时间使用LocalDateTime,ZonedDateTime,LocalDate

    前言 java 8的时间已经能够满足日常的使用,也方便理解.joda-time作为一个有优秀的时间组件也不得不告知使用者在java 8以后使用自带的时间 LocalDateTime以及ZonedDat ...

  5. Linux 修改/etc/profile 出错 导致所有命令都 command not found 的解决办法

    执行命令 export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin 然后再修改/etc/profile 再执行文件: source /etc/p ...

  6. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)E(多重集维护)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long ans[1000007]; ...

  7. [Android实例] Android网络收音机项目(内含源码)

    ======================帖子内容===================================最近喜欢听广播,但是搜索了一下,苦于网上没有Android的网络收音机项目的例 ...

  8. 为什么很多 Android 程序喜欢在存储卡根目录建文件夹来存储数据而不是 Android/data 目录下?

    知乎回答.   http://www.zhihu.com/question/19866689   pansz,欢迎评论 知乎用户.弓长.知乎用户 赞同 这个道理很简单:因为没人管啊.你乱存放文件,在a ...

  9. arduino#呼吸灯

    ; // 使用引脚号 void setup() { // nothing happens in setup } void loop() { // fade in from min to max in ...

  10. Windows服务器权限分析

    一.Windows常见用户 二.Windows常见用组 2.1Windows常见组 2.2Windows2003常见组 三.Windows目录权限 四.Windows2003默认权限 五.不同环境下的 ...