后缀数组 SPOJ 694 Distinct Substrings
题意:给定一个字符串,求不相同的子串的个数
分析:我们能知道后缀之间相同的前缀的长度,如果所有的后缀按照 suffix(sa[0]), suffix(sa[1]), suffix(sa[2]), …… ,suffix(sa[n])的顺序计算,不难发现,对于每一次新加进来的后缀 suffix(sa[k]),它将产生 n-sa[k]+1 个新的前缀。但是其中有 height[k]个是和前面的字符串的前缀是相同的。所以 suffix(sa[k])将“贡献” 出 n-sa[k]+1- height[k]个不同的子串。累加后便是原问题的答案。
这个”贡献“是不会重叠的,因为每次加的都是对于当前来说是没有与之相同的子串。
#include <cstdio>
#include <cstring>
#include <algorithm> const int N = 1e3 + 5;
int sa[N], rank[N], height[N];
int t[N], t2[N], c[N];
char s[N]; void da(char *s, int n, int m = 128) {
int i, p, *x = t, *y = t2;
for (i=0; i<m; ++i) c[i] = 0;
for (i=0; i<n; ++i) c[x[i]=s[i]]++;
for (i=1; i<m; ++i) c[i] += c[i-1];
for (i=n-1; i>=0; --i) sa[--c[x[i]]] = i;
for (int k=1; k<=n; k<<=1) {
for (p=0, i=n-k; i<n; ++i) y[p++] = i;
for (i=0; i<n; ++i) if (sa[i] >= k) y[p++] = sa[i] - k;
for (i=0; i<m; ++i) c[i] = 0;
for (i=0; i<n; ++i) c[x[y[i]]]++;
for (i=0; i<m; ++i) c[i] += c[i-1];
for (i=n-1; i>=0; --i) sa[--c[x[y[i]]]] = y[i];
std::swap (x, y);
p = 1; x[sa[0]] = 0;
for (i=1; i<n; ++i) {
x[sa[i]] = (y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k] ? p - 1 : p++);
}
if (p >= n) break;
m = p;
}
} void calc_height(int n) {
int i, k = 0;
for (i=0; i<n; ++i) rank[sa[i]] = i;
for (i=0; i<n; ++i) {
if (k) k--;
int j = sa[rank[i]-1];
while (s[i+k] == s[j+k]) k++;
height[rank[i]] = k;
}
} int n; int main() {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%s", s);
n = strlen (s);
n++;
da (s, n);
calc_height (n);
int ans = 0;
for (int i=0; i<n; ++i) {
ans += n - sa[i] - 1 - height[i];
}
printf ("%d\n", ans);
}
return 0;
}
后缀数组 SPOJ 694 Distinct Substrings的更多相关文章
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- 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 694 Distinct Substrings
Distinct Substrings Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. O ...
- spoj 694. Distinct Substrings 后缀数组求不同子串的个数
题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...
- SPOJ 694 Distinct Substrings(不相同子串个数)
https://vjudge.net/problem/SPOJ-DISUBSTR 题意: 给定一个字符串,求不相同的子串的个数. 思路: #include<iostream> #inclu ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings
[SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
随机推荐
- [Android] Android5.1系统自带的应用启动次数统计
reference to : http://blog.csdn.net/elder_sword/article/details/50508257 前段时间要做一个统计手机中激活量的东东,这个统计不是单 ...
- [Android Pro] 网络流量安全测试工具Nogotofail
reference to : http://www.freebuf.com/tools/50324.html 从严重的HeartBleed漏洞到苹果的gotofail 漏洞,再到最近的SSL v3 P ...
- [Android Pro] Android保存图片到系统图库
http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/ http://blog.csdn.net/x ...
- iOS 自定义返回按钮,保留系统滑动返回
原文链接 自定义返回按钮保留系统滑动返回手势.gif 1.简介 使用苹果手机,最喜欢的就是用它的滑动返回.作为一个开发者,我们在编写很多页面的时候,总是会因为这样那样的原因使得系统的滑动返回不可用.使 ...
- 51nod1006(lcs)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 题意:中文题诶- 思路:最长公共子序列模板题- 我们用d ...
- poj1456(贪心+并查集)
题目链接: http://poj.org/problem?id=1456 题意: 有n个商品, 已知每个商品的价格和销售截止日期, 每销售一件商品需要花费一天, 即一天只能销售一件商品, 问最多能买多 ...
- NYOJ题目112指数运算
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs0AAAIICAIAAAAaCETRAAAgAElEQVR4nO3drW7jWtwv4PcmwnMhxb ...
- WebApiのエラーメッセージをどうカスタマイズです?
本来是发布在客户的Wiki上,所以就用日语写. ------------------------------------------------------------------------ Web ...
- GENERATED_UCLASS_BODY 和 GENERATED_BODY 区别
the GENERATED_BODY() macro allows the class to build without having a constructor defined. If you ne ...
- [J2EE] 在Web如何取得相关路径
来自网络,自己整整一下: request.getRealPath("url"); // 虚拟目录映射为实际目录,不建议使用,使用ServletContext.getRealPath ...