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 ...
随机推荐
- JAVA8 之 Stream sorted() 示例
下面代码以自然序排序一个listlist.stream().sorted() 自然序逆序元素,使用Comparator 提供的reverseOrder() 方法list.stream().sorted ...
- Effective Java 第三版——73.抛出合乎于抽象的异常
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- AWS免费套餐服务器部署NETCORE网站
之前的linode充了5美元,一个月就用完了,还是创建的最便宜的服务器的!!! 以前一直想用用aws的所谓的免费套餐服务器的,现在linode过期了,可以试着用用了 下面是我的操作流程,包含错误及解决 ...
- 转:图像处理、显示中的行宽(linesize)、步长(stride)、间距(pitch)
在图像数据传输和显示的过程中有一个不常用的参数:间距. 间距的名称: 它有很多的别名,在使用d3d显示的时候,它叫pitch:在用ffmpeg解码的时候,它叫linesize: 在用ffmpeg转换格 ...
- golang:iconv
最近在做邮件解析的工作,遇到需要转字符集编码的情况,go官方好像没有提供这样的库,于是从github上找了一下. https://github.com/qiniu/iconv 开发环境: linux ...
- vim常用技巧
# vim常用技巧 ## 行操作------------------------------ 行首 0- 行尾 $- 第一个非空字符 ^ ## 列编辑模式----------------------- ...
- Java多线程:CountDownLatch、CyclicBarrier 和 Semaphore
场景描述: 多线程设计过程中,经常会遇到需要等待其它线程结束以后再做其他事情的情况. 有几种方案: 1.在主线程中设置一自定义全局计数标志,在工作线程完成时,计数减1.主线程侦测该标志是否为0,一 ...
- HttpWebRequest 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系
请求对象前加入 ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; 然后实现该方法 ...
- linux下反编译android apk
1.所需要的工具 1)apktool,功能:反编译出apk所需要的资源文件和布局设置文件等, 下载地址:https://code.google.com/p/android-apktool/downlo ...
- IIS 请求 超时设置
asp.net 默认的 session state 模式是 in proc(进程内),数据是在网站的应用程序池里面保存的.这样在 web.config 设置的超时时间,是在应用程序池没有发生回收的基础 ...