Spoj SUBST1 New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings.
Input
T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 50000
Output
For each test case output one number saying the number of distinct substrings.
Example
Input:
2
CCCCC
ABABA Output:
5
9 就是让你求一下一个串的本质不同的子串的个数。
这个就等价于求一下后缀自动机每个节点的权值和,每个节点的权值等于(max{}-min{}+1)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
#define maxn 200005
int f[maxn],ch[maxn][26],cnt=1;
int n,siz[maxn],l[maxn],T,las=1;
int a[maxn],c[maxn];
char s[maxn];
ll ans=0; inline void init(){
cnt=las=1;
memset(f,0,sizeof(f));
memset(ch,0,sizeof(ch));
memset(c,0,sizeof(c));
siz[1]=l[1]=ans=0;
} inline void ins(int x){
int p=las,np=++cnt;
las=np,l[np]=l[p]+1;
siz[np]=1; for(;p&&!ch[p][x];p=f[p]) ch[p][x]=np;
if(!p) f[np]=1;
else{
int q=ch[p][x];
if(l[q]==l[p]+1) f[np]=q;
else{
int nq=++cnt;
l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
f[nq]=f[q];
f[q]=f[np]=nq;
for(;ch[p][x]==q;p=f[p]) ch[p][x]=nq;
}
}
} inline void build(){
for(int i=0;i<n;i++) ins(s[i]-'a');
} inline void solve(){
for(int i=1;i<=cnt;i++) ans+=(ll)(l[i]-l[f[i]]);
} int main(){
scanf("%d",&T);
while(T--){
init();
scanf("%s",s);
n=strlen(s);
build();
solve();
printf("%lld\n",ans);
} return 0;
}
Spoj SUBST1 New Distinct Substrings的更多相关文章
- SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数
题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...
- 后缀数组: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【SAM||SA】
SAM里的转台不会有重复串,所以答案就是每个right集合所代表的串个数的和 #include<iostream> #include<cstdio> #include<c ...
- SPOJ SUBST1 New Distinct Substrings(后缀数组 本质不同子串个数)题解
题意: 问给定串有多少本质不同的子串? 思路: 子串必是某一后缀的前缀,假如是某一后缀\(sa[k]\),那么会有\(n - sa[k] + 1\)个前缀,但是其中有\(height[k]\)个和上一 ...
- 【刷题】SPOJ 705 SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ 694&&SPOJ705: Distinct Substrings
DISUBSTR - Distinct Substrings 链接 题意: 询问有多少不同的子串. 思路: 后缀数组或者SAM. 首先求出后缀数组,然后从对于一个后缀,它有n-sa[i]-1个前缀,其 ...
- 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 ...
- SP705 SUBST1 - New Distinct Substrings
\(\color{#0066ff}{ 题目描述 }\) 给定一个字符串,求该字符串含有的本质不同的子串数量. \(\color{#0066ff}{输入格式}\) T- number of test c ...
- [SPOJ]DISUBSTR:Distinct Substrings&[SPOJ]SUBST1:New Distinct Substrings
题面 Vjudge Vjudge Sol 求一个串不同子串的个数 每个子串一定是某个后缀的前缀,也就是求所有后缀不同前缀的个数 每来一个后缀\(suf(i)\)就会有,\(len-sa[i]+1\)的 ...
随机推荐
- Yapi的坑
前一段时间,研究WEB Api相关的工具. YApi 可以内网部署,内心十分的欢喜啊.而且gitHub上推荐超过4000星,成绩很优异嘛.然而通过最终的尝试,我还是打算放弃他,投入Postman的怀抱 ...
- python基础实践(三)
-*-列表是新手可直接使用的最强大的python功能之一,它融合了众多重要的编程概念.-*- # -*- coding:utf-8 -*-# Author:sweeping-monkQuestion_ ...
- os--留
os.path.abspath(path) #返回绝对路径 绝对路径和文件路径的区别,绝对路径是当前在操作文本的路径,文件路径是当前文本的文件的路径 os.path.basename(path) ...
- 孤荷凌寒自学python第五十一天初次尝试使用python连接Firebase数据库
孤荷凌寒自学python第五十一天初次尝试使用python连接Firebase数据库 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数据库服 ...
- HDU 4741 Save Labman No.004 ( 三维计算几何 空间异面直线距离 )
空间异面直线的距离直接套模板. 求交点:求出两条直线的公共法向量,其中一条直线与法向量构成的平面 与 另一条直线 的交点即可.还是套模板o(╯□╰)o 1.不会有两条线平行的情况. 2.两条直线可能相 ...
- Oracle设置用户密码永不过期
1.查看用户的profile是那个,一般是default: select username, profile from dba_users; 2.查看指定概要文件(如default)的密码有效期设置: ...
- iOS大神班笔记01-项目中常见的文件
1.Info.plist文件:项目配置文件 主要作用:保存应用的信息,软件名称等等,相当于身份 证.程序加载首先加载配置文件,读取软件名称等信息. Bundle display name:项目名称 B ...
- vue-router中scrollBehavior的巧妙用法
问题:使用keep-alive标签后部分安卓机返回缓存页位置不精确问题 解决方案: <div id="app"> <keep-alive> <rout ...
- 解决某些PC站在手机端宽度显示不正常的问题
可以打开控制台查看html标签的宽度,发现不是当前屏幕的宽度,更改下宽度即可:用js控制下,上代码 document.getElementsByTagName('html')[0].style.wid ...
- Heine-Borel定理
前:开始学数学..来写写理解和补充吧.. 书:M.A.Armstrong著<Basic Topology> Heine-Borel定理:实轴上闭区间是紧集. 证法(1)延伸法: 思想 闭区 ...