SPOJ 694&&SPOJ705: Distinct Substrings
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的更多相关文章
- 【SPOJ 694】Distinct Substrings 不相同的子串的个数
不会FQ啊,没法评测啊,先存一下代码QAQ 2016-06-16神犇Menci帮我测过AC了,谢谢神犇Menci QwQ #include<cstdio> #include<cstr ...
- SPOJ 694 DISUBSTR - Distinct Substrings
思路 求本质不同的子串个数,总共重叠的子串个数就是height数组的和 总子串个数-height数组的和即可 代码 #include <cstdio> #include <algor ...
- 【SPOJ 694】Distinct Substrings (更直接的求法)
[链接]h在这里写链接 [题意] 接上一篇文章 [题解] 一个字符串所有不同的子串的个数=∑(len-sa[i]-height[i]) [错的次数] 0 [反思] 在这了写反思 [代码] #inclu ...
- 【SPOJ 694】Distinct Substrings
[链接]h在这里写链接 [题意] 给你一个长度最多为1000的字符串 让你求出一个数x,这个x=这个字符串的不同子串个数; [题解] 后缀数组题. 把原串复制一份,加在 ...
- SPOJ 694 || 705 Distinct Substrings ( 后缀数组 && 不同子串的个数 )
题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀 ...
- 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 ...
- 后缀数组:SPOJ SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- Spoj SUBST1 New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ705 Distinct Substrings (后缀自动机&后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
随机推荐
- 离散对数&&大步小步算法及扩展
bsgs algorithm ax≡b(mod n) 大步小步算法,这个算法有一定的局限性,只有当gcd(a,m)=1时才可以用 原理 此处讨论n为素数的时候. ax≡b(mod n)(n为素数) 由 ...
- Java虚拟机13:Java类加载机制
前言 我们知道我们写的程序经过编译后成为了.class文件,.class文件中描述了类的各种信息,最终都需要加载到虚拟机之后才能运行和使用.而虚拟机如何加载这些.class文件?.class文件的信息 ...
- 【[IOI2014]Wall 砖墙】
好像随便一卡就最优解了 malao告诉我这道题挺不错的,于是就去写了写 这两个操作很有灵性啊,感觉这么有特点的数大概是需要分块维护的吧 但是并没有什么区间查询,只是在最后输出整个序列 于是我们就直接用 ...
- lazysizes-好用的延迟加载JS插件
此插件可直接引入lazysizes即可 <script src="lazysizes.min.js"></script> 延迟加载(lazy load)是( ...
- python -- peewee处理数据库连接
目前,实现了的Database子类有三个:SqliteDatabase.MySQLDatabase.PostgresqlDatabase class SqliteDatabase(Database) ...
- System.Reflection 获取描述
我们需要获取类,属性,方法的描述.这个跟获取枚举的描述一样,需要我们通过反射来做.这还需要我们的利用System.ComponentModel:Description 的属性来完成. 新建一个类:使 ...
- Linux 防止rm -rf 误删Shell脚本
#!/bin/bash #:set ff=unix #:set nobomb #-*- coding:utf-8 -*- ####################################### ...
- first-child伪类选择器
原文链接地址:https://www.cnblogs.com/wangmeijian/p/4562304.html :first-child 选择器用于选取属于其父元素的首个子元素的指定选择器.——w ...
- ubuntu下USB口插入USB转TTL查看串口号
首先先要获取权限 sudo su 然后 cd /devls ls可以列出所有的串口号(确保此时USB转TTL已经插在电脑上了) 然后拔掉USB转TTL 在ls一下列出所有的串口设备 对比可以发现,插上 ...
- NSDate|NSTimeZone|时区|日历
NSDate,NSDateFormatter以及时区转换-开发者-51CTO博客 iOS 时区转换 东八区 - 简书 iOS时间的时区转换以及一些方法记录 - 简书 iOS - OC NSTimeZo ...