洛谷P3804 【模板】后缀自动机 (SAM) (后缀自动机)

手把手教你用Apple Developer App完成苹果开发者账号注册(含证件验证失败解决方案) 本文详细指导如何通过Apple Developer App完成苹果开发者账号注册,特别针对证件验证失败问题提供解决方案。从准备工作到注册流程,再到验证失败的处理方法,帮助开发者顺利完成注册,避免常见错误。 阅读详情

题目

给定一个只包含小写字母的字符串S(|S|<=1e6),

请你求出 S 的所有出现次数不为 1 的子串的出现次数乘上该子串长度的最大值。

思路来源

https://www.cnblogs.com/cjyyb/p/8446349.html

题解

一个串的出现次数等于endpos集合的大小,

叶子结点的出现次数显然为1,因为在树上不可再分

而一个节点endpos的大小等于所有parent树上儿子的大小

考虑到,父亲的串一定比儿子的串短,所以对长度基数排拓扑序,

短的排在前面,长的在后面,然后从长的向短的(树的底层向上层)更新,

这样就不用建parent树了,每个节点的size是其right/endpos集合的大小

对于每个size>1的节点,ans=max(longest∗size)

心得

size的大小可以单独拿出来考虑,

如果放在node里,在第三种情况nq复制节点的时候要将sz强制清零,

因为只有叶子节点的大小是明确的1,

nq产生时,是一个分支节点,需要从后续儿子节点里更新获取sz

 

代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
struct SAM{
    struct NODE
    {
        int ch[26];
        int len,fa,sz;
        NODE(){memset(ch,0,sizeof(ch));len=sz=0;}
    }dian[N<<1];
    int las=1,tot=1,len;//rt为1 代表空串
    char s[N];
    void add(int c)
    {
        int p=las;int np=las=++tot;
        dian[np].sz=1;
        dian[np].len=dian[p].len+1;
        for(;p&&!dian[p].ch[c];p=dian[p].fa)dian[p].ch[c]=np;
        if(!p)dian[np].fa=1;//以上为case 1
        else
        {
            int q=dian[p].ch[c];
            if(dian[q].len==dian[p].len+1)dian[np].fa=q;//以上为case 2
            else
            {
                int nq=++tot;dian[nq]=dian[q];
                dian[nq].sz=0;
                dian[nq].len=dian[p].len+1;
                dian[q].fa=dian[np].fa=nq;
                for(;p&&dian[p].ch[c]==q;p=dian[p].fa)dian[p].ch[c]=nq;//以上为case 3
            }
        }
    }
    void init()
    {
        scanf("%s",s);len=strlen(s);
        for(int i=0;i<len;i++)add(s[i]-'a');
    }
}sam;
int c[N<<1],a[N<<1];//c用于基数排序 a用于记录点号
ll ans;
int main()
{
    sam.init();
    for(int i=1;i<=sam.tot;++i)c[sam.dian[i].len]++;
    for(int i=1;i<=sam.tot;++i)c[i]+=c[i-1];
    for(int i=1;i<=sam.tot;++i)a[c[sam.dian[i].len]--]=i;
    for(int i=sam.tot;i>=1;--i){
        int now=a[i],par=sam.dian[now].fa;
        sam.dian[par].sz+=sam.dian[now].sz;
        if(sam.dian[now].sz>1){
            ans=max(ans,1ll*sam.dian[now].sz*sam.dian[now].len);
        }
    }
    printf("%lld\n",ans);
    return 0;
}

 

基于Simulink的永磁同步电机容错控制(缺相容错) 本文基于Simulink平台,详细介绍了永磁同步电机(PMSM)缺相容错控制的仿真实现方法。针对逆变器开路故障导致的缺相问题,提出"检测-重构-补偿"三阶段控制策略:采用电流阈值法快速检测故障(<5ms),重构αβ坐标系实现两相矢量控制,并通过谐波电流注入抑制转矩脉动(<8%)。仿真结果表明,该方法能有效维持故障后电机运行,转速波动<3%,满足电动汽车等关键应用的可靠性需求。文章包含完整的Simulink建模步骤、MATLAB函数代码及参数设置,为工程实践提供了可复现的 阅读详情

相关推荐

VHDL按键消抖(状态机版本)

