SPOJ Distinct Substrings
给定一个字符串,求不相同子串个数。
每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同子串个数。
总数为n*(n-1)/2,再减掉height[i]的和就是答案
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e6+;
int sa[maxn];
int t1[maxn];
int t2[maxn];
int c[maxn];
int rk[maxn];
int height[maxn];
void build_sa (int s[],int n,int m) {
int i,j,p;
int *x=t1,*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 (j=;j<=n;j<<=) {
p=;
for (i=n-j;i<n;i++) y[p++]=i;
for (i=;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
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-]+j]==y[sa[i]+j]?p-:p++;
if (p>=n) break;
m=p;
}
}
void getHeight (int s[],int n) {
int i,j,k=;
for (i=;i<=n;i++) rk[sa[i]]=i;
for (i=;i<n;i++) {
if (k) k--;
j=sa[rk[i]-];
while (s[i+k]==s[j+k]) k++;
height[rk[i]]=k;
}
}
bool check (int n,int k,int t) {
int num=;
for (int i=;i<=n;i++) {
if (height[i]>=t) {
num++;
if (num>=k) return true;
}
else num=;
}
return false;
}
char str[maxn];
int s[maxn];
int main () {
int T;
scanf ("%d",&T);
while (T--) {
scanf ("%s",str);
int n=strlen(str);
for (int i=;i<=n;i++) s[i]=str[i];
build_sa(s,n+,);
getHeight(s,n);
int ans=n*(n+)/;
for (int i=;i<=n;i++) ans-=height[i];
printf ("%d\n",ans);
}
return ;
}
SPOJ Distinct Substrings的更多相关文章
- SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- SPOJ Distinct Substrings【后缀数组】
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- spoj - Distinct Substrings(后缀数组)
Distinct Substrings 题意 求一个字符串有多少个不同的子串. 分析 又一次体现了后缀数组的强大. 因为对于任意子串,一定是这个字符串的某个后缀的前缀. 我们直接去遍历排好序后的后缀字 ...
- SPOJ - Distinct Substrings,求不同的字串个数!
DISUBSTR - Distinct Substrings 题意:给你一个长度最多1000的字符串,求不相同的字串的个数. 思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已 ...
- SPOJ Distinct Substrings SA
正解:SA 解题报告: 传送门! 啊先给个翻译趴QwQ大概就是说给个字符串,求互不相等的子串的个数 算是道小水题辣趴,,,并不难想到的呢QAQ只是因为是新知识所以巩固下而已QAQ 然后就显然考虑合法方 ...
- spoj Distinct Substrings 后缀数组
给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和 ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
随机推荐
- Redis事务实现原理
一:简介 Redis事务通常会使用MULTI,EXEC,WATCH等命令来完成,redis实现事务实现的机制与常见的关系型数据库有很大的却别,比如redis的事务不支持回滚,事务执行时会阻塞其它客户端 ...
- 1054 The Dominant Color
大致题意就是给出N行M列的元素,找出出现次数最多的元素并输出. #include<iostream> #include<unordered_map> using namespa ...
- 插件与App的跳转,及路由的关系
在SDK中 无法直接跳App 的界面,这个时候需要使用 路由,或者通过 NSClassFromString 的 presentViewController 来跳转. 直接贴代码: UIViewCont ...
- sysbench下载与安装
目标:下载.安装sysbench软件,做数据库压测 准备: 在sysbench启动的linux机器上,首先安装好mysql,查看mysql已经启动 例如,在机器上已经安装完mysql,其路径为 /us ...
- 正则表达式过滤url请求
过滤url中带reset的url请求 atgBusSignFilter.setSignUriRegex("^.*/reset/.*$")等价于 atgBusSignFilter.s ...
- jQuery 扩展 全屏切换
(function ($) { //全屏切换方法 $.fn.toggleFullScreen = function (qp1, qp2) { var fullFlag = false; var _qp ...
- 解决windows配置visual studio code调试golang环境问题
写这篇随笔是为了Mark下在这个过程中配到的几个问题 1.具体过程可参考https://www.cnblogs.com/JerryNo1/p/5412864.html,Jerry博主写的非常详细了 1 ...
- ASP.NET + MVC5 入门完整教程七 -—-- MVC基本工具(下)
https://blog.csdn.net/qq_21419015/article/details/80493633 Visual Stdio 的单元测试
- Educational Codeforces Round 76 (Rated for Div. 2) D. Yet Another Monster Killing Problem
You play a computer game. In this game, you lead a party of mm heroes, and you have to clear a dunge ...
- 用git无法连接github的解决方法
如果要從 GitHub 存取 Git 儲存庫,建議還是多採用 SSH 與 HTTPS 通訊協定最為穩定可靠,因此我的替代方案就是設定 Git 的全域設定值 ( –global ),預設將所有 git: ...