DISUBSTR - Distinct Substrings

题意:给你一个长度最多1000的字符串,求不相同的字串的个数。

思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已经将所有的重复的都计算出来了,直接减去就行。需要注意的是在字符串的最后面加个0,不参与Rank排名,这样得到的height数组直接从1到n。

char s[N];
int sa[N],Rank[N],height[N],c[N],t[N],t1[N],n,m;
void build(int n)
{
int i,*x=t,*y=t1;
memset(c,0,sizeof(c));
for(i=0; i<n; i++) c[x[i]=s[i]]++;
for(i=1; i<m; i++) c[i]+=c[i-1];
for(i=n-1; i>=0; i--) sa[--c[x[i]]]=i;
for(int k=1; k<=n; k<<=1)
{
int p=0;
for(i=n-k; i<n; i++) y[p++]=i;
for(i=0; i<n; i++) if(sa[i]>=k) y[p++]=sa[i]-k; memset(c,0,sizeof(c));
for(i=0; i<n; i++) c[x[y[i]]]++;
for(i=1; i<m; i++) c[i]+=c[i-1];
for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1,x[sa[0]]=0;
for(i=1; i<n; i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
if(p>=n) break;
m=p;
}
}
void get_height(int n)//n不保存最后的0
{
int i,j,k=0;
for(i=1; i<=n; i++) Rank[sa[i]]=i;
for(i=0; i<n; i++)
{
if(k) k--;
j=sa[Rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[Rank[i]]=k;
}
}
void show()
{
puts("");
for(int i=0;i<=n;i++) printf("%d ",i);
puts("");
for(int i=0;i<=n;i++) printf("%c ",s[i]);
printf("\nsa:\n");
for(int i=0;i<=n;i++) printf("%d ",sa[i]);
printf("\nRank:\n");
for(int i=0;i<=n;i++) printf("%d ",Rank[i]);
printf("\nHight:\n");
for(int i=0;i<=n;i++) printf("%d ",height[i]);
puts("");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
n=strlen(s);
s[n]=0,m=125;
build(n+1);
get_height(n);
// show();
int ans=(n+1)*n/2;
for(int i=1;i<=n;i++) ans-=height[i];
printf("%d\n",ans);
}
return 0;
}

可以试着show()函数看看规律。

SPOJ - Distinct Substrings,求不同的字串个数!的更多相关文章

  1. 洛谷P2408 不同字串个数 [后缀数组]

    题目传送门 不同字串个数 题目背景 因为NOI被虐傻了,蒟蒻的YJQ准备来学习一下字符串,于是它碰到了这样一道题: 题目描述 给你一个长为N的字符串,求不同的子串的个数 我们定义两个子串不同,当且仅当 ...

  2. HDU4641 || 6194多校 (后缀自动机-最少出现K次的字串个数 || 恰好出现K次字符串的个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=4641 http://acm.hdu.edu.cn/showproblem.php?pid=6194 题意: 开始 ...

  3. SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)

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

  4. spoj - Distinct Substrings(后缀数组)

    Distinct Substrings 题意 求一个字符串有多少个不同的子串. 分析 又一次体现了后缀数组的强大. 因为对于任意子串,一定是这个字符串的某个后缀的前缀. 我们直接去遍历排好序后的后缀字 ...

  5. SPOJ Distinct Substrings【后缀数组】

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

  6. poj2406 Power Strings (kmp 求最小循环字串)

    Power Strings   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 ...

  7. SPOJ Distanct Substrings(求不同子串的数量)

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

  8. 洛谷2408不同字串个数/SPOJ 694/705 (后缀数组SA)

    真是一个三倍经验好题啊. 我们来观察这个题目,首先如果直接整体计算,怕是不太好计算. 首先,我们可以将每个子串都看成一个后缀的的前缀.那我们就可以考虑一个一个后缀来计算了. 为了方便起见,我们选择按照 ...

  9. SPOJ Distinct Substrings SA

    正解:SA 解题报告: 传送门! 啊先给个翻译趴QwQ大概就是说给个字符串,求互不相等的子串的个数 算是道小水题辣趴,,,并不难想到的呢QAQ只是因为是新知识所以巩固下而已QAQ 然后就显然考虑合法方 ...

随机推荐

  1. jsp中<c:forEach varStatus="status">的属性值问题

    c:forEach varStatus属性 current当前这次迭代的(集合中的)项 index当前这次迭代从 0 开始的迭代索引 count当前这次迭代从 1 开始的迭代计数 first用来表明当 ...

  2. Azure 项目构建 – 部署 Drupal 网站

    通过完整流程详细介绍了如何通过 Azure Web 应用. MySQL DB on Azure 等服务在 Azure 平台上快速搭建 Drupal 服务器,并将其连接到 MySQL 数据库. 此系列的 ...

  3. C# DataTable的詳細用法 (转)

    在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...

  4. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest C Sequence (打表)

    打个表找找规律,到24445的时候乘2以后产生了0出现循环. 一般地,判断循环节是否存在可以用Floyd判圈算法. #include<bits/stdc++.h> using namesp ...

  5. Scala 的list

    9.1 使用列表 列表类型:跟数组一样,列表也是同质化的(homogeneous).即所有元素都要是同种类型. 列表结构:所有列表由两部分组成:Nil 和 ::(cons). 基本操作:主要有三个:h ...

  6. 使用vue做移动端瀑布流分页

    讲到瀑布流分页有一个方法一定是要用到的 pullToRefresh() 这个也没什么好解释的,想了解的可以去百度一下 下面上代码 <div id="main" class=& ...

  7. 解决IllegalBlockSizeException:last block incomplete in decryption异常

    解决IllegalBlockSizeException:last block incomplete in decryption异常分类: webkit android最近做个加解密的实现,虽然实现了, ...

  8. Linux文件的IO操作 一

    系统调用 系统调用: 操作系统提供给用户程序调用的一组“特殊”接口,用户程序可以通过这组“特殊”接口来获得操作系统内核提供的服务 为什么用户程序不能直接访问系统内核提供的服务 为了更好地保护内核空间, ...

  9. k8s master init and add node

    目录 一. add google apt-key 二. k8s master init 三. k8s node add to master cluster(use this command when ...

  10. What is the difference between try/except and assert?

    assert only check if a condition is true or not and throw an exception. A try/except block can run a ...