SPOJ 694 (后缀数组) Distinct Substrings
将所有后缀按照字典序排序后,每新加进来一个后缀,它将产生n - sa[i]个前缀。这里和小罗论文里边有点不太一样。
height[i]为和字典序前一个的LCP,所以还要减去,最终累计n - sa[i] - height[i]即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
char s[maxn];
int sa[maxn], rank[maxn], height[maxn];
int t[maxn], t2[maxn], c[maxn];
int n; void build_sa(int n, int m)
{
int i, *x = t, *y = t2;
for(i = ; i < m; i++) c[i] = ;
for(i = ; i < n; i++) c[x[i] = s[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 <<= )
{
int 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 build_height()
{
int k = ;
for(int i = ; i <= n; i++) rank[sa[i]] = i;
for(int i = ; i < n; i++)
{
if(k) k--;
int j = sa[rank[i] - ];
while(s[i + k] == s[j + k]) k++;
height[rank[i]] = k;
}
} int main()
{
//freopen("in.txt", "r", stdin); int T; scanf("%d", &T);
while(T--)
{
scanf("%s", s);
n = strlen(s);
memset(sa, , sizeof(sa));
build_sa(n + , );
build_height(); int ans = ;
for(int i = ; i <= n; i++)
ans += n - sa[i] - height[i];
printf("%d\n", ans);
} return ;
}
代码君
SPOJ 694 (后缀数组) Distinct Substrings的更多相关文章
- spoj 694(后缀数组)
题意:求一个字符串的不重复子串的个数. 分析:对于下标为i的位置,能够产生的前缀子串个数为len-i(下标从0开始),对于与它字典序相邻的后缀产生的子串是重复的(就是他们的最长公共前缀),所以我们要减 ...
- Spoj-DISUBSTR - Distinct Substrings~New Distinct Substrings SPOJ - SUBST1~(后缀数组求解子串个数)
Spoj-DISUBSTR - Distinct Substrings New Distinct Substrings SPOJ - SUBST1 我是根据kuangbin的后缀数组专题来的 这两题题 ...
- Distinct Substrings SPOJ - DISUBSTR 后缀数组
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- 【SPOJ – SUBST1】New Distinct Substrings 后缀数组
New Distinct Substrings 题意 给出T个字符串,问每个字符串有多少个不同的子串. 思路 字符串所有子串,可以看做由所有后缀的前缀组成. 按照后缀排序,遍历后缀,每次新增的前缀就是 ...
- 705. New Distinct Substrings spoj(后缀数组求所有不同子串)
705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...
- SPOJ 题目705 New Distinct Substrings(后缀数组,求不同的子串个数)
SUBST1 - New Distinct Substrings no tags Given a string, we need to find the total number of its di ...
- SPOJ - SUBST1 D - New Distinct Substrings
D - New Distinct Substrings 题目大意:求一个字符串中不同子串的个数. 裸的后缀数组 #include<bits/stdc++.h> #define LL lon ...
- SPOJ PHRASES 后缀数组
题目链接:http://www.spoj.com/problems/PHRASES/en/ 题意:给定n个字符串,求一个最长的子串至少在每个串中的不重叠出现次数都不小于2.输出满足条件的最长子串长度 ...
- SPOJ REPEATS 后缀数组
题目链接:http://www.spoj.com/problems/REPEATS/en/ 题意:首先定义了一个字符串的重复度.即一个字符串由一个子串重复k次构成.那么最大的k即是该字符串的重复度.现 ...
随机推荐
- Excel每隔两行自动求和一次怎么操作?
今天ytkah得到一份数据,要求进行统计分析,由于是原始数据,还没处理过,数据量有点大,如下图所示(Excel每隔两行自动求和),每天的数字由两项组成,男生的人数.消费值和女生的人数和消费值,数字都在 ...
- HTTP协议中的5类状态码
① 客户方错误 100 继续 101 交换协议 ② 成功 200 OK 201 已创建 202 接收 ...
- POJ 1144 Network(Tarjan)
题目链接 题意 : 找出割点个数. 思路 : Tarjan缩点,u是割点的充要条件是:u要么是具有两个以上子女的深度优先生成树的根,要么不是根,而有一个子女v满足low[v]>=dfn[u]. ...
- 微软VSS的超级BUG
发现问题:今天一个新同事,无意中发现他直接就登录进VSS了,并且还是“admin”用户: 解决问题:于是开始在网上找度娘和谷歌帮忙,真是不查不要紧,一查吓一跳,VSS本身就存在这个bug,并且是一个超 ...
- hadoop 1 testcase运行方法
转入hadoop2.0后,逐渐忘记了之前做testcase运行的方法,记录一下: ant -Dtestcase=Test*** 如果只运行core包得testcase可以 an ...
- proc插入数据到数据库
#include<stdio.h>EXEC SQL INCLUDE SQLCA; void insert (char password_[6],char id_[20],int balan ...
- ./jad: error while loading shared libraries: libstdc++-libc6.2-2.so.3: cannot open shared object file: No such file or directory
Ubuntu 上使用jad,出现上面错误: ./jad: error while loading shared libraries: libstdc++-libc6.2-2.so.3: cannot ...
- WordPress主题制作教程9:文章形式
wordpress的文章形式: aside -----------------日志:不显示标题的标准文章image --------------图像:单张图像.文章中的首个 <img /> ...
- jquery.chosen.js实现模糊搜索
jquery.chosen.js查询时,chosen默认从第一个字符搜索,所以写中间的字符搜索时,是搜索不出来的 若想实现中间字符的模糊查询,下面的js中(search_contains属性为true ...
- swift:创建九宫格
九宫格的创建是有规律可循的,每一个格子的水平和竖直方向的间隔是固定的,通过计算每一个格子的位置,然后把它们依次放到视图中即可. 一般的步骤为: 1.设置格子的总个数和每一列的格子数 2.求格子的间隔: ...