SPOJ 705 New Distinct Substrings
后缀数组。按照排序完的后缀一个一个统计。每一个后缀对答案做出的贡献为:n-SA[i]-height[i]。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=+; int wa[maxn],wb[maxn],wv[maxn],WS[maxn];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=; i<m; i++) WS[i]=;
for(i=; i<n; i++) WS[x[i]=r[i]]++;
for(i=; i<m; i++) WS[i]+=WS[i-];
for(i=n-; i>=; i--) sa[--WS[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,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<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) WS[i]=;
for(i=; i<n; i++) WS[wv[i]]++;
for(i=; i<m; i++) WS[i]+=WS[i-];
for(i=n-; i>=; i--) sa[--WS[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return;
} int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) rank[sa[i]]=i;
for(i=; i<n; height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-]; r[i+k]==r[j+k]; k++);
return;
} int T,n,a[maxn],SA[maxn];
char str[maxn]; int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%s",str); n=strlen(str);
for(int i=;i<n;i++) a[i]=(int)str[i];
a[n]=; da(a,SA,n+,); calheight(a,SA,n);
LL ans=;
for(int i=;i<=n;i++) ans=ans+n-SA[i]-height[i];
printf("%lld\n",ans);
}
return ;
}
SPOJ 705 New Distinct Substrings的更多相关文章
- 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 ...
- 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 - 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 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 —— 后缀数组 单个字符串的子串个数
题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...
- 【SPOJ】694. Distinct Substrings
http://www.spoj.com/problems/DISUBSTR/ 题意:求字符串不同子串的数目. #include <bits/stdc++.h> using namespac ...
- 【SPOJ 694】Distinct Substrings 不相同的子串的个数
不会FQ啊,没法评测啊,先存一下代码QAQ 2016-06-16神犇Menci帮我测过AC了,谢谢神犇Menci QwQ #include<cstdio> #include<cstr ...
随机推荐
- Docker 总结
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Docker总结 简单介绍 1 Docker 架构 安装和环境配置 1 mac 11 brew安装 11 dmg文件安装 1 ...
- 取消a标签在移动端点击时的背景颜色
一.取消a标签在移动端点击时的蓝色 -webkit-tap-highlight-color: rgba(255, 255, 255, 0); -webkit-user-select: none; -m ...
- 解决Windows内存问题的两个小工具RamMap和VMMap(这个更牛更好)
来源:http://www.cr173.com/html/13006_1.html .net程序内存监测分配工具(CLR Profiler for .NET Framework 4)官方安装版 类型: ...
- 为什么new的普通数组用delete 和 delete[]都能正确释放
由同事推荐的一篇博客: 为何new出的对象数组必须要用delete[]删除,而普通数组delete和delete[]都一样-------_CrtMemBlockHeader 文章解释了delete 内 ...
- 在UE4中使用SVN作为source control工具
==========预先处理 1.到这个目录下 2.鼠标在空白处 按住shift键 同时右键 会多出一个 可以打开的cmd 3.输入命令,修改红线部分. me: 登陆svn地址的用户名, URL网址: ...
- java变量初始化
java全局变量会自动初始化,但局部变量不会自动初始化.当我们新建一个对象的时候,java会申请一个区域存放类的数据,而成员变量就是类的数据,也是放在这个内存区域中,jvm申请内存时初始化.而方法中变 ...
- 第八十二节,CSS3过渡效果
CSS3过渡效果 学习要点: 1.过渡简介 2.transition-property 3.transition-duration 4.transition-timing-function 5.tra ...
- JS的异步回调函数
hi :)几日不见,趁着周末和父母在广州走走逛逛,游山玩水,放松身心,第一天上班就被一个问题难住了,不废话,以下是关于JS函数回调方面的知识,今天的查阅看的也是一知半解,摘录下来日后慢慢琢磨! js中 ...
- ckplayer 项目实战
<div class="control-group" id="videoDiv" style="display: none;"> ...
- jQuery技巧大放送【转】
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...