检测下降沿:设置一个和,分别代表之前的之前和之前的信号状态,若两个信号key_in_d2=1,key_in_d1=0,他俩的not key_in_d2 and key_in_d1为1,则说明下降沿产生,把它设置为标志位key_flag。延时计数:50MHz的板子每次计数需要20ns,而抖动时间大约为5-10ms。所以取7ms,作为延时时间。可以看见当检测到下降沿时,开始计数计数计满再次检测,若为低电平则为按下按键,否则为抖动。通过检测下降沿来临,延时计数再次检测,如果为低电平则为按键按下。

qq_69639216的博客 1086

【luogu P3804】【模板】后缀自动机 (SAM)

给你一个字符串,求出它出现次数超过 1 的子串乘它长度的最大值。 以及 SAM 的详细讲解,常见用法。

欢迎您的光顾awa 516

Flink SQL数据血缘实战:如何用Calcite追踪字段级数据流向(附源码示例)

本文深入探讨了如何利用Apache Calcite解析Flink SQL执行计划,实现字段级数据血缘的追踪。通过扩展Calcite的元数据处理器,特别是针对Flink特有的Snapshot等节点,文章提供了从获取优化后的逻辑计划到提取并可视化血缘关系的完整实战方案,并附有核心源码示例,帮助开发者构建实时数仓的数据治理能力。

weixin_29032337的博客 144

后缀自动机(SAM)讲解 + Luogu p3804【模板】后缀自动机 (SAM)

后缀自动机 SAM

Jacob0824的博客 3052

音视频从入门到精通——指数哥伦布码原理

在计算机中,一般数字的编码都为二进制,但是由于以相等长度来记录不同数字,因此会出现很多的。

mirana的专栏 1388

YonSuite如何新增用户并分配角色角色授权图文教程

用友YonSuite产品基础教学

ct341688的博客 630

P3804 【模板】后缀自动机 (SAM)

P3804 【模板】后缀自动机 (SAM)

qq_61305213的博客 294

Label Studio + SAM2分割半自动标注与yolo11训练

Label Studio + SAM2分割半自动标注与yolo11训练

schFallingCloud 1417

【后缀自动机模板 | 洛谷P3804】后缀自动机 SAM

模板题 本文只是放上后缀自动机的模板,并不会对后缀自动机的原理进行讲解。 首先放上模板: #include <bits/stdc++.h> #define sc(n) scanf("%d",&n) #define pt(n) printf("%d\n",n) #define rep(i,a,b) for(int i=a;i<=b;i++) #define vi vecto...

KIKO_caoyue的博客 430

SAP UI5 百科全书的目录

笔者 12 年 SAP UI5 开发岁月中写下的 595 篇 SAP UI5 学习笔记,全部收录到《SAP UI5 百科全书》专栏中去,目录如下。请大家按照自己的学习需要,根据关键字搜索到对应的文章,然后进行学习。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 3852

【毕设实战】飞行器目标识别系统:基于YOLOv5的智能检测全流程教程,助你毕设脱颖而出

亲爱的同学,毕设是不是让你既期待创新又担心技术门槛?如果你的毕设方向是计算机视觉、智能检测,那这个绝对能成为你的“毕设王牌”。从数据准备到界面开发,我会把整个项目拆成“傻瓜式”步骤,让你能一步步做出属于自己的毕设成果。

know_F0727的博客 109

如何使用csdn下载文档?

如何使用csdn下载文档? 新人求助。如何不买那么多的月卡年卡下载文档?

weixin_43045092的博客 786

万字长文带你由浅入深夯实ARM汇编基础——汇编指令及寻址方式最全梳理(附示例)!

见到汇编就害怕?读完这篇,扫除汇编拦路虎!!(示例丰富,可做手册,随手查阅!)

车载系统攻城狮 1万+

洛谷 P3804 【模板】后缀自动机 (SAM)(后缀自动机)

后缀自动机 (SAM)

qq_38232157的博客 527

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

[洛谷] (P3804)【模板】后缀自动机 ---- SAM(模板题)

题目传送门 做法: 考察SAM的基本概念,即各个状态的endpos集合的大小怎么求? 我们从parent树上自底向上跑,类似树的遍历(即回溯过程),去统计集合的大小即可。 AC代码: #include&lt;bits/stdc++.h&gt; #define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #def...

WangMeow --- ᶘ ᵒᴥᵒᶅ ฅ^•ﻌ•^ฅ 368

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

后缀自动机(SAM) 学习笔记

