题目链接:戳我

后缀自动机模板?

求不同的子串数量。

直接\(\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. VS2017 Intelligense C++ 设置的几个重点

    1,高级-点到箭头的转换,用于指针操作 2,高级-成员列表筛选模式-一定要选[模糊],而不要选[智能] 这样在我们输入 vkphdfea就能检索到vkPhysicalDeviceFeatures, 而 ...

  2. Python 多人聊天工具 ( 多线程 )

    程序实现: 1.单或多客户端使用 telnet 登陆服务端 ( 可远程 ) 进行会话 2.服务端实现登陆.注册.退出功能 3.客户端发送的消息会被广播到已经登陆的其他用户界面 4.连接到服务端后,可以 ...

  3. PHP 动态添加 Mcrypt 扩展库

    简介: PHP 动态添加 Mcrypt 扩展库,这是一个支持多种加密.解密算法.模式的扩展库. shell > php -m | grep mcrypt # 如果没有输出,就是缺少这个扩展 sh ...

  4. iOS 上的蓝牙框架 - Core Bluetooth for iOS

    原文: Core Bluetooth for iOS 6 Core Bluetooth 是在iOS5首次引入的,它允许iOS设备可以使用健康,运动,安全,自动化,娱乐,附近等外设数据.在iOS 6 中 ...

  5. js闭包的定义

    通过函数字面量创建的函数对象包含一个连接到外部上下文的连接,这叫做闭包. 还有一种定义:函数可以访问它被创建时所处的上下文环境,叫做闭包.

  6. lock free queues

    无锁队列,下面链接是源码,包含4种队列:单生产者单消费者/多生产者多消费者,队列定长/不定长.元素建议为简单数据类型,复杂类型都采用指针形式. queues-master.zip 源码来源:https ...

  7. 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组

    [抄题]: Given an array nums and a target value k, find the maximum length of a subarray that sums to k ...

  8. 8-机器分配(hud4045-组合+第二类斯特林数)

    http://acm.hdu.edu.cn/showproblem.php?pid=4045 Machine schedulingTime Limit: 5000/2000 MS (Java/Othe ...

  9. Java 代理机制

    Table of Contents 1 引言 2 常见的代理 3 代理模式UML图 4 代理模式实例 5 java动态代理 5.1 java动态代理UML图 6 代理模式与装饰者模式的区别 6.1 装 ...

  10. Nginx 事件基本处理流程分析

    说明:本文章重点关注事件处理模型.有兴趣的同学可以去http://tengine.taobao.org/book/查找更多资料.Tengine应该是淘宝基于Nginx自己做的修改.这个地址的文档还在不 ...