spoj 694 705 不相同的子串的个数
http://www.spoj.com/problems/SUBST1/
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
代码:
//论文题,每个子串都是某个后缀的前缀,因此每个后缀的贡献是len-sa[i],去掉重复的就是len-sa[i]-he[i];
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int he[MAXN+],ra[MAXN+],sa[MAXN+],xx[MAXN+],yy[MAXN+],buc[MAXN+];
char s[MAXN+];
int len,m;
void get_suf()
{
int *x=xx,*y=yy;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[i]=s[i]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[i]]]=i;
for(int k=;k<=len;k<<=){
int p=;
for(int i=len-;i>=len-k;i--) y[p++]=i;
for(int i=;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[y[i]]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[y[i]]]]=y[i];
swap(x,y);
p=;x[sa[]]=;
for(int i=;i<len;i++){
if(y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k])
x[sa[i]]=p-;
else x[sa[i]]=p++;
}
if(p>=len) break;
m=p;
}
for(int i=;i<len;i++) ra[sa[i]]=i;
int k=;
for(int i=;i<len;i++){
if(ra[i]==) { he[]=;continue; }
if(k) k--;
int j=sa[ra[i]-];
while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
he[ra[i]]=k;
}
}
int solve()
{
int ans=len-sa[];
for(int i=;i<len;i++){
ans+=(len-sa[i]-he[i]);
}
printf("%d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%s",s);
len=strlen(s);
m=;
get_suf();
solve();
}
return ;
}
spoj 694 705 不相同的子串的个数的更多相关文章
- SPOJ 694 || 705 Distinct Substrings ( 后缀数组 && 不同子串的个数 )
题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀 ...
- 洛谷2408不同字串个数/SPOJ 694/705 (后缀数组SA)
真是一个三倍经验好题啊. 我们来观察这个题目,首先如果直接整体计算,怕是不太好计算. 首先,我们可以将每个子串都看成一个后缀的的前缀.那我们就可以考虑一个一个后缀来计算了. 为了方便起见,我们选择按照 ...
- SPOJ 694/705 后缀数组
思路: 论文题*n Σn-i-ht[i]+1 就是结果 O(n)搞定~ //By SiriusRen #include <cstdio> #include <cstring> ...
- SPOJ 694 && SPOJ 705 (不重复子串个数:后缀数组)
题意 给定一个字符串,求它的所有不重复子串的个数 思路 一个字符串的子串都必然是它的某个后缀的前缀.对于每一个sa[i]后缀,它的起始位置sa[i],那么它最多能得到该后缀长度个子串(n-sa[i]个 ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- SPOJ 694 Distinct Substrings(不相同子串个数)
https://vjudge.net/problem/SPOJ-DISUBSTR 题意: 给定一个字符串,求不相同的子串的个数. 思路: #include<iostream> #inclu ...
- SPOJ - DISUBSTR 多少个不同的子串
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- SPOJ 694 Distinct Substrings
Distinct Substrings Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. O ...
- 后缀数组 SPOJ 694 Distinct Substrings
题目链接 题意:给定一个字符串,求不相同的子串的个数 分析:我们能知道后缀之间相同的前缀的长度,如果所有的后缀按照 suffix(sa[0]), suffix(sa[1]), suffix(sa[2] ...
随机推荐
- Centos7 Zabbix添加主机、图形、触发器
制作自定义key zabbix自带模板Template OS Linux (Template App Zabbix Agent)提供CPU.内存.磁盘.网卡等常规监控,只要新加主机关联此模板,就可自动 ...
- UI优秀框架(库)
1.vux 官网:https://doc.vux.li/zh-CN/ Github:https://github.com/airyland/vux 13818 Stars 3064 Forks ...
- c++ Dynamic Memory (part 2)
Don't use get to initialize or assign another smart pointer. The code that use the return from get c ...
- Buy the Ticket HDU 1133 卡特兰数应用+Java大数
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- 作业要求20181113-4 Beta阶段第1周/共2周 Scrum立会报告+燃尽图 03
作业要求:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2385 版本控制:[https://git.coding.net/lglr201 ...
- 阿帕奇web服务器下载部署安装运行
链接: https://jingyan.baidu.com/album/d8072ac47baf0eec95cefdca.html?picindex=4 1.apache服务安装成功可是启动失败“wi ...
- “来用”Beta版使用说明
补发Beta版使用说明.Beta版与alpha版相比去掉了计算器,界面上没有太大变化. 1引言 1 .1编写目的 针对我们发布的Beta版本做出安装和使用说明,使参与内测的人员及用户了解软件的使用方法 ...
- Spring中jdbc Template使用
http://1358440610-qq-com.iteye.com/blog/1826816
- spring表单—乱码解决方案
1.POST方式提交 A.jsp页面POST方式提交表单 B.web.xml配置过滤器 <!-- spring过滤器解决乱码问题 --> <filter> <filter ...
- 第七次JAVA语言笔记