最近学了SAM已经SAM的比较简单的应用,SAM确实不好理解呀,记录一下。 这里提一下后缀自动机比较重要的性质: 1,SAM的点数和边数都是O(n)级别的,但是空间开两倍。 2,SAM每个结点代表一个endpos,每个endpos有可能代表多个字串(当然这些字串的endpos相等),且这些字串的长度呈一个梯形。 3,令tree[x].len为点x代表的所有字串中长度最长的,tre...

weixin_30508241的博客 276

洛谷 P3804 【模板】后缀自动机 统计单词出现次数

洛谷 P3804 【模板】后缀自动机 统计单词出现次数 后缀自动机模板题. 关键时求解每个节点的 $right$ 大小. 由于后缀自动机在构建时会保证点和点的 $right$ 只可能没有交集,或者一个是另一个的真子集,我们可以不重复的对 $right$ 进行统计与更新. 从长度大的子串向前更新,沿着 $parent$ 跳即可. 最后再枚举一下. C...

EM LGH 414

洛谷 P3804 [模板] 后缀自动机

题目:https://www.luogu.org/problemnew/show/P3804 模仿了一篇题解,感觉很好写啊。 代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; ...

aodan5477的博客 167

洛谷P3804 - 【模板】后缀自动机

Portal Description 模板题啦~ 推荐WJMZBMR在WC2012上的讲义《后缀自动机》。 Code //【模板】后缀自动机 #include <cstdio> int max(int x,int y) {return x>y?x:y;} int const N=2e6+10; int n; char s[N]; int rt,ndCnt,last; i...

TC1129的博客 145

【题解】洛谷P3804 【模板】后缀自动机

后缀自动机真是魔法 。 ——小秋自己说的。 给定一个只包含小写字母的字符串S,请你选择S的一个出现次数不为1的子串,使它的出现次数乘以长度最大,输出这个最大值。 后缀自动机的每个节点(状态)等价于一组长度连续的子串,以及一个endpos集,endpos集的大小也就是这些子串的出现次数。 如果已知每个节点的endpos集大小,可以通过遍历节点,枚举最大的长度*出现次数。 如何知道endpos集...

秋叶红于二月花 372

洛谷P3804【模板】后缀自动机

4. 洛谷P3804【模板】后缀自动机 题意:求字符串 S 中所有出现次数不为 1 的子串的出现次数乘上该子串长度的最大值。 方法:建立SAM,令叶子节点 size 等于 1。按照拓扑序从下往上计数。个数大于1的就累计结果。 #include &lt;cstdio&gt; #include &lt;cstring&gt; #include &lt;algorithm&gt; using na...

生活不易,多才多艺 432

【详细注释 | 字符串算法集合 2】后缀自动机 SAM & 后缀数组 SA

难,但我讲的很好。

Isaac Regulus的博客 1179

洛谷P3804(SAM)

题目链接:https://www.luogu.org/problem/P3804 思路:sam模板 #include <cstdio> #include <cstdlib> #include <cstring> #include <bitset> #include <cmath> #include <cctype> #i...

llmxby 422

后缀自动机实际应用

上一篇我们具体介绍了后缀自动机的构造方式,但并没有谈它的应用,那么我们在这一篇里详细谈一谈后缀自动机的应用 首先,后缀自动机的性质:从根节点开始可以识别一个字符串的所有子串 接下来,我们需要给出一个定义: 将后缀自动机上的pre指针反指,会得到一棵树形结构,我们把这棵树叫做parent树! parent树是原串反串的后缀树! 记住这个定义,因为后缀自动机中大部分题都与这棵树有千丝万缕的联...

lleozhang的博客 993

洛谷 P3804 【模板】后缀自动机

题目描述 给定一个只包含小写字母的字符串 SSS , 请你求出 SSS 的所有出现次数不为 111 的子串的出现次数乘上该子串长度的最大值。 输入输出格式 输入格式: 一行一个仅包含小写字母的字符串 SSS 输出格式: 一个整数,为 所求答案 输入输出样例 输入样例#1: abab 输出样例#1: 4 说明 对于 10%10%10\% 的数据, |S|&amp;amp;lt;=10...

liangzihao1的博客 342
上一篇: 后缀自动机(知识整理+板子总结)
下一篇: 洛谷P2408 不同子串个数(后缀自动机)
小衣同学
博客等级 码龄9年 1444粉丝 995原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小衣同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址