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. 【转】HashMap 和 HashTable 到底哪不同 ?

    2017/05/29 | 分类: 基础技术 | 2 条评论 | 标签: HASHMAP, HASHTABLE 分享到: 原文出处: 程序员赵鑫 HashMap和HashTable有什么不同?在面试和被 ...

  2. 2566. [51nod 1129] 字符串最大值

    [题目描述] 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd. 给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值. 例 ...

  3. ubuntu中使用eclipse开发android,logcat显示问题

    〜/工作区/ .metadata / .plugins / org.eclipse.core.runtime / .settings / com.android.ide.eclipse.ddms.pr ...

  4. android布局带参返回

    package com.lxj.lesson2_3ID19; import com.example.lesson2_3_id19.R; import com.lxj.other.AgeActivity ...

  5. copyin函数

    详见:http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.kerneltechref%2Fdoc%2Fk ...

  6. hihoCoder #1079 : 离散化 (线段树,数据离散化)

    题意:有一块宣传栏,高一定,给出长度,再给出多张海报的张贴位置,问还能见到几张海报(哪怕有一点被看到)?假设海报的高于宣传栏同高. 思路:问题转成“给出x轴上长为L的一条线段,再用n条线段进行覆盖上去 ...

  7. 洛谷 P1464 Function

    题目描述 对于一个递归函数w(a,b,c) 如果a<=0 or b<=0 or c<=0就返回值1. 如果a>20 or b>20 or c>20就返回w(20,2 ...

  8. Tunneling cannot be enabled without the local_ip bound to an interface on the host. Please configure local_ip 192.168.30.71 on the host interface to be used for tunneling and restart the agen

    按照官方文档配置linux bridge 会出现一下问题 Tunneling cannot be enabled without the local_ip bound to an interface ...

  9. Fedora19添加和设置YUM源

    Fedora19添加和设置YUM源添加yum源前先安装fastestmirror/downloadonly插件和axelget插件: 1.安装fastestmirror/downloadonly插件 ...

  10. 自动发表QQ空间说说

    require("gb2312toutf8") local http = require "socket.http" local surl = "ht ...