【SPOJ】Distinct Substrings(后缀自动机)

题面

Vjudge

题意:求一个串的不同子串的数量

题解

对于这个串构建后缀自动机之后

我们知道每个串出现的次数就是\(right/endpos\)集合的大小

但是实际上我们没有任何必要减去不合法的数量

我们只需要累加每个节点表示的合法子串的数量即可

这个值等于\(longest-shortest+1=longest-parent.longest\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 1000100
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
char ch[MAX];
ll ans;
int tot=1,last=1;
struct Node
{
int son[26];
int ff,len;
}t[MAX<<1];
void extend(int c)
{
int p=last,np=++tot;last=np;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[p].len+1==t[q].len)t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q];t[nq].len=t[p].len+1;
t[q].ff=t[np].ff=nq;
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
int main()
{
int T=read();
while(T--)
{
scanf("%s",ch+1);
ans=0;last=tot=1;memset(t,0,sizeof(t));
for(int i=1,l=strlen(ch+1);i<=l;++i)extend(ch[i]-65);
for(int i=1;i<=tot;++i)ans+=t[i].len-t[t[i].ff].len;
printf("%lld\n",ans);
}
return 0;
}

【SPOJ】Distinct Substrings(后缀自动机)的更多相关文章

  1. SPOJ NSUBSTR Substrings 后缀自动机

    人生第一道后缀自动机,总是值得纪念的嘛.. 后缀自动机学了很久很久,先是看CJL的论文,看懂了很多概念,关于right集,关于pre,关于自动机的术语,关于为什么它是线性的结点,线性的连边.许多铺垫的 ...

  2. spoj 8222 Substrings (后缀自动机)

    spoj 8222 Substrings 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 解题思路:我们构造S的SAM,那么对于 ...

  3. SPOJ NSUBSTR Substrings ——后缀自动机

    建后缀自动机 然后统计次数,只需要算出right集合的大小即可, 然后更新f[l[i]]和rit[i]取个max 然后根据rit集合短的一定包含长的的性质,从后往前更新一遍即可 #include &l ...

  4. spoj Distinct Substrings 后缀数组

    给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB  BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和 ...

  5. 2019HNCPC C Distinct Substrings 后缀自动机

    题意 给定一个长度为n字符串,字符集大小为m(1<=n,m<=1e6),求\(\bigoplus_{c = 1}^{m}\left(h(c) \cdot 3^c \bmod (10^9+7 ...

  6. spoj 1812 lcsII (后缀自动机)

    spoj 1812 lcsII (后缀自动机) 题意:求多个串的lcs,最多10个串,每个串最长10w 解题思路:后缀自动机.先建好第一个串的sam,然后后面的串拿上去跑(这个过程同前一题).sam上 ...

  7. SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  8. ●SPOJ 8222 NSUBSTR–Substrings(后缀自动机)

    题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 后缀自动机的水好深啊!懂不了相关证明,带着结论把这个题做了.看来这滩深水要以后再来了. 本题要用到一个叫 R ...

  9. SPOJ Distinct Substrings【后缀数组】

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  10. spoj - Distinct Substrings(后缀数组)

    Distinct Substrings 题意 求一个字符串有多少个不同的子串. 分析 又一次体现了后缀数组的强大. 因为对于任意子串,一定是这个字符串的某个后缀的前缀. 我们直接去遍历排好序后的后缀字 ...

随机推荐

  1. String,StringBuffer,StringBuilder的区别

    数据结构: 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合. 比如数据库就是对硬盘中的数据进行有规则的管理,可以进行增删改查工作,而良好的数据结构可以优化这些操作, 也许大家会想这些和St ...

  2. hdfs文件按修改时间下载

    应用于:对于不同用户创建的表目录,进行文件的下载,程序中执行hadoop cat命令 下载文件到本地,随后通过ftp传至目标服务器,并将hdfs文件目录的修改时间存入mysql中.每次修改前将mysq ...

  3. Java基础点滴

    1. 关于interface的定义 [修饰符] interface 接口名 [extends 父接口名列表]{ [public] [static] [final] 常量;[public] [abstr ...

  4. Redis 实践2-数据结构

    alias redis-cli='/usr/local/redis/bin/redis-cli'   vi .bashrc 编辑加入  alias redis-cli='/usr/local/redi ...

  5. spring boot 使用java9上传到github其他人clone后报错

    错误原因: Java.lang.NoClassDefFoundError:javax/xml/bind/JAXBException jdk9存在版本兼容问题. 经过查找资料发现问题所在 大致意思是ja ...

  6. javascript selenium全套教程发布

    为什么有这个系列 目前javascript生态非常丰富,越来越多的人开始用js去做前端的ui测试了.而selenium是web ui测试的标准解决方案,所以一套js的selenium教程是很有必要的. ...

  7. Mybatis学习之道(一)

    本例子为采用的mysql+maven+mybatis构建. 初步学习mybatis: mybatis为一个半自动框架,相对于hibernate来说他更加轻巧,学习成本更低. 1.新建一个maven工程 ...

  8. iOS-UI控件优化

    一.tableView的优化 tableView作为iOS开发中使用最为频繁的控件之一,对其进行优化,对整个应用性能的提升显得至关重要.官方设计的框架中,已经包含了UITableViewCell的重用 ...

  9. 轻松搭建Git服务器(Ubuntu)

    搭建Git服务器 在远程仓库节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改. GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业 ...

  10. Google 浏览器被劫持怎么办?

    chrome://version/ 输入以上语句,在命令行中可看到恶意网址,复制该命令行,修改后面的网址即可