dp+高精度 uva-10069-Distinct Subsequences

[递推dp] zoj 3747 Attack on Titans 题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5170 Attack on Titans Time Limit: 2 Seconds      Memory Limit: 65536 KB Over centuries ago, mankind faced a new enemy, th 阅读详情

 

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1010

 

题目大意:

给两个字符串A,B,求B串在A串中出现的个数。

 

解题思路:

dp+高精度。

dp[i][j][k]表示串B的前i个在串A的前j个中出现的个数,k表示位数,每一位表示4位十进制的数。

如果save2[i]==save1[j]  则dp[i][j]=dp[i][j-1]+dp[i-1][j-1]; 注意处理初始化的问题。

如果save2[i]!=save1[j]   则dp[i][j]=dp[i][j-1];

高精度:每一位表示4位十进制的数,如果一位一位的相加的话会超时。

 

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#define eps 1e-6
#define INF (1<<20)
#define PI acos(-1.0)
using namespace std;

char save1[11000],save2[120];
int dp[120][11000][30];   //dp[i][j]表示Zi在Aj中一共有多少个  一位表示4位10000


void add(int i,int j)
{
    int i1=i,j1=j-1,i2=i-1,j2=j-1;

    for(int k=1;k<=30;k++)
    {
        dp[i][j][k+1]+=(dp[i1][j1][k]+dp[i2][j2][k])/10000; //进位
        dp[i][j][k]+=(dp[i1][j1][k]+dp[i2][j2][k])%10000;
    }
    return ;
}

int main()
{
    int ca;

    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%s%s",save1,save2);

        int len1=strlen(save1),len2=strlen(save2);

        memset(dp,0,sizeof(dp));

        for(int j=0;j<=len1;j++) //注意初始化,用于构建第一种情况
            dp[0][j][1]=1;

        for(int i=1;i<=len2;i++)
            for(int j=1;j<=len1;j++)
            {
                if(save2[i-1]==save1[j-1])
                {
                    //dp[i][j]=dp[i][j-1]+dp[i-1][j-1]; //可以从dp[i][j-1]继承过来,也可以新增dp[i-1][j-1]个
                    add(i,j);
                }
                else
                {
                    //dp[i][j]=dp[i][j-1];
                    for(int k=1;k<=30;k++)  //直接复制
                        dp[i][j][k]=dp[i][j-1][k];

                }
            }
        //printf("%lld\n",dp[len2][len1]);
        int i;
        for(i=30;dp[len2][len1][i]==0&&i>=1;i--)  //除掉前置零
            ;
        printf("%d",dp[len2][len1][i]); //把第一个处理,不用4位处理。
        i--;
        for(;i>=1;i--)
            printf("%04d",dp[len2][len1][i]); //注意前置零
        putchar('\n');
    }
    return 0;
}



 

 

 

 

递推计数-hdu-4747-Mex 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4747 题目大意: 给n个数,求所有区间内没有出现的最小非负整数和。 解题思路: 首先感谢大神博客:http://www.cnblogs.com/xin-hua/p/3325154.html可以解释的太少了。 我再解释一遍。 首先要明白,以i结束的所有区间的值的和记为f[i]肯定不超 阅读详情

相关推荐

[高精度+递推] uva 10328 Coin Toss

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1269 Toss is an important part of any event. When everything becomes equal toss is the ulti

cc_again的专栏 4678

uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)

