hihoCoder_1445_后缀自动机二·重复旋律5
#1445 : 后缀自动机二·重复旋律5
描述
小Hi平时的一大兴趣爱好就是演奏钢琴。我们知道一个音乐旋律被表示为一段数构成的数列。
现在小Hi想知道一部作品中出现了多少不同的旋律?
输入
共一行,包含一个由小写字母构成的字符串。字符串长度不超过 1000000。
输出
一行一个整数,表示答案。
- 样例输入
-
aab
- 样例输出
- 5
- 后缀自动机中节点表示的后缀是连续的几个后缀
- 这里连续的意味着以相同节点结尾的几个后缀的长度是连续的比如长度分别是3和4和5
- 而当前节点的最短后缀长度是par指向节点的len+1
- 因此当前节点所代表所有后缀的个数是以当前节点结尾的后缀的最大长度减最小长度加一
- 而一般后缀自动机中对于当前节点的minlen是不用存储的,只要访问par节点的len即可,而后缀自动机中的len一般指maxlen,par和link都是同样一个意思
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e6 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; struct SAM{
int n, root, last, tot;
struct node{
int len;
int link, go[];
};
node state[maxn<<];
void init(char *str){
n=strlen(str);
tot=;
root=;
last=;
memset(&state,,sizeof(state));
}
void extend(int w){
tot++;
int p=last;
int np=tot;
state[np].len=state[p].len+;
while(p && state[p].go[w]==){
state[p].go[w]=np;
p=state[p].link;
}
if(p==){
state[np].link=root;
}else{
int q=state[p].go[w];
if(state[p].len+==state[q].len){
state[np].link=q;
}else{
tot++;
int nq=tot;
state[nq].len=state[p].len+;
memcpy(state[nq].go,state[q].go,sizeof(state[q].go));
state[nq].link=state[q].link;
state[q].link=nq;
state[np].link=nq;
while(p && state[p].go[w]==q){
state[p].go[w]=nq;
p=state[p].link;
}
}
}
last=np;
}
void build(char *str){
init(str);
for(int i=;i<n;i++)
extend(str[i]-'a');
}
LL substring_num(void){
LL ans=0LL;
for(int i=root;i<=tot;i++)
ans+=state[i].len-state[state[i].link].len;
return ans;
}
};
SAM A;
char str[maxn];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%s",str)){
A.build(str);
printf("%lld\n",A.substring_num());
}
return ;
}
hihoCoder_1445_后缀自动机二·重复旋律5的更多相关文章
- hihoCoder #1445 : 后缀自动机二·重复旋律5
#1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...
- hiho一下第128周 后缀自动机二·重复旋律5
#1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...
- hiho一下第131周 后缀自动机二·重复旋律8(循环相似子串)
后缀自动机五·重复旋律8 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 小Hi ...
- hiho一下第130周 后缀自动机二·重复旋律7
后缀自动机四·重复旋律7 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 神奇的 ...
- hiho一下第129周 后缀自动机二·重复旋律6
后缀自动机三·重复旋律6 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数列. 现在小Hi ...
- hihocoder1445 后缀自动机二·重复旋律5
传送门:http://hihocoder.com/problemset/problem/1445 [题解] 大概看了一天的后缀自动机,总算懂了一些 这篇文章写的非常好,诚意安利:Suffix Auto ...
- 【后缀自动机】hihocoder1445 后缀自动机二·重复旋律5
解题方法提示 小Hi:本周的题目其实就是给定一个字符串S,要求出S的所有不同子串的数目.小Ho你知道如何快速求解么? 小Ho:我们最近在讨论后缀自动机,所以肯定是和后缀自动机有关!根据上周学习的SAM ...
- hihocoder 后缀自动机二·重复旋律5
求不同子串个数 裸的后缀自动机 #include<cstring> #include<cmath> #include<iostream> #include<a ...
- HihoCoder1445 后缀自动机二·重复旋律5(后缀自动机 子串种数)
题意: 询问串的不同子串个数 思路: 后缀自动机每个节点表示以当前字符结尾的一系列后缀,个数为\(maxlen - minlen\),其中\(minlen = maxlen[father]\). 代码 ...
随机推荐
- e591. Drawing Simple Text
See also e575 The Quintessential Drawing Program. public void paint(Graphics g) { // Set the desired ...
- QTableWidget的表头颜色设置
设置水平和垂直表头的颜色ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{b ...
- Unity简介
Unity (也称 nity3D) 是一套包括图形. 声音.物理等功能的游戏引擎,提供了一个强大的关卡编辑器,支持大部分主流 3D 软件格式,使用 C# JavaScript 等高级语言实现脚本功能, ...
- Onject.Instantiate实例
该函数有两个函数原型: Object Instantiate(Object original,Vector3 position,Quaternion rotation); Onject Instant ...
- 通过json传递图片(base64编码)
程序一: 后台代码: public ActionResult Index() { FileStream fs = new FileStream("e:\\file\\psb.jpg" ...
- php如何解决多线程同时读写一个文件的问题
<?php header("content-type:text/html;charset=utf-8"); $fp = fopen("lock.txt", ...
- POI-根据Cell获取对应的String类型值
/** * 根据不同情况获取Java类型值 * <ul><li>空白类型<ul><li>返回空字符串</li></ul>< ...
- oracle中如何把结果集插入临时表中
比如临时表叫temp,你要查询的语句为select * from 表名 where id=1. 1.建表temp 2.插入语句: ; commit;
- motion移植
一. 支持ffmpeg功能(使能motion中的视频编码功能)支持视频采集 —> ffmpeg不支持 —host 1. mkdir _install 2. ./configure —pref ...
- Java精选笔记_IO流(转换流、常用流、流操作规律、字符编码)
IO流 用来处理设备之间的数据传输,java对数据的操作是通过流的方式,java用于操作流的对象都在IO包中 按操作数据分为:字节流和字符流:按流向分为:输入流和输出流. 程序从输入流中读取数据,向输 ...