hdu3336-Count the string

hdu3336(kmp)Count the string Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10970    Accepted Submission(s): 5109 Problem Description It is we 阅读详情

Count the string

使用get_next函数多了,渐渐对get_next函数的理解又深了一点点,

这其中dp[i]从1加到n

dp[i]=dp[next[i]]+1;


// File Name: hdu3336.cpp
// Author: bo_jwolf
// Created Time: 2013年04月30日 星期二 20:39:15

#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>

using namespace std;
const int maxn = 200005 ;
int dp[ maxn ] ;
const int MOD = 10007 ;
char str[ maxn ] ;
int next[ maxn ];
int len ;
void get_next()
{
	int j = 0 , k = -1 ;
	next[ 0 ] = -1 ;
	len = strlen( str ) ;
	while( j < len )
	{
		if( k == -1 || str[ j ] == str[ k ] )
		{
			j++ ;
			k++ ;
			next[ j ] = k ; 
		}
		else
			k = next[ k ] ;
	}
}

int main()
{
	int n ;
	int Case ;
	int ans ;
	scanf( "%d" ,&Case );
	while( Case-- )
	{
		scanf( "%d" , &n ) ;
		scanf( "%s" , str );
		get_next();
		dp[ 0 ] = 0 ;
		ans = 0 ;
		for(int i = 1 ; i <= n ; i++ )
		{
			dp[ i ] = dp[ next[ i ] ] + 1 ;
			dp[ i ] = dp[ i ] % MOD ;
			ans += dp[ i ];
			ans = ans % MOD ;
		}
		printf( "%d\n" , ans ) ;
	}
return 0;
}






物流系统集成实战:从零开始用Java对接国际快递(DHL/FedEx/UPS) 本文详细介绍了如何使用Java技术栈对接DHL、FedEx和UPS三大国际快递巨头的API,构建高效、稳定的物流集成解决方案。内容涵盖认证机制、报文标准、多语言支持等关键点,并提供实战代码示例和最佳实践,帮助开发者快速实现国际快递系统集成。 阅读详情

相关推荐

手机摄影新宠:In-line Hardware EIS如何让你的4K视频稳如鸡头(附实测对比)

本文深入解析了手机摄影中的In-line Hardware EIS(硬件级行内电子图像稳定)技术。通过对比传统软件EIS,阐述了其通过硬件管道实时处理实现极低延迟、减少画面裁切和有效抑制果冻效应的核心优势,并结合4K视频的日夜间实测,展示了该技术如何让手持拍摄的视频“稳如鸡头”,显著提升创作体验与成片质量。

tree的博客 1001

HDU - 3336 (kmp+dp)

题意:给你一个串,问你他的所有前缀和他本身匹配的次数mod10007。比如 abab 他的子串有a,ab,aba,abab,他们的匹配次数分别是 2,2,1,1,思路:我们知道nex[i]表示的是你在第i位的时候匹配的最长的相等前后缀的长度(也就是说你在第i位的时候,和你相等的最长相等的前后缀在nex[i]的位置),那么我们就可以定义dp[i]表示的是你在第i位的时候匹配成功的次数,那么dp[i]...

wjmwsgj的博客 263

日前日内多阶段多时间尺度源荷储协调调度(matlab代码)

多阶段多时间尺度的协调调度的优势是考虑新能源出力的波 动性与随机性,需求响应负荷的不确定性会对电网制定的日前调度计划准确性造成影响,也就是能够更加精准的进行调度和分析,优化结果的可用性更强。在这方面论文里面,金力的《考虑特性分布的储能电站接入的电网多时间尺度源储荷协调调度策略》就考虑了日前日内实时三个阶段调度,每个阶段考虑了不同负荷需求响应策略,同时考虑了负荷波动性的影响,具有很强的参考意义。 目录 一、模型 二、程序运行 三、程序链接 一、模型 负荷需求响应考虑电价型和激励型两种,电.

zhangxd212489的博客 7918

HDU(3336)

给出一段字符串s时,我们可以写出该字符串的所有非空前缀。 例如:s: "abab" 前缀是,a, ab, aba, abab 其中 a,ab出现俩次 所以所有非空前缀的总和为=1*2+1*2+1+1=6;

m0_52262443的博客 444

避开新手第一个坑:手把手教你正确安装Perforce服务器与客户端(P4d/P4V)

本文详细指导如何正确安装和配置Perforce服务器与客户端(P4d/P4V),避免新手常见的安装顺序错误。从环境准备到分步安装,再到常见问题排查和性能优化,帮助开发者快速搭建稳定的版本控制环境,提升团队协作效率。

weixin_26804345的博客 681

openlayers6【十九】vue HeatmapLayer热力图层实现热力图效果详解