题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth 解题思路:二维数组DP, 有类似于求解最长公共子序列, cnt[i][j]表示在x的前j个字符中有多少个z 前i个字符。 状态转移方程   1、x[j] != z[i]              cnt[i][j] = cnt[i][j - 1]; 2、x[j

不慌不忙、不急不躁 1923

区间dp+输出方案 poj-1141-Brackets Sequence

题目链接: http://poj.org/problem?id=1141 题目大意: 给一个由[,],{,}组成的字符串序列,求增加最少的字符,使该序列能够匹配,并输出最后的方案。 解题思路: 区间dp.dp[i][j]表示从i~j 所需的最少的字符使之能匹配,转移的话要么是头尾匹配直接加中间,要么分成两段。 不过要输出到达路径,所以在用一个path[i][j]表示到达该路径时的选

cc_again的专栏 4294

UVA 10069 Distinct Subsequences(dp + 高精度)

Problem E Distinct Subsequences Input: standard input Output: standard output   A subsequence of a given sequence is just the given sequence with some elements (possibly none) left out. Formally,

Remilia's 2185

【GPIO模拟串口通信协议】

由于这段时间搞了个安卓相关的项目,需要和安卓进行Uart通信。但是选用的芯片并没有自带uart功能,所以在充分评估项目后,考虑波特率不高的情况下,决定使用GPIO去模拟串口通信协议了。

学海无涯苦作舟 2638

探索Vortex开源GPGPU:RISC-V SIMT架构(2),指令Decode

本文分析了Vortex RISC-V内核的Decode模块设计。文章详细介绍了指令译码过程,包括操作码、功能码和寄存器字段的提取,以及不同类型指令(R/I/S/B/U/UJ)的立即数处理。重点阐述了基于操作码和功能码的分层译码逻辑,包括ALU运算、分支跳转、系统调用等指令的译码规则。此外,还描述了定制指令的处理机制以及与调度器的握手协议。Decode模块通过组合逻辑实现快速译码,并将结果传递给下级Issue模块,同时协调指令流控制。

weixin_55313207的博客 1793

Distinct Subsequences - UVa 10069 dp+高精度

Distinct Subsequences Input: standard input Output: standard output   A subsequence of a given sequence is just the given sequence with some elements (possibly none) left out. Formally, given a se

提比的强行AC 643

【2022KaliLinux】openvas/gvm 没有scan configs 或者是 scan config error “XXXXXXXXXXX”

gvm/openvas的configuration的scan config为空

zbossz的学习小分队 3607

UVA 10069 Distinct Subsequences(dp+高精度加法)

题意: 给定2个字符串a, b,求b在a中出现的次数。 要求可以是不连续的,但是b在a中的顺序必须和b以前的一致。 思路: 类似于数字分解的题目。 dp[i][j]表示: b的前j个字符在a的前i个字符中出现的次数。 dp[i][j] = dp[i][j-1]; if(b[i] == a[j])     dp[i][j] += dp[i-1][j-1]; 母串:ba

小G的ACM之路 774

样本不均衡时AUC反而下降?用imbalanced-learn库实战解决分类陷阱

本文探讨了样本不均衡时AUC指标下降的现象,并通过imbalanced-learn库实战演示了如何解决这一分类陷阱。文章详细分析了不同采样技术(如SMOTE、ADASYN)对模型性能的影响,并提供了XGBoost和逻辑回归模型的优化建议,帮助数据科学家在实际项目中做出更明智的决策。

weixin_29174013的博客 343

python + QXDM5高通平台通过自动化截取log方法及代码

python + QXDM5高通平台通过自动化截取log方法及代码

dgzj2001的博客 1820

uva10069-Distinct Subsequences

题目链接请戳这里 解题思路 设\(dp[i][j]\)为\(s1[1..i]\)作为\(s2[1..j]\)子串的个数。 这样我们可以写出状态转移方程\[dp[i][j]=\begin{cases} dp[i-1][j-1]+dp[i][j-1] & \text{$s1[i]=s2[j]$} \\ dp[i][j-1] & \text{$s1[i]!=s2[j]$}\e...

anyi4391的博客 185

Vibe Coding 实战指南:用 3 类社区资源实现每周高效追新

介绍一种分层技术雷达校准方法,通过领域过滤器(L1技术栈锚定、L2能力映射、L3场景收敛)精准筛选AI编程相关更新,结合可验证的Shell命令与CI任务卡,将信息过载压缩为可执行、可审计的工程实践。

weixin_27251469的博客 331

uva_10069_Distinct Subsequences (普通DP)

这题是一个普通的dp,lcs变形,这里需要注意的是初始化,和这里需要使用高精度 阶段:以主串的字符顺序作为阶段,从后面枚举 状态:dp[i][j] 表示主串的第i个字符和子串的第j个字符匹配的方案数 dp[i][j+1] (stra[i] != strb[j]) 状态转移:dp[i][j] -> dp[i][j+1]

unixcsir的专栏 384

基于MATLAB的变分模态分解(VMD)算法详解

变分模态分解(VMD)是一种先进的信号处理方法,通过变分框架将复杂信号分解为多个模态分量。相比传统经验模态分解(EMD),VMD具有坚实的数学基础,能有效抑制模态混叠和端点效应。其核心思想是将信号分解转化为约束优化问题,通过交替方向乘子法迭代求解各模态的中心频率和带宽。VMD在机械故障诊断、生物医学信号处理等领域展现出优越性能,尤其适合处理非平稳非线性信号。该算法通过严格的数学模型实现自适应频谱优化,为信号分析提供了更可靠的工具。

qq_42212808的博客 735

构建栅格地图matlab代码

1 使用MATLAB实现地图栅格化 效果图如下所示 图1 ...

qq_59747472的博客 4518

Uva-10069-Distinct Subsequences

简单DP+高精度,后面将高精度优化了一下 代码: #include #include #include using namespace std; const int maxn=10010; char stra[maxn],strb[110]; int lena,lenb,dp[110][maxn][21]; void Add(int *ans,int *a,int *b) { for(

z309241990的专栏 841

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

UVA 10069 Distinct Subsequences

http://uva.onlinejudge.org/external/100/10069.pdf 两个字符串,求下一个再上一个之中出现的次数。 如果a[i] == b[j]则 dp[i][j] = dp[i-1][j] + dp[i-1][j-1] 如果a[i] != b[j]则 dp[i][j] = dp[i-1][j] 这里还涉及到高精度加法,是时候使用C语言的类了。

Occult 444

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

UVa 10069 - Distinct Subsequences

高精度的运算,,所以用了JAVA的大数。 用b表示长串,a表示短串。dp[k][j]表示的是短串的位置k之前的子串在长串位置j之前出现的次数。 所以当str[j] == str[k] dp[k][j] += dp[k - 1][j - 1] 不过在对dp[k][j]进行初始化的时候要特别注意   import java.math.BigDecimal; import java.math.

AcToy 563

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

【DP专辑】ACM动态规划总结

动态规划一直是ACM竞赛中的重点

cc_again的专栏 2万+

[dp] Codeforces 429B B. Working out

题目链接: http://codeforces.com/problemset/problem/429/B PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST B. Working out time limit per test 2 seconds memory li

cc_again的专栏 9715

组合dp hdu-4489-The King’s Ups and Downs

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4489 题目意思: 给一个n,求n个高矮不同的人排成一排使得高、矮依次排列的种数。 解题思路: 组合dp. 对于n个人,设其高度分别为1,2,3,,,,,n.   对于第n个人,假设前面的n-1个人已经放好了。第n个人有n个位置可放,对于任一位置j,显然第n个人的身高n大于前n-1个

cc_again的专栏 4974

计数dp-hdu-4054-Number String

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4055 题目大意: 给一个只含‘I','D','?'三种字符的字符串,I表示当前数字大于前面的数字,D表示当前的数字小于前面一位的数字,?表示当前位既可以小于又可以大于。 问1~n的排列中有多少个满足该字符串。 解题思路: 计数dp. dp[i][j]表示长度为i字符串,最后一个数为

cc_again的专栏 4481
上一篇: 递归 uva-620-Cellular Structure
下一篇: dp uva-825-Walking on the Safe Side
Accagain
博客等级 码龄14年 376粉丝 316原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值