SPOJ distinct subtrings
题目链接:戳我
后缀自动机模板?
求不同的子串数量。
直接\(\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的更多相关文章
- 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 SA
正解:SA 解题报告: 传送门! 啊先给个翻译趴QwQ大概就是说给个字符串,求互不相等的子串的个数 算是道小水题辣趴,,,并不难想到的呢QAQ只是因为是新知识所以巩固下而已QAQ 然后就显然考虑合法方 ...
- 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(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
随机推荐
- jquery排序与动态添加option以及属性
function getOrgansid() { url="<%=basePath%>/rest/bsc/organ/selectOrganSidAllList"; $ ...
- mysql语句与sql语句的基本区别
. MySQL支持enum和set类型,SQL Server不支持: . MySQL不支持nchar.nvarchar.ntext类型: . MySQL数据库的递增语句是AUTO_INCREMENT, ...
- Linux实战教学笔记14:用户管理初级(上)
第十四节 用户管理初级(上) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,账号管理 1.1 管理用户命令汇总 命令 注释说明(特殊颜色的必须掌握) useradd增 ...
- IEnumerator & IEnumerable
[IEnumerator] 用于遍历一个对象,IEnumerator在System.Collections命名空间中. public interface IEnumerator { object Cu ...
- git的突出解决--git rebase之abort、continue、skip
(1)应用实例描述 假设在github或者gitoschina上建立了一个项目,默认分支为master分支,远程master分支上c.sh文件内容: 开发者A.B分别将项目拷贝到自己本地进行开发 某一 ...
- 配置atom需要安装的插件
本人是一个node后端开发,这里之记录我需要的atom插件,如果需要别的请去别处查找,也省这我自己再去找 1.atom-beautify 代码格式一键美化 2.minimap 代码小地图 3 ...
- Spring学习笔记(四)--MVC概述
一. 飞机 最近马来西亚航空370号班机事故闹得沸沸扬扬,情节整的扑朔迷离,连我在钻研springMVC平和的心情都间接的受到了影响.正当我在想这个MVC的处理过程可以怎样得到更好的理解呢?灰机,灰机 ...
- 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计
[抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...
- eclipse 使用 scons 编译的配置说明
eclipse版本: eclipse-cpp-kepler-SR1-win32.zip 创建项目必须选择“Makefile Project” 然后进入“Projects Properities” 先 ...
- CecOS Virtualization
CecOS CecOS 是 OPENFANS 为中小型企业提供企业开源云计算解决方案的基础框架:CecOSvt 是一款运行在社区企业云操作系统(CecOS)上的虚拟化开放式解决方案. 也可用演示模式. ...