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

题面

Vjudge1

Vjudge2

题解

要求的是串的不同的子串个数

两道一模一样的题目

其实很容易:

总方案-不合法方案数

对于串进行后缀排序后

不合法方案数=相邻两个串的不合法方案数的和

也就是\(height\)的和

所以$$ans=\frac{n(n+1)}{2}-\sum_{i=1}^{len}height[i]$$

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 60000
int n;
int SA[MAX],x[MAX],y[MAX],t[MAX];
int height[MAX],rk[MAX],a[MAX];
char ch[MAX];
void init()
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
memset(t,0,sizeof(t));
memset(SA,0,sizeof(SA));
memset(rk,0,sizeof(rk));
memset(height,0,sizeof(height));
}
bool cmp(int i,int j,int k){return y[i]==y[j]&&y[i+k]==y[j+k];}
void GetSA()
{
int m=50000;
for(int i=1;i<=n;++i)t[x[i]=a[i]]++;
for(int i=1;i<=m;++i)t[i]+=t[i-1];
for(int i=n;i>=1;--i)SA[t[x[i]]--]=i;
for(int k=1;k<=n;k<<=1)
{
int p=0;
for(int i=n-k+1;i<=n;++i)y[++p]=i;
for(int i=1;i<=n;++i)if(SA[i]>k)y[++p]=SA[i]-k;
for(int i=0;i<=m;++i)t[i]=0;
for(int i=1;i<=n;++i)t[x[y[i]]]++;
for(int i=1;i<=m;++i)t[i]+=t[i-1];
for(int i=n;i>=1;--i)SA[t[x[y[i]]]--]=y[i];
swap(x,y);
x[SA[1]]=p=1;
for(int i=2;i<=n;++i)
x[SA[i]]=cmp(SA[i],SA[i-1],k)?p:++p;
if(p>=n)break;
m=p;
}
for(int i=1;i<=n;++i)rk[SA[i]]=i;
for(int i=1,j=0;i<=n;++i)
{
if(j)--j;
while(a[i+j]==a[SA[rk[i]-1]+j])++j;
height[rk[i]]=j;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%s",ch+1);
n=strlen(ch+1);
for(int i=1;i<=n;++i)a[i]=ch[i];
GetSA();
long long ans=1ll*n*(n+1)/2;
for(int i=1;i<=n;++i)ans-=height[i];
printf("%lld\n",ans);
}
return 0;
}

【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)的更多相关文章

  1. SPOJ - PHRASES Relevant Phrases of Annihilation —— 后缀数组 出现于所有字符串中两次且不重叠的最长公共子串

    题目链接:https://vjudge.net/problem/SPOJ-PHRASES PHRASES - Relevant Phrases of Annihilation no tags  You ...

  2. cogs1709. [SPOJ 705] 不同的子串(后缀数组

    http://cogs.pro:8080/cogs/problem/problem.php?pid=vyziQkWaP 题意:给定一个字符串,计算其不同的子串个数. 思路:ans=总共子串个数-相同的 ...

  3. Spoj-DISUBSTR - Distinct Substrings~New Distinct Substrings SPOJ - SUBST1~(后缀数组求解子串个数)

    Spoj-DISUBSTR - Distinct Substrings New Distinct Substrings SPOJ - SUBST1 我是根据kuangbin的后缀数组专题来的 这两题题 ...

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

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

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

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

  6. SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)

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

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

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

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

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

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

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

随机推荐

  1. iOS通知传值的使用

    通知 是在跳转控制器之间常用的传值代理方式,除了代理模式,通知更方便.便捷,一个简单的Demo实现通知的跳转传值. 输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IB ...

  2. Linux中7个用来浏览网页和下载文件的命令

    上一篇文章中,我们提到了rTorrent.wget.cURL.w3m.Elinks等几个有用的工具,很多人回信说还有其它几个类似的工具也值得讨论,所以就有了这篇文章.如果错过了第一部分的讨论,可以通过 ...

  3. table内容强制换行

    为防止文字过长而撑坏表格,一般我们需要通过css使td中内容强制换行.分别给table和td加一条样式即可实现: <meta charset="utf-8"> < ...

  4. filter-api文档

    git地址:https://github.com/jiqianqin/filters 不断优化中,欢迎加入讨论- filter-tags 效果图: 参数 说明 格式 备注 data 展示的数据 [{ ...

  5. python3.6+django2.0 一小时学会开发一套学员管理系统demo

    1.在pycharm中新建project demo1 添加app01 点击create按钮完成新建 2.在demo项目目录下新建目录static,并在settings.py中追加代码: STATICF ...

  6. 3.3 for 循环

    Python 编程中 for循环用来遍历序列类型的对象,逐一取出序列中的元素值,每取出一个元素值就执行一次循环体,直到元素取完,循环结束.循环体中的代码块可以和序列中的元素值一点关系都没有,因为for ...

  7. Codeforces475D - CGCDSSQ

    Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\),给出\(q(q\leq3\times10^5)\)个\(x\),对于每个\(x\),求满足 ...

  8. Appium安卓真机环境搭建

    说明 步骤可能比较简洁,因为手头上有安卓测试机,所以需要配置虚拟机的童鞋请去虫师博客园,因为我也是从那儿学的,哈哈.点我飞到虫师那儿 但是如果你要搭建真机测试环境的话,本教程将是最简单实用的. 1. ...

  9. nginx笔记3-负载均衡算法

    1.nginx测试:先从官网下载nginx 官网网址为:http://nginx.org/  然后找到stable version的版本下载,因为这版本是最稳定的,不要去下载最新,因为不稳定,如下图: ...

  10. python介绍篇

    二进制编码ASSIC 每一个字符统一都需要8个bit来存储 计算机容量 1位 = 1bit 8bit = 1byte = 1字节 1024bytes = 1kbytes =1KB 1024个字符,小文 ...