SP705 SUBST1 - New Distinct Substrings
\(\color{#0066ff}{ 题目描述 }\)
给定一个字符串,求该字符串含有的本质不同的子串数量。
\(\color{#0066ff}{输入格式}\)
T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 50000
\(\color{#0066ff}{输出格式}\)
For each test case output one number saying the number of distinct substrings.
\(\color{#0066ff}{输入样例}\)
2
CCCCC
ABABA
\(\color{#0066ff}{输出样例}\)
5
9
\(\color{#0066ff}{数据范围与提示}\)
none
\(\color{#0066ff}{ 题解 }\)
本质不同字串???
这不就是自动机上所有节点维护的所有串吗
作为一个最简自动机,这才是真正的板子题吧qwq
\(ans = \sum{len[o] - len[fa[o]]}\)
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
char ch; int x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 1e5 + 5;
struct SAM {
protected:
struct node {
node *ch[26], *fa;
int len, siz;
node(int len = 0, int siz = 0): fa(NULL), len(len), siz(siz) {
memset(ch, 0, sizeof ch);
}
};
node *root, *tail, *lst;
node pool[maxn];
node *extend(int c) {
node *o = new(tail++) node(lst->len + 1, 1), *v = lst;
for(; v && !v->ch[c]; v = v->fa) v->ch[c] = o;
if(!v) o->fa = root;
else if(v->len + 1 == v->ch[c]->len) o->fa = v->ch[c];
else {
node *n = new(tail++) node(v->len + 1), *d = v->ch[c];
std::copy(d->ch, d->ch + 26, n->ch);
n->fa = d->fa, d->fa = o->fa = n;
for(; v && v->ch[c] == d; v = v->fa) v->ch[c] = n;
}
return lst = o;
}
public:
void clr() {
tail = pool;
root = lst = new(tail++) node();
}
SAM() { clr(); }
LL ins(char *s) {
LL ans = 0;
for(char *p = s; *p; p++) {
node *o = extend(*p - 'a');
ans += o->len - o->fa->len;
}
return ans;
}
}sam;
char s[maxn];
int main() {
for(int T = in(); T --> 0;) {
scanf("%s", s);
printf("%lld\n", sam.ins(s));
sam.clr();
}
return 0;
}
SP705 SUBST1 - New Distinct Substrings的更多相关文章
- SPOJ - SUBST1 New Distinct Substrings —— 后缀数组 单个字符串的子串个数
题目链接:https://vjudge.net/problem/SPOJ-SUBST1 SUBST1 - New Distinct Substrings #suffix-array-8 Given a ...
- 后缀数组:SPOJ SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- 【刷题】SPOJ 705 SUBST1 - New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- Spoj SUBST1 New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- SPOJ705 SUBST1 - New Distinct Substrings(后缀数组)
给一个字符串求有多少个不相同子串. 每一个子串一定都是某一个后缀的前缀.由此可以推断出总共有(1+n)*n/2个子串,那么下面的任务就是找这些子串中重复的子串. 在后缀数组中后缀都是排完序的,从sa[ ...
- spoj SUBST1 - New Distinct Substrings【SAM||SA】
SAM里的转台不会有重复串,所以答案就是每个right集合所代表的串个数的和 #include<iostream> #include<cstdio> #include<c ...
- SPOJ SUBST1 New Distinct Substrings(后缀数组 本质不同子串个数)题解
题意: 问给定串有多少本质不同的子串? 思路: 子串必是某一后缀的前缀,假如是某一后缀\(sa[k]\),那么会有\(n - sa[k] + 1\)个前缀,但是其中有\(height[k]\)个和上一 ...
- SPOJ-New Distinct Substrings,注意会爆int
SUBST1 - New Distinct Substrings 和上一题题意一样,只是数据范围有所改动,50000. 思路还是和上一题一样,所有字串数(len+1)*len/2.注意这里可能爆int ...
- SPOJ 题目705 New Distinct Substrings(后缀数组,求不同的子串个数)
SUBST1 - New Distinct Substrings no tags Given a string, we need to find the total number of its di ...
随机推荐
- 2015.1.31 DataGridView自动滚动到某行
方法一.dv.CurrentCell = dv.Rows[i].Cells[2] 但此cell不能是隐藏cell 方法二. if (dgr.Index < dv_sel_aw.FirstDisp ...
- java反射专题一
一丶Class的理解 /* * Class类是反射的源头 * 创建一个类,通过编译(javac.exe),生成对应的.class文件,之后使用java.exe加载(JVM的类加载器完成的)此.clas ...
- Log4j 记录error 日志
第一个bug的起始,是在线上日志发现一个频繁打印的异常——java.lang.ArrayIndexOutOfBoundsException.但是却没有堆栈,只有一行一行的ArrayIndexOutOf ...
- IDEA创建的Maven项目中 解决编写pom.xml没有提示
问题如下 没有提示信息 解决方案 把Repositories中的配置更新成本地仓库 问题解决
- 关于handler和异步任务
handler使用流程概要 首先在主线程新建一个handler实例,重写onhandlemessage(Message msg) 方法,对传过来的message进行处理 然后在子线程中完成操作,操作完 ...
- Express响应方法
下表中响应对象(res)的方法向客户端返回响应,终结请求响应的循环.如果在路由句柄中一个方法也不调用,来自客户端的请求会一直挂起. 方法 描述 res.download() 提示下载文件. res.e ...
- Centos彻底完全删除已安装软件的办法
1.查询是否安装了软件 rpm -qa | grep -i 软件名 rpm -qa | grep php 2.删除已安装的软件包 根据第一步显示的软件包名,一个个删除 sudo rpm -e -- 包 ...
- Bootstrap 的 Tooltip 和 Popover
简介 Tooltip 指提示框,Popover 指弹出框. Tooltip 默认 Tooltip 功能是关闭的,使用前要手动开启. $(function () { $('[data-toggle=&q ...
- 《Effective Java》第4章 类和接口
第13条:使类和成员的可访问性最小化 第一规则很简单:尽可能地使每个类或者成员不被外界访问.换句话说.应该使用与你正在编写的软件的对应功能相一致的.尽可能最小的访问级别. 对于顶层的(非嵌套的)类和接 ...
- Java多线程并发学习-进阶大纲
1.synchronized 的实现原理以及锁优化? 2.volatile 的实现原理? 3.Java 的信号灯? 4.synchronized 在静态方法和普通方法的区别? 5.怎么实现所有线程在等 ...