文章目录1. 写在前面2. Heatmap 类实现热力图2.1 Heatmap 参数2.2 实现热力图3. 完整代码4. 添加删除map图层的方法5. 热力图自身的get,set方法 1. 写在前面 我们主要看热力图的效果,区域绘制效怎么设置详细内容可以访问 openlayers6【十七】vue VectorLayer矢量图层画地图省市区,多省市区(粤港澳大湾区)效果详解 在 openlayers 中,图层是使用 layer 对象表示的,主要有 WebGLPoints Layer、热度图(HeatMap L

@必意玲 1万+

HDU—— 3336 Count the string

题意:给定一个字符串,duiyugaiz

u014086857的专栏 520

Footprint Expert Pro与Allegro协同设计:高效封装库创建实战

本文详细介绍了如何利用Footprint Expert Pro与Allegro协同设计高效创建PCB封装库。通过版本匹配、环境配置、参数输入技巧和导出优化等实战经验,帮助工程师快速生成符合IPC标准的封装,显著提升工作效率,特别适用于BGA、QFN等复杂封装设计。

weixin_29278805的博客 193

hdu3336 Count the string

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1147    Accepted Submission(s): 494 Problem Description It is well kno

夏天的风 1750

统计学习导论(ISLR) 第六章变量选择课后习题

ISLR统计学习导论第六章变量选择课后题

阿里云专家博主、数据科学领域优质创作者、统计er在读 2万+

图像紫边消除(depurple)

对图像紫边形成原因、消除方法进行了介绍。

沧海一升的博客 7571

hdu3336--KMP入门

根据kmp中的next数组的定义,用动态规划可以求得前缀出现的次数题目链接:hdu3336#include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;iostream&gt; using namespace std; int d[200005],n,nextt[200005]; char T[200005]; void getNext(...

onsterm的博客 539

风电光伏功率预测竞赛精华总结:技术解析与实战经验

本文深度解析新能源功率预测竞赛的技术要点与实战经验。首先介绍了风电、光伏功率预测对电网调度优化、降低运营风险的重要性,以及多个竞赛的基本概况。在数据特征分析部分,详细探讨了气象预报数据和功率数据的特征,包括时间周期性、数据质量问题及处理方法。针对异常值检测、数据标准化和特征工程(如时间特征、统计聚合特征、风速风向特征和滞后特征)提供了具体的技术实现方案。文章为新能源功率预测提供了实用的技术指导,对相关竞赛和实际应用具有重要参考价值。

算法工程师 208

HDU 3336 Count the string【KMP的next数组性质】

题目链接:HDU 3336 Count the string Problem Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s , we can write down all the non-empty prefixes of this string. For example: s: "abab". T

memcpy0的博客 447

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

hdu 3336 Count the string(KMP)

题目链接:hdu 3336 Count the string 题目大意:给出一个字符串,要求计算所有前缀串在该字符串中出现次数的总和。 解题思路:首先求出jump数组,然后对于每个位置来说,如果jump[p]!=0的话,即为存在一个前缀串,注意这里p = jump[p], 因为前缀串中含可能包含前缀串。最后要+n。 #include #include con

不慌不忙、不急不躁 1108

深度学习工具包 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")

Count the string_HDU3336

文章目录[Count the string_HDU3336](http://acm.hdu.edu.cn/showproblem.php?pid=3336)题目大意解题思路代码 Count the string_HDU3336 题目大意 一共有T组字符串,每组有一个长度为n的字符串。让你求这个字符串的每一个前缀串在这个字符串中出现的次数和是多少。 解题思路 我们对这个字符串进行KMP求出next数组,我们会发现,next[i] 表示的是,公共前缀子串的长度,进而对next数组的值作为线性DP的下标进行求解就

qq_43750980的博客 302

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

HDU3336Count the string

HDU3336Count the string Problem Description It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty p...

weixin_30500473的博客 75

HDU 3336 Count the string(KMP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 解题思路: 先把原数组构造失配数组 fail[i] 表示0~i-1最长相同前后缀 首先我们先令ans = len,也就是先把每个前缀本身加上去 我们可以保证在fail数组所得到的后缀不会与这些重合,不然那叫前缀。 接下去遍历fail[2]~fail[len] 对于每个fail我们求...

shadiao.的博客 198

[HDU 3336] Count the string

题目传送-HDU3336 题意: 给你一个长度为\(n\)的字符串\(S\),问这个字符串的所有前缀在字符串中出现的次数之和(mod \(10007\)) \(n\le 200000\) 题解: 1.考虑KMP,并记录\(f_i\)表示以\(i\)为结尾的所有串中是前缀的个数,那么答案显然是所有\(f\)之和 根据KMP中nxt数组的定义,\(f_i=f_{nxt_i}+1\)(如果这里...

A4401780916的博客 117

HDU 3336 Count the string

Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description It is well known that AekdyCoin is good at string problems as well as number th...

忘梦心的博客 272

HDU 3336 Count The String

K - Count the string                                 HDU - 3336题意:例如abab这样的字符串的子字符串看为有a,ab,aba,abab四种,就是从第一个开始,每次加一个元素,也就是所有的非空前缀,其中a,ab在原字符串中出现两次,aba,abab各出现一次,所以一共六次,结果要%10007 对,每次都是从0开始更新,很慢分析一下这个a...

STOP 682

HDU 3336 Count the string kmp

一、内容 It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: s: ...

Tizzi的博客 787

HDU 3336 Count the string 另解

嗯 看别人的方法千篇一律(都是 kmp) 想个另类方法解决 其实本质上差不多 Orz HDU 3336 是个前缀问题 并且有个规律 --------- 下一位可能的情况 只有在这一位可能的基础上才有可能 比如 abcabcaca 先把 四个a入链表 对于 ab 只可能 是 上一位是 a 的情况下才有可能 所以 把第三和第四个a 出链表 此时 链表中 还剩 2个元素 当c入队时在继续筛选 这样子快...

qq_45770909的博客 160

Count the string HDU - 3336

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:  s: "abab...

cpx17852033609的博客 259

hdu 3336 Count the string

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 分析:求字符串中所有前缀的匹配次数,先求出next数组,从n->1开始枚举计数。 /*Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot...

weixin_30674525的博客 98

HDU3336 Count the string

题目链接:HDU3336 Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7642    Accepted Submission(s): 3553 Problem Descri

teddywang 1029
上一篇: hdu1711-Number Sequence
下一篇: KMP算法
「已注销」
博客等级 码龄14年 73粉丝 565原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值