DISUBSTR - Distinct Substrings

链接

题意:

  询问有多少不同的子串。

思路:

  后缀数组或者SAM

  首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其中有height[rnk[i]]个被rnk[i]-1的后缀算了。所以再减去height[rnk[i]]即可。

代码:

换了板子。

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath> using namespace std; const int N = ;
char s[N];
int t1[N],t2[N],sa[N],height[N],rnk[N],c[N];
int n; void get_sa(int m) {
int *x = t1,*y = t2,i,p;
for (i=; i<=m; ++i) c[i] = ;
for (i=; i<=n; ++i) x[i]=s[i],c[x[i]]++;
for (i=; i<=m; ++i) c[i] += c[i-];
for (i=n; i>=; --i) sa[c[ x[i] ]--] = i;
for (int k=; k<=n; k<<=) {
p = ;
for (i=n-k+; i<=n; ++i) y[++p] = i;
for (i=; i<=n; ++i) if (sa[i]>k) y[++p] = sa[i]-k;
for (i=; i<=m; ++i) c[i] = ;
for (i=; i<=n; ++i) c[ x[y[i]] ]++;
for (i=; i<=m; ++i) c[i] += c[i-];
for (i=n; i>=; --i) sa[c[ x[y[i]] ]--] = y[i];
swap(x,y);
p = ;
x[sa[]] = ;
for (i=; i<=n; ++i)
x[sa[i]] = y[sa[i]]==y[sa[i-]]&&y[sa[i]+k]==y[sa[i-]+k] ? p- : p++;
if (p > n) break;
m = p;
}
}
void get_height() {
for (int i=; i<=n; ++i) rnk[sa[i]] = i;
int k = ;
height[] = ;
for (int i=; i<=n; ++i) {
if (rnk[i]==) continue;
if (k) k--;
int j = sa[rnk[i]-];
while (i+k<=n && j+k<=n && s[i+k]==s[j+k])
k++;
height[rnk[i]] = k;
}
}
void get_ans() {
long long ans = ;
for (int i=; i<=n; ++i) {
ans += max(n-i+-height[rnk[i]],);
// ans += max(n-sa[i]-height[i],0);
}
cout << ans << '\n';
}
int main() {
int T;
scanf("%d",&T);
while (T--) {
scanf("%s",s+);
n = strlen(s+);
get_sa();
get_height();
get_ans();
}
return ;
}

原来的

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath> using namespace std; const int N = ;
char s[N];
int t1[N],t2[N],sa[N],height[N],rnk[N],c[N];
int n; void get_sa(int m) {
int *x = t1,*y = t2;
for (int i=; i<m; ++i) c[i] = ;
for (int i=; i<n; ++i) c[x[i] = s[i]]++;
for (int i=; i<m; ++i) c[i] += c[i-];
for (int i=n-; i>=; --i) sa[--c[x[i]]] = i;
for (int k=; k<=n; k<<=) {
int p = ;
for (int i=n-k; i<n; ++i) y[p++] = i;
for (int i=; i<n; ++i) if (sa[i] >= k) y[p++] = sa[i] - k;
for (int i=; i<m; ++i) c[i] = ;
for (int i=; i<n; ++i) c[x[y[i]]]++;
for (int i=; i<m; ++i) c[i] += c[i-];
for (int i=n-; i>=; --i) sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = ;
x[sa[]] = ;
for (int i=; i<n; ++i)
x[sa[i]] = (y[sa[i-]]==y[sa[i]] && sa[i-]+k<n && sa[i]+k<n &&
y[sa[i-]+k]==y[sa[i]+k]) ? p- : p++; if (p >= n) break;
m = p;
}
}
void get_height() {
for (int i=; i<n; ++i) rnk[sa[i]] = i;
int k = ;
height[] = ;
for (int i=; i<n; ++i) {
if (!rnk[i]) continue;
if (k) k--;
int j = sa[rnk[i]-];
while (i+k < n && j+k < n && s[i+k]==s[j+k]) k++;
height[rnk[i]] = k;
}
}
void get_ans() {
long long ans = ;
for (int i=; i<n; ++i) {
ans += max(n-i-height[rnk[i]],);
// ans += max(n-sa[i]-height[i],0);
}
cout << ans << '\n';
}
int main() {
int T;
scanf("%d",&T);
while (T--) {
scanf("%s",s);
n = strlen(s);
get_sa();
get_height();
get_ans();
}
return ;
}

SPOJ 694&&SPOJ705: Distinct Substrings的更多相关文章

  1. 【SPOJ 694】Distinct Substrings 不相同的子串的个数

    不会FQ啊,没法评测啊,先存一下代码QAQ 2016-06-16神犇Menci帮我测过AC了,谢谢神犇Menci QwQ #include<cstdio> #include<cstr ...

  2. SPOJ 694 DISUBSTR - Distinct Substrings

    思路 求本质不同的子串个数,总共重叠的子串个数就是height数组的和 总子串个数-height数组的和即可 代码 #include <cstdio> #include <algor ...

  3. 【SPOJ 694】Distinct Substrings (更直接的求法)

    [链接]h在这里写链接 [题意] 接上一篇文章 [题解] 一个字符串所有不同的子串的个数=∑(len-sa[i]-height[i]) [错的次数] 0 [反思] 在这了写反思 [代码] #inclu ...

  4. 【SPOJ 694】Distinct Substrings

    [链接]h在这里写链接 [题意]     给你一个长度最多为1000的字符串     让你求出一个数x,这个x=这个字符串的不同子串个数; [题解]     后缀数组题.     把原串复制一份,加在 ...

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

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

  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 SUBST1 - New Distinct Substrings

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

  8. Spoj SUBST1 New Distinct Substrings

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

  9. SPOJ705 Distinct Substrings (后缀自动机&后缀数组)

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

随机推荐

  1. angular cli

    1. 安装cnpm: npm install -g cnpm --registry=https://registry.npm.taobao.org 2. 安装angular/cli: cnpm ins ...

  2. SpringBoot接口返回去掉空字段

    返回的接口中存在值为null或者空的字段过滤掉 @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMis ...

  3. 【[NOI2005]瑰丽华尔兹】

    非常无脑和码农的单调队列优化\(dp\) 我们发现一个时间段内移动的情况是一样的,而时间段的数目又非常少,所以可以直接按照时间段来进行\(dp\) 由于每一次\(dp\)的移动距离都是小于等于某一个固 ...

  4. programming-languages学习笔记--第3部分

    programming-languages学习笔记–第3部分 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.src ...

  5. 【jQuery】动画小练习

    1.jQuery部分代码如下 <script type="text/javascript"> $(function(){ var page = 1; var i = 4 ...

  6. python多线程练习

    import threading from time import sleep,ctime def print1(): for i in range(10): print(i,end='') prin ...

  7. Redis(三)内存模型

    本文转载自编程迷思,原文链接 深入学习Redis(1):Redis内存模型 前言 Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Redis是实现网站高并发不可 ...

  8. Android开发之自己定义TabHost文字及背景(源码分享)

    使用TabHost 能够在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该怎样进行自己定义改动优化呢 MainActivity的源码 package com.dream. ...

  9. redis 安装 配置 及启动

    linux下安装redis及其中遇到的问题的解决方法1.将下载好的压缩包放到/usr/local目录下# tar xzf redis-3.0.2.tar.gz# cd redis-3.0.2# mak ...

  10. ABAP术语-Update Data

    Update Data 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/20/1114169.html The data which is t ...