SPOJ Distinct Substrings SA
正解:SA
解题报告:
啊先给个翻译趴QwQ大概就是说给个字符串,求互不相等的子串的个数
算是道小水题辣趴,,,并不难想到的呢QAQ只是因为是新知识所以巩固下而已QAQ
然后就显然考虑合法方案就会是所有方案-不合法方案
所有方案显然是n*(n+1)/2,不合法方案就是相等的子串的个数
考虑相等的子串的个数怎么求?不就是,∑height[i]
欧克做完了
#include<bits/stdc++.h>
using namespace std;
#define il inline
#define gc getchar()
#define ri register int
#define rb register bool
#define rc register char
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i) const int N=+;
int x[N],y[N],sa[N],rk[N],t[N],n,as;
char ch[N]; il int read()
{
rc ch=gc;ri x=;rb y=;
while(ch!='-' && (ch>'' || ch<''))ch=gc;
if(ch=='-')ch=gc,y=;
while(ch>='' && ch<='')x=(x<<)+(x<<)+(ch^''),ch=gc;
return y?x:-x;
}
il bool cmp(ri gd,ri gs,ri k){return y[gd]==y[gs] && y[gd+k]==y[gs+k];}
il void SA()
{
ri m=,h=;
rp(i,,n)++t[x[i]=ch[i]-'A'+];
rp(i,,m)t[i]+=t[i-];
my(i,n,)sa[t[x[i]]--]=i;
for(ri k=;k<=n;k<<=)
{
ri p=;
rp(i,,n)y[i]=;rp(i,,m)t[i]=;
rp(i,n-k+,n)y[++p]=i;rp(i,,n)if(sa[i]>k)y[++p]=sa[i]-k;
rp(i,,n)++t[x[y[i]]];
rp(i,,m)t[i]+=t[i-];
my(i,n,)sa[t[x[y[i]]]--]=y[i];
swap(x,y);
x[sa[]]=p=;
rp(i,,n)x[sa[i]]=cmp(sa[i],sa[i-],k)?p:++p;
if(p>=n)break;m=p;
}
rp(i,,n)rk[sa[i]]=i;
rp(i,,n)
{
if(h)--h;
while(ch[i+h]==ch[sa[rk[i]-]+h])++h;
as-=h;
}
} int main()
{
ri T=read();
while(T--)
{
memset(x,,sizeof(x));memset(y,,sizeof(y));memset(t,,sizeof(t));memset(sa,,sizeof(sa));memset(rk,,sizeof(rk));
scanf("%s",ch+);n=strlen(ch+);as=n*(n+)/;SA();printf("%d\n",as);
}
return ;
}
这儿是代码QwQ
SPOJ Distinct Substrings SA的更多相关文章
- 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 后缀数组
给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和 ...
- SPOJ Distinct Substrings
给定一个字符串,求不相同子串个数.每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同子串个数.总数为n*(n-1)/2,再减掉height[i]的和就是答案 #include< ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- SPOJ 694 Distinct Substrings
Distinct Substrings Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. O ...
随机推荐
- iOS开发之资料收集
github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自github:https://github ...
- Effective Java 第三版——79. 避免过度同步
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- 射频与微波测量之S参数
转自:https://www.cnblogs.com/lyh523329053/p/9128577.html S参数 S散射也叫散射参数.是微波传输中的一组重要参数.由于我们很难在高频率时测量电流或电 ...
- 在Python中定义和使用抽象类的方法
https://www.jb51.net/article/87710.htm 像java一样python也可以定义一个抽象类. 在讲抽象类之前,先说下抽象方法的实现. 抽象方法是基类中定义的方法,但却 ...
- 【转载】systemctl命令完全指南
Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器. Systemd是一个系统管理守护进程.工具和库的集合,用于取代System V初始进程.Systemd的功能是 ...
- C#学习笔记(35)——事件做的登录案例
说明(2018-4-9 20:11:42): 1. 先自定义了一个登录控件,可以输入账号.密码,点击登录.然后在Form1里面拖入这个控件,要求输入账号密码正确时,点击登录,控件显示绿色,否则显示红色 ...
- 现代php编程
自动加载__autolaod和spl_autoload_register() 自动加载就是指如果找不到某个类如何处理的方式,具体可参见此文,可以说spl_autoload_register是更加高级, ...
- extjs ajax 同步 及 confirm 确认提示框问题
//上传文件 uploadModel: function() { if(Ext.getCmp('exup').getForm().isValid()) { var ssn = this.upPanel ...
- dma 测试例子
#include <linux/module.h> #include <linux/slab.h> #include <linux/sched.h> #includ ...
- 大数据架构:搭建CDH5.5.1分布式集群环境
yum install -y ntp gcc make lrzsz wget vim sysstat.x86_64 xinetd screen expect rsync bind-utils ioto ...