题目链接:https://cn.vjudge.net/contest/283743#problem/F

题目大意:给你一个字符串,然后让你求出不同的子串的个数。

具体思路:首先,一个字符串中总的子串个数是len*(len+1)/2,然后就开始去重了,通过height数组,求出所有重复的子串的个数,然后就用总的子串的个数-重复的子串的个数就可以了。

AC代码:

 #include<iostream>
#include<stack>
#include<cstring>
#include<iomanip>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
# define ll long long
const int maxn = 5e5+;
int cntA[maxn], cntB[maxn], sa[maxn], tsa[maxn], A[maxn], B[maxn], height[maxn];
int Rank[maxn];
char ch[maxn];
ll n;
//sa[i]代表第i小的后缀位置,Rank[i]代表第i位置开始的后缀在所有的后缀串中排名第几
// height[i]代表排名第i个字符串和第i-1个字符串的相同前缀的长度
void cal()
{
for(int i = ; i < ; i++) cntA[i] = ;
for(int i = ; i <= n; i++) cntA[ch[i-]]++;
for(int i = ; i < ; i++) cntA[i] += cntA[i-];
for(int i = n; i; i--) sa[cntA[ch[i-]]--] = i;
Rank[sa[]] = ;
for(int i = ; i <= n; i++)
{
Rank[sa[i]] = Rank[sa[i-]];
if(ch[sa[i]-] != ch[sa[i-]-]) Rank[sa[i]]++;
}
for(int l = ; Rank[sa[n]] < n; l <<= )
{
memset(cntA, , sizeof(cntA));
memset(cntB, , sizeof(cntB));
for(int i = ; i <= n; i++)
{
cntA[A[i] = Rank[i]]++;
cntB[B[i] = (i+l <= n)?Rank[i+l]:]++;
}
for(int i = ; i <= n; i++) cntB[i] += cntB[i-];
for(int i = n; i; i--) tsa[cntB[B[i]]--] = i;
for(int i = ; i <= n; i++) cntA[i] += cntA[i-];
for(int i = n; i; i--) sa[cntA[A[tsa[i]]]--] = tsa[i];
Rank[sa[]]=;
for(int i = ; i <= n; i++)
{
Rank[sa[i]] = Rank[sa[i-]];
if(A[sa[i]] != A[sa[i-]] || B[sa[i]] != B[sa[i-]]) Rank[sa[i]]++;
}
}
for(int i = , j = ; i <= n; i++)
{
if(j) j--;
while(ch[i+j-] == ch[sa[Rank[i]-] + j - ]) j++;
height[Rank[i]] = j;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",ch);
n=strlen(ch);
if(n==)
{
printf("1\n");
continue;
}
// cout<<1<<endl;
cal();
ll ans=n*(n+)/;
//cout<<ans<<endl;
for(int i=; i<=n; i++)
{
ans-=height[i];
// cout<<i<<" "<<height[i]<<endl;
}
printf("%lld\n",ans);
}
return ;
}

F - New Distinct Substrings (后缀数组)的更多相关文章

  1. SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数

    题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...

  2. SPOJ - DISUBSTR Distinct Substrings (后缀数组)

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

  3. 【SPOJ – SUBST1】New Distinct Substrings 后缀数组

    New Distinct Substrings 题意 给出T个字符串,问每个字符串有多少个不同的子串. 思路 字符串所有子串,可以看做由所有后缀的前缀组成. 按照后缀排序,遍历后缀,每次新增的前缀就是 ...

  4. SPOJ DISUBSTR Distinct Substrings 后缀数组

    题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...

  5. SPOJ 694 || 705 Distinct Substrings ( 后缀数组 && 不同子串的个数 )

    题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀 ...

  6. spoj Distinct Substrings 后缀数组

    给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB  BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和 ...

  7. [spoj694&spoj705]New Distinct Substrings(后缀数组)

    题意:求字符串中不同子串的个数. 解题关键:每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数. 1.总数减去height数组的和即可. 注意这里height中为什么不需 ...

  8. spoj 694. Distinct Substrings 后缀数组求不同子串的个数

    题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...

  9. SPOJ_705_New Distinct Substrings_后缀数组

    SPOJ_705_New Distinct Substrings_后缀数组 题意: 给定一个字符串,求该字符串含有的本质不同的子串数量. 后缀数组的一个小应用. 考虑每个后缀的贡献,如果不要求本质不同 ...

  10. SPOJ- Distinct Substrings(后缀数组&后缀自动机)

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

随机推荐

  1. 【Linux】MGR部署脚本

    脚本没有完善,现在只有上半部分的基础环境搭建 [准备条件] 1.三个节点的防火墙关闭 2.原有mysql卸载删除 3.文件夹名字: mgr 所有的源码包都放在mgr的文件夹下 4.文件位置: /roo ...

  2. 自学Python1.4-Centos内vim中文乱码问题

    自学Python之路 自学Python1.4-Centos内vim中文乱码问题 1. 登陆的系统---区域语言设置 1.1查看安装中文包: 查看系统是否安装中文语言包 (列出所有可用的公共语言环境的名 ...

  3. [hdu3966]Aragorn's Story

    传送门 题目描述 Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One ...

  4. 洛谷 P1120 小木棍 [数据加强版]解题报告

    P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它 ...

  5. CF1114B Yet Another Array Partitioning Task(贪心,构造题)

    我至今不敢相信我被这么一道简单的题卡了这么久……看来还是太弱了…… 题目链接:CF原网 题目大意:定义一个序列的“美丽度”为这个序列前 $m$ 大的数的和.现在有一个长度为 $n$ 的序列,你需要把它 ...

  6. HTMLUnit web测试

    httpClient不能动态执行网页中的js,这样无法获取js生成的动态网页.htmlUnit是个解决方法. if you’re considering web application testing ...

  7. centos6.5修改主机名

    centos 修改主机名 0.说明 系统安装后,系统默认的主机名称是localhost,现在想要修改为master.操作需要root权限. 1.方案一:仅当前登录有效,重启后失效 直接在命令行执行命令 ...

  8. 焦点监听事件FocusListener

    public class Demo extends JFrame { public Demo() { setBounds(100, 100, 200, 120); setDefaultCloseOpe ...

  9. (大数 万进制) N! hdu1042

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

  10. python 面向对象之多态

    多态是什么? 用一句话来概括下,多态就是同一操作(方法)作用于不同的对象时,可以有不同的解释,产生不同的执行结果. #!/usr/bin/env python # -*- coding: utf-8 ...