[SPOJ-DISUBSTR]Distinct Substrings
题意
给你一个串,求不同字串个数。
\(n\le10^5\)
sol
直接建SAM然后输出\(\sum_{i=1}^{tot}len[i]-len[fa[i]]\)
code
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5+5;
int T,n,last=1,tot=1,tr[N][26],fa[N],len[N];
char s[N];long long ans;
void extend(int c)
{
int v=last,u=++tot;last=u;
len[u]=len[v]+1;
while (v&&!tr[v][c]) tr[v][c]=u,v=fa[v];
if (!v) fa[u]=1;
else
{
int x=tr[v][c];
if (len[x]==len[v]+1) fa[u]=x;
else
{
int y=++tot;
memcpy(tr[y],tr[x],sizeof(tr[y]));
fa[y]=fa[x];fa[x]=fa[u]=y;len[y]=len[v]+1;
while (v&&tr[v][c]==x) tr[v][c]=y,v=fa[v];
}
}
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%s",s+1);n=strlen(s+1);
ans=0;last=tot=1;
memset(tr,0,sizeof(tr));
memset(fa,0,sizeof(fa));
memset(len,0,sizeof(len));
for (int i=1;i<=n;++i) extend(s[i]-'A');
for (int i=1;i<=tot;++i) ans+=len[i]-len[fa[i]];
printf("%lld\n",ans);
}
return 0;
}
[SPOJ-DISUBSTR]Distinct Substrings的更多相关文章
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ DISUBSTR Distinct Substrings 后缀数组
题意:统计母串中包含多少不同的子串 然后这是09年论文<后缀数组——处理字符串的有力工具>中有介绍 公式如下: 原理就是加上新的,减去重的,这题是因为打多校才补的,只能说我是个垃圾 #in ...
- DISUBSTR - Distinct Substrings
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- 【SPOJ】Distinct Substrings
[SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...
- 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
Distinct Substrings Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. O ...
- spoj694 DISUBSTR - Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- 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 ...
随机推荐
- 去除app中的标题栏
我之前一直用的是在oncreate方法中添加 requestWindowFeature(Window.FEATURE_NO_TITLE),并且必须写在setContentView(R.layout.a ...
- hdu1695(容斥 or 莫比乌斯反演)
刚开始看题,想了一会想到了一种容斥的做法.复杂度O( n(3/2) )但是因为题目上说有3000组测试数据,然后吓尿.完全不敢写. 然后想别的方法. 唉,最近精神有点问题,昨天从打完bc开始想到1点多 ...
- Black And White(DFS+剪枝)
Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others ...
- elasticsearch从入门到出门-03-多种搜索
1.query string search 2.query DSL 3.query filter 4.full-text search 5.phrase search 6.highlight sear ...
- php在不同平台下路径分隔符不同的解决办法
在看phpamf时看到一个常量“DIRECTORY_SEPARATOR”,最后发现是一个全局的常量,用来定义路径分隔符的 主要解决在windows和linux下路径分隔符不同的造成代码不通用的问题,在 ...
- 我的Android进阶之旅------> Android应用升级构想和要点总结
广大博友,看过后帮忙顶顶,谢谢大家!!! 转载请注明: http://blog.csdn.net/richway2010/article/details/6408258 [博主:各位博友,网友们,大家 ...
- linux多个分区合并为一个分区
备份# rsync -avP -e ssh /data xxx卸载# umount /data /data?设置分区类型为8e# fdisk /dev/sdb 创建PV# pvcreate /dev/ ...
- linux awk数组相关操作介绍
用awk进行文本处理,少不了就是它的数组处理.那么awk数组有那些特点,一般常见运算又会怎么样呢.我们先看下以下的一些介绍,结合样例我们会解说下它的不同之处.在 awk 中数组叫做关联数组(assoc ...
- LeetCode:完全平方数【279】【DP】
LeetCode:完全平方数[279][DP] 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示 ...
- [算法]在数组中找到出现次数大于N/K的数
题目: 1.给定一个整型数组,打印其中出现次数大于一半的数.如果没有出现这样的数,打印提示信息. 如:1,2,1输出1. 1,2,3输出no such number. 2.给定一个整型数组,再给 ...