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. HTML的CoreText流畅度超过WebView。CoreText第三方框架DTCoreText的介绍

    为什么要用CoreText(富文本)来取代WebView去显示内容.主要的原因就WebView有很大的问题,性能,FPS,卡顿,与原生不搭.这些都是大问题. WebView的缺点 1.直接使用WebV ...

  2. java反射机制执行命令

    public class Encryptor{ public static void main(String[] args) throws IOException, ClassNotFoundExce ...

  3. 4、Android-数据存储方案(使用LitePal操作数据库)

    4.5.使用LitePal操作数据库 4.5.1.LitePal简介 LitePal是一款开源的Android数据库框架 采用了关系映射(ORM)的模式 将经常使用的一些数据库做了封装 是得不用编写S ...

  4. [Python 网络编程] TCP、简单socket模拟ssh (一)

    OSI七层模型(Open System Interconnection,开放式系统互联) 应用层 网络进程访问应用层: 为应用程序进程(例如:电子邮件.文件传输和终端仿真)提供网络服务: 提供用户身份 ...

  5. ES6标准入门 字符串的扩展

    1:模板字符串与模板引擎 https://blog.csdn.net/crper/article/details/52940625 es6模板字符串中标签模板作为参数时产生空元素的问题 https:/ ...

  6. 新版剑指offer14 剪绳子

    int maxProduct(int length){ ) ; ) ; ) ; ; == ) numof3 -= ; )/; ,numof3))*(,numof2)); }

  7. UVA - 1197 (简单并查集计数)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  8. HDU 3367 (伪森林,克鲁斯卡尔)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3367 Pseudoforest Time Limit: 10000/5000 MS (Java/Oth ...

  9. nDPI的安装与测试

    目录 简介 nDPI的环境依赖项安装 nDPI 安装 nDPI测试 官方Quick Start nDPI 的 github 简介 nDPI是一个开源的基于 OpenDPI 的 DPI 库,目前由 nt ...

  10. DDL-表的管理

    一.创建表 ★create table [if not exists] 表名(    字段名 字段类型 [约束],    字段名 字段类型 [约束],    ...    字段名 字段类型 [约束] ...