题目描述】

给定一个字符串,计算其不同的子串个数。

【输入格式】

一行一个仅包含大写字母的字符串,长度<=50000

【输出格式】

一行一个正整数,即不同的子串个数。

【样例输入】

ABABA

【样例输出】

9

题解:

显然后缀可以是一个子串,然后后缀中可能包含多个子串。

我们考虑不重复统计,容易发现 一个后缀的贡献为L-high[i]+1

因为high[i]之前的显然可以在后面的串中被统计到,所以可以避免重复

 #include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N=;
char s[N];int n,k,rk[N],sa[N],tmp[N],high[N];
bool comp(int i,int j){
if(rk[i]!=rk[j])return rk[i]<rk[j];
int ri=i+k<=n?rk[i+k]:-;
int rj=j+k<=n?rk[j+k]:-;
return ri<rj;
}
void Getsa(){
for(int i=;i<=n;i++)sa[i]=i,rk[i]=s[i];
for(k=;k<=n;k<<=){
sort(sa+,sa+n+,comp);
for(int i=;i<=n;i++)tmp[sa[i]]=tmp[sa[i-]]+comp(sa[i-],sa[i]);
for(int i=;i<=n;i++)rk[i]=tmp[i];
}
}
void Gethight(){
int j,h=;
for(int i=;i<=n;i++){
j=sa[rk[i]-];
if(h)h--;
for(;j+h<=n && i+h<=n;h++)if(s[i+h]!=s[j+h])break;
high[rk[i]-]=h;
}
}
void Getanswer(){
long long ans=;
for(int i=;i<=n;i++){
if(high[i]==n-sa[i]+)continue;
ans+=n-sa[i]+-high[i];
}
printf("%lld\n",ans);
}
int main()
{
freopen("subst1.in","r",stdin);
freopen("subst1.out","w",stdout);
scanf("%s",s+);
n=strlen(s+);
Getsa();Gethight();Getanswer();
return ;
}

[SPOJ705]不同的子串的更多相关文章

  1. Cogs 1709. [SPOJ705]不同的子串 后缀数组

    题目:http://cojs.tk/cogs/problem/problem.php?pid=1709 1709. [SPOJ705]不同的子串 ★★   输入文件:subst1.in   输出文件: ...

  2. spoj705 后缀数组求不同子串的个数

    http://www.spoj.com/problems/SUBST1/en/  题目链接 SUBST1 - New Distinct Substrings no tags  Given a stri ...

  3. LeetCode[5] 最长的回文子串

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  5. C语言计算字符串子串出现的次数

    #include<stdio.h>#include<string.h>int substring(char *str,char *str1);//函数原型int main(vo ...

  6. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  7. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  9. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. 2017-2018-1 1623 bug终结者 冲刺007

    bug终结者 冲刺007 by 20162302 杨京典 今日任务:排行榜界面 排行榜界面,选项界面 简要说明 排行榜界面用于展示用户通关是所使用的步数和时间,选项界面可以调整背景音乐的开关.选择砖块 ...

  2. python 堆排序

    堆排序就是把堆顶的最大数取出, 将剩余的堆继续调整为最大堆,具体过程在第二块有介绍,以递归实现 剩余部分调整为最大堆后,再次将堆顶的最大数取出,再将剩余部分调整为最大堆,这个过程持续到剩余数只有一个时 ...

  3. VisualVM 使用 service:jmx:rmi:///...无法连接linux远程服务器

    VisualVM 无法使用 service:jmx:rmi:///jndi/rmi:///jmxrmi 连接到 关闭远程机器的防火墙即可:service iptables stop 不关闭防火墙的解决 ...

  4. 从PRISM开始学WPF(六)MVVM(三)事件聚合器EventAggregator?

    从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? ...

  5. python全栈开发-Day13 内置函数

    一.内置函数 注意:内置函数id()可以返回一个对象的身份,返回值为整数. 这个整数通常对应与该对象在内存中的位置,但这与python的具体实现有关,不应该作为对身份的定义,即不够精准,最精准的还是以 ...

  6. SSM框架中前端页面(AJAX+Jquery+spring mvc+bootstrap)

    前端新增页面的模态框,采用bootstarp建立.定义了empName,email,gender,depatName,四个属性的ID:其中保存按钮的ID:emp_save_btn,对应的点击函数如下: ...

  7. MySQL Group Relication 部署环境入门篇

      一:环境介绍   cenos 6.7 版本 数据库的版本5.7.19 二:部署规划单机多实例的部署   端口号 数据目录  group_repplicatoon 通信接口   3307 /data ...

  8. Django实现发邮件

    1 首先去自己的邮箱申请,在设置里面找,申请开通smtp服务,我用的是163邮箱 2 在项目下settings.py中添加设置: # 配置邮箱发邮件的相关功能 #这一项是固定的 EMAIL_BACKE ...

  9. Excel as a Service —— Excel 开发居然可以这么玩

    前言 据不完全统计,全世界使用Excel作为电子表格和数据处理的用户数以十亿计,这不仅得益于它的使用简便,同时还因为它内置了很多强大的函数,结合你的想象力可以编写出各种公式,并可快速根据数据生成图表和 ...

  10. MySql中的varchar长度究竟是字节还是字符

    今天在设计表的时候,遇到个小问题,由于不知道未来将要存储的数据有多长(数据是通过第三方http接口提供的,根据sample显示,数据大概是如下:) 也就是6个字符. 我在设计表的时候,有点犹豫,本来准 ...