F - New Distinct Substrings (后缀数组)
题目链接:https://cn.vjudge.net/contest/283743#problem/F
题目大意:给你一个字符串,然后让你求出不同的子串的个数。
具体思路:首先,一个字符串中总的子串个数是len*(len+1)/2,然后就开始去重了,通过height数组,求出所有重复的子串的个数,然后就用总的子串的个数-重复的子串的个数就可以了。
AC代码:
#include<iostream>
#include<stack>
#include<cstring>
#include<iomanip>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
# define ll long long
const int maxn = 5e5+;
int cntA[maxn], cntB[maxn], sa[maxn], tsa[maxn], A[maxn], B[maxn], height[maxn];
int Rank[maxn];
char ch[maxn];
ll n;
//sa[i]代表第i小的后缀位置,Rank[i]代表第i位置开始的后缀在所有的后缀串中排名第几
// height[i]代表排名第i个字符串和第i-1个字符串的相同前缀的长度
void cal()
{
for(int i = ; i < ; i++) cntA[i] = ;
for(int i = ; i <= n; i++) cntA[ch[i-]]++;
for(int i = ; i < ; i++) cntA[i] += cntA[i-];
for(int i = n; i; i--) sa[cntA[ch[i-]]--] = i;
Rank[sa[]] = ;
for(int i = ; i <= n; i++)
{
Rank[sa[i]] = Rank[sa[i-]];
if(ch[sa[i]-] != ch[sa[i-]-]) Rank[sa[i]]++;
}
for(int l = ; Rank[sa[n]] < n; l <<= )
{
memset(cntA, , sizeof(cntA));
memset(cntB, , sizeof(cntB));
for(int i = ; i <= n; i++)
{
cntA[A[i] = Rank[i]]++;
cntB[B[i] = (i+l <= n)?Rank[i+l]:]++;
}
for(int i = ; i <= n; i++) cntB[i] += cntB[i-];
for(int i = n; i; i--) tsa[cntB[B[i]]--] = i;
for(int i = ; i <= n; i++) cntA[i] += cntA[i-];
for(int i = n; i; i--) sa[cntA[A[tsa[i]]]--] = tsa[i];
Rank[sa[]]=;
for(int i = ; i <= n; i++)
{
Rank[sa[i]] = Rank[sa[i-]];
if(A[sa[i]] != A[sa[i-]] || B[sa[i]] != B[sa[i-]]) Rank[sa[i]]++;
}
}
for(int i = , j = ; i <= n; i++)
{
if(j) j--;
while(ch[i+j-] == ch[sa[Rank[i]-] + j - ]) j++;
height[Rank[i]] = j;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",ch);
n=strlen(ch);
if(n==)
{
printf("1\n");
continue;
}
// cout<<1<<endl;
cal();
ll ans=n*(n+)/;
//cout<<ans<<endl;
for(int i=; i<=n; i++)
{
ans-=height[i];
// cout<<i<<" "<<height[i]<<endl;
}
printf("%lld\n",ans);
}
return ;
}
F - New Distinct Substrings (后缀数组)的更多相关文章
- SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数
题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...
- SPOJ - DISUBSTR 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 后缀数组
New Distinct Substrings 题意 给出T个字符串,问每个字符串有多少个不同的子串. 思路 字符串所有子串,可以看做由所有后缀的前缀组成. 按照后缀排序,遍历后缀,每次新增的前缀就是 ...
- SPOJ DISUBSTR Distinct Substrings 后缀数组
题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...
- SPOJ 694 || 705 Distinct Substrings ( 后缀数组 && 不同子串的个数 )
题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀 ...
- spoj Distinct Substrings 后缀数组
给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和 ...
- [spoj694&spoj705]New Distinct Substrings(后缀数组)
题意:求字符串中不同子串的个数. 解题关键:每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数. 1.总数减去height数组的和即可. 注意这里height中为什么不需 ...
- spoj 694. Distinct Substrings 后缀数组求不同子串的个数
题目链接:http://www.spoj.com/problems/DISUBSTR/ 思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照su ...
- SPOJ_705_New Distinct Substrings_后缀数组
SPOJ_705_New Distinct Substrings_后缀数组 题意: 给定一个字符串,求该字符串含有的本质不同的子串数量. 后缀数组的一个小应用. 考虑每个后缀的贡献,如果不要求本质不同 ...
- SPOJ- Distinct Substrings(后缀数组&后缀自动机)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
随机推荐
- LCT总结——应用篇(附题单)(LCT)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--概念篇戳这里 题单 灰常感谢XZY巨佬提供的强力资磁!(可参考XZY巨佬的博客总结) 题单对于系 ...
- A1070. Mooncake
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
- MATLAB:图像滤波,绝对值差(filter2,imabsdiff函数)
下面是对图像进行滤波,以及求滤波后的图像与原图像的绝对值差的实现过程,涉及到的函数有filter2,imabsdiff函数: close all; %关闭当前所有图形窗口,清空工作空间变量,清除工作空 ...
- springboot静态资源处理
转:https://blog.csdn.net/catoop/article/details/50501706 Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConf ...
- Makefile ------ .PHONY的作用
看下面的例子 Makefile文件 .PHONY: cleanclean: rm *.o 当Makefile文件所在目录有文件名为clean的文件,命令行“.PHONY: clean”又没添加的话,执 ...
- Eclipse启动时出现错误 An internal error occurred during: "Updating indexes"
在Eclipse的workspace下有个.metadata文件夹,Eclipse出现异常的log文件就在这个目录下. 最近出现了这样的错误: 查看日志文件发现: !ENTRY org.ecl ...
- 权限管理-ACL
权限管理-ACL 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ACL权限简介与开启 1.ACL权限简介 比如在根下有一个目录(”/yinzhengjie“),这个目录的所有者 ...
- nginx安装ngx_lua_waf防护
ngx_lua_waf基于ngx_lua的web应用防火墙,使用起来简单,高性能和轻量级. ♦防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击♦防止svn/备份之类 ...
- python---redis缓存页面前戏之剖析render源码
1.初始代码: def get(self, *args, **kwargs): import time tm = time.time() self.render('home/index.html', ...
- POJ - 1019 Number Sequence (思维)
https://vjudge.net/problem/POJ-1019 题意 给一串1 12 123 1234 12345 123456 1234567 12345678 123456789 1234 ...