题目链接:戳我

后缀自动机模板?

求不同的子串数量。

直接\(\sum t[i].len-t[t[i].ff].len\)即可

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 1010
char a[MAXN];
long long ans;
int tot=1,last=1,T;
struct Node{int son[26],ff,len;}t[MAXN<<1];
inline 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()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
scanf("%s",a+1);
last=tot=1;
int lenth=strlen(a+1),ans=0;
memset(t,0,sizeof(t));
for(int i=1;i<=lenth;i++) extend(a[i]-'A');
for(int i=1;i<=tot;i++) ans+=t[i].len-t[t[i].ff].len;
printf("%lld\n",ans);
}
return 0;
}

SPOJ distinct subtrings的更多相关文章

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

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

  2. SPOJ Distinct Substrings【后缀数组】

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

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

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

  4. SPOJ - Distinct Substrings,求不同的字串个数!

    DISUBSTR - Distinct Substrings 题意:给你一个长度最多1000的字符串,求不相同的字串的个数. 思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已 ...

  5. SPOJ Distinct Substrings SA

    正解:SA 解题报告: 传送门! 啊先给个翻译趴QwQ大概就是说给个字符串,求互不相等的子串的个数 算是道小水题辣趴,,,并不难想到的呢QAQ只是因为是新知识所以巩固下而已QAQ 然后就显然考虑合法方 ...

  6. spoj Distinct Substrings 后缀数组

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

  7. SPOJ Distinct Substrings

    给定一个字符串,求不相同子串个数.每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同子串个数.总数为n*(n-1)/2,再减掉height[i]的和就是答案 #include< ...

  8. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

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

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...

随机推荐

  1. kibana-4.6.3-linux-x86_64.tar.gz的安装(图文详解)(升级)

     前期博客 kibana-4.6.3-linux-x86_64.tar.gz的下载(图文详解) 因为,我的机器情况是如下: 1.上传 [hadoop@master app]$ rz [hadoop@m ...

  2. 添加 MyEclipse Persistence Tools 类库

    1).右键点击你的项目,然后选择Properties.2).在 Java Build Path 页面, 在 Libraries 面板下选择 Add Library….3).选择 MyEclipse L ...

  3. gvim

    [gvim] 1.gvim的配置文件在安装目录下,文件名为_vimrc. 2.通过以下命令选择配色方案:

  4. 违反“Don’t Modify Objects You Don’t Own”原则对SharePoint页面带来的影响

    最近看了一本相当不错的JavaScript书,<Maintainable JavaScript>(中文版叫做<编写可维护的JavaScript>),"Don't Mo ...

  5. 高性能Web服务器Nginx的配置与部署研究(14)平滑升级你的Nginx

    1.概述(可以直接跳过看第2部分) Nginx方便地帮助我们实现了平滑升级.其原理简单概括,就是: (1)在不停掉老进程的情况下,启动新进程. (2)老进程负责处理仍然没有处理完的请求,但不再接受处理 ...

  6. Scrapy Test

    (flappbird) luo@luo-ThinkPad-W540:~$ scrapy startproject myspider0315New Scrapy project 'myspider031 ...

  7. scrapy定制爬虫-爬取javascript——乾颐堂

    很多网站都使用javascript...网页内容由js动态生成,一些js事件触发的页面内容变化,链接打开.甚至有些网站在没有js的情况下根本不工作,取而代之返回你一条类似"请打开浏览器js& ...

  8. 关于更新vs2017后发布的问题 命令“bower install”已退出,代码为 9009

    更新vs2017 尝试发布 出现  命令“bower install”已退出,代码为 9009 然后我点工具测试了一下nodejs 出现下图弹窗  百度了一下 没找到对策,有没有大侠知道怎么解决 解决 ...

  9. .NET开源MSSQL、Redis监控产品Opserver之MSSQL配置

    MSSQL的配置比较简单,主要包括三部分: 默认配置(defaultConnectionString).集群配置(clusters).单实例配置(instances) defaultConnectio ...

  10. [GO]空接口

    package main import "fmt" //空接口的实际意义就在于在使用函数时,空接口可以接收任意类型的值,类似于python中的*args, **kwargs fun ...