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 ...
随机推荐
- 转:面对一个全新的环境,作为一个Oracle DBA,首先应该了解什么
转载:面对一个全新的环境,作为一个Oracle DBA,首先应该了解什么 由于时间比较久了,找不到原文章的链接,先贴上内容供大家参考 在这里,不谈那些大的方面,比如了解整个IT环境整体情况,假设你已经 ...
- 环形缓冲区的应用ringbuffer
在嵌入式开发中离不开设备通信,而在通信中稳定性最高的莫过于环形缓冲区算法, 当读取速度大于写入速度时,在环形缓冲区的支持下不会丢掉任何一个字节(硬件问题除外). 在通信程序中,经常使用环形缓冲区作为数 ...
- Python-使用unrar库时Couldn't find path to unrar library的解决办法
在Pycharm安装完unrar后,还要安装rar官方的库 不然运行的时候会抛出Couldn't find path to unrar library的错误 Windows: 下载rarlib的库文件 ...
- Solaris10技巧
如何查看UFS文件系统创建命令 root@ofs0accmcc01 # mkfs -m /dev/md/rdsk/d100 mkfs -F ufs -o nsect=128,ntrack=48,bsi ...
- node install webpack -cli webpack4.xxxx
webpack 4.xx 版本 分离了 webpack-cli ; 安装webpack4.xx 需要再安装webpack-cli;
- 问题:oracle 排序 null值放在最后;结果: ORACLE中null的排序问题
ORACLE中null的排序问题 关键字: oracle nulls 问题描述: 在平时的业务处理中,经常遇到要对业务数据进行排序,并且要对null值也做相应的排序.在Oracle中,进行Ord ...
- Shell编程进阶 2.1 shell函数
函数 vim fun.sh #!/bin/bash function mysum() { sum=$[$+$] echo $sum } a= b= mysum $a $b sh fun.sh 6 可以 ...
- DAY4-函数进阶
目录: 一.迭代器 二.生成器 三.面向过程编程 四.三元表达式.列表推导式.生成器表达式 五.第归与二分法 六.匿名函数 七.内置函数 练习 一.迭代器 一.迭代的概念 #迭代器即迭代的工具,那什么 ...
- PTA 估值一亿的AI核心代码
题面 比赛时被模拟题打自闭了,本来以为是个比较麻烦的模拟,实际上只要会C++的regex不到40行就能把这个题过掉了(orz smz) regex是用来处理正则表达式,里面有个函数regex_repl ...
- c++的单例模式及c++11对单例模式的优化
单例模式 单例模式,可以说设计模式中最常应用的一种模式了,据说也是面试官最喜欢的题目.但是如果没有学过设计模式的人,可能不会想到要去应用单例模式,面对单例模式适用的情况,可能会优先考虑使用全局或者静态 ...