题目链接:https://vjudge.net/contest/216347#problem/C

Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A' and sequence B' are same. For example, {1,2} and {1,2} are same. {1,2,4} and {1,4,2} are not same. A' is a subsequence of A. B' is a subsequence of B. The subsequnce can be not continuous. For example, {1,1,2} has 7 subsequences {1},{1},{2},{1,1},{1,2},{1,2},{1,1,2}. The answer can be very large. Output the answer mod 1000000007.

InputThe input contains multiple test cases.

For each test case, the first line cantains two integers N,M(1≤N,M≤1000)N,M(1≤N,M≤1000). The next line contains N integers. The next line followed M integers. All integers are between 1 and 1000.OutputFor each test case, output the answer mod 1000000007.Sample Input

3 2
1 2 3
2 1
3 2
1 2 3
1 2

Sample Output

2
3

题目大意:给你两个数组,要你求出这两个数组有多少个公共子串
个人思路:说是简单dp,但是要是想不出来就很不简单了,不说废话了,这题要求出状态转移方程,方法是用面积来求
如图,当a[i]!=b[j]时,dp[i][j]=dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1],因为多加了一个dp[i-1][j-1]的面积
当a[i]==b[j]时,a[i]等于b[j]本身构成一个相等子串,dp[i-1][j-1]加上a[i],b[i]也构成新的子串,所以在不想等的情况下加上这两个条件
dp[i][j]=dp[i-1][j]+dp[i][j-1]+1
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
typedef long long ll;
using namespace std;
const ll INF=1e9+7;
//#define INF 1e9+7
ll dp[1100][1100];
int main()
{ll n,m;ll a[1100],b[1100];while(scanf("%I64d%I64d",&n,&m)!=EOF){memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++){scanf("%I64d",&a[i]);}for(int i=1;i<=m;i++)scanf("%I64d",&b[i]);for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(a[i]!=b[j]){dp[i][j]=(dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1]+INF)%INF;//注意括号里面可能是负数,不加上会wa}else{dp[i][j]=(dp[i][j-1]+dp[i-1][j]+1)%INF;}}}printf("%I64d\n",dp[n][m]);}return 0;
}

  

题目链接:https://vjudge.net/contest/231313#problem/E

We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices. For each device, we are free to choose from several manufacturers. Same devices from two manufacturers differ in their maximum bandwidths and prices.
By overall bandwidth (B) we mean the minimum of the bandwidths of the chosen devices in the communication system and the total price (P) is the sum of the prices of all chosen devices. Our goal is to choose a manufacturer for each device to maximize B/P.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. Each test case starts with a line containing a single integer n (1 ≤ n ≤ 100), the number of devices in the communication system, followed by n lines in the following format: the i-th line (1 ≤ i ≤ n) starts with mi (1 ≤ mi ≤ 100), the number of manufacturers for the i-th device, followed by mi pairs of positive integers in the same line, each indicating the bandwidth and the price of the device respectively, corresponding to a manufacturer.

Output

Your program should produce a single line for each test case containing a single number which is the maximum possible B/P for the test case. Round the numbers in the output to 3 digits after decimal point.

Sample Input

1 3
3 100 25 150 35 80 25
2 120 80 155 40
2 100 100 120 110

Sample Output

0.649题目大意:输入t,代表t组测试样例,输入n,代表有n 个设备,接下来n行,每行有一个num,代表有num个厂家,接着有num对数,分别代表b,p。每个设备选择一个厂家,最后求出b与sum的最大比值,b是你选择的厂家里最小的b,sum是你选择的p的总和。个人思路:最先想到dfs,直接暴力搜索,没想到数据这么小也超时了,然后猜想只能用dp来写了,因为不能剪枝,感觉自己对dp还是不熟,感觉要写出来了却还是差一点,最后搜了题解,看懂了ac代码如下
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
#define INF 0x3f3f3f
int dp[110][20000];//dp[i][j]代表从第1~i个厂家使得带宽为j的最小价格总和,因为题目没有说带宽的范围,我们假设最大20000
int main()
{int t;scanf("%d",&t);while(t--){int n;double ans=0;scanf("%d",&n);for(int i=1;i<=n;i++){for(int j=0;j<=2000;j++)dp[i][j]=INF;//先把每一个都初始化为无穷大}for(int i=1;i<=n;i++){int num;scanf("%d",&num);for(int j=1;j<=num;j++){int p,q;scanf("%d%d",&p,&q);if(i==1)//如果i=1的话,直接赋初值,因为没有上一个状态转移给idp[i][p]=min(dp[i][p],q);//注意为啥要min(),因为可能dp[i][p]会有多次到达,所以取最小elsefor(int k=0;k<=2000;k++){if(dp[i-1][k]!=INF){if(k<=p){dp[i][k]=min(dp[i][k],dp[i-1][k]+q);//刚开始没有想到这里为什么也要min(),以为//不要也行,提交wa了,后来看代码,因为上面有个for(j)的循环,同样dp[i][k]会有多次}else{dp[i][p]=min(dp[i][p],dp[i-1][k]+q);}}}}}for(int i=0;i<=2000;i++){if(dp[n][i]!=INF){if(1.0*i/dp[n][i]>ans)ans=1.0*i/dp[n][i];}}printf("%.3lf\n",ans);}return 0;
}

  

转载于:https://www.cnblogs.com/caijiaming/p/9294403.html

简单的dp(dp专题)相关推荐

  1. 简单的树形dp NOJ376 小强的Linux

    树形dp是建立的树的基础上的有二叉和三叉等等树,现在学到的知识很简单的树形dp,但还是要注意深搜时要标记避免已经搜素得到过的结果可以直接用了不用在浪费时间去搜素了.还有就是对根节点的处理. 点击打开链 ...

  2. dp入门 专题记录 2017-7-26

    POJ3176-Cow Bowling 题目大意:现有n行数,以金字塔的形式排列,即第一行一个数字,第二行2个数字,依次类推,现在需要找一条从第一层到第n层的路线,使得该路线上的所有点的权值和最大 思 ...

  3. Leetcode之动态规划(DP)专题-1025. 除数博弈(Divisor Game)

    Leetcode之动态规划(DP)专题-1025. 除数博弈(Divisor Game) 爱丽丝和鲍勃一起玩游戏,他们轮流行动.爱丽丝先手开局. 最初,黑板上有一个数字 N .在每个玩家的回合,玩家需 ...

  4. SIMATIC S7-300 Profibus通讯——(3)DP/DP Coupler使用

    SIMATIC S7-300 Profibus通讯 --(3)DP/DP Coupler使用-- 1 DP/DP Coupler模块概述 DP/DP Coupler用于连接两个Profibus-DP主 ...

  5. 简单暴力到dp的优化(中级篇)

    下面再放三道我比较喜欢的,需要好好写一下的题. 第一题比较水 1. White Cloud is exercising in the playground. White Cloud can walk ...

  6. 简单暴力到dp的优化(入门篇)

    上篇,我们提到,遇到问题,首先根据定义写出笨方法,找出依赖关系(有些题这一步就不太简单,要自己归纳关系),然后进行优化,下面,我们通过几道此方面的经典的,较为简单的二维题目进行讲解. 开始根据题来说明 ...

  7. cojs 简单的数位DP 题解报告

    首先这道题真的是个数位DP 我们考虑所有的限制: 首先第六个限制和第二个限制是重复的,保留第二个限制即可 第五个限制在转移中可以判断,不用放在状态里 对于第一个限制,我们可以增加一维表示余数即可 对于 ...

  8. 简单暴力到dp的优化(萌新篇)

    想写一系列文章,总结一些题目,看看解决问题.优化方法的过程到底是什么样子的. 系列问题一:斐波那契数列问题 在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1, F(n)=F( ...

  9. 简单暴力到dp的优化(初级篇)

    一.一维非脑残 1 一个只包含'A'.'B'和'C'的字符串,如果存在某一段长度为3的连续子串中恰好'A'.'B'和'C'各有一个,那么这个字符串就是纯净的,否则这个字符串就是暗黑的.例如: BAAC ...

最新文章

  1. Java记录 -55- Set下的HashSet
  2. python流程控制-Python 流程控制
  3. 北海a货翡翠,牡丹江a货翡翠
  4. 数据中心系统管理员基础知识培训
  5. cryptogen (2)generate 生成证书再举例
  6. 计算机制图学什么,计算机制图
  7. 深度学习分割json_to_data报错Too many dimensions: 3 > 2
  8. 【数据结构与算法】之深入解析“解数独”的求解思路与算法示例
  9. linux环境部署常用命令
  10. 红帽:将开源进行到底
  11. 佳能g3800故障灯说明书_佳能打印机故障如何维修 佳能打印机故障维修方法【介绍】...
  12. hadoop fs命令无法使用_Hadoop从入门到入土(三)HDFS集群简单维护及JAVA客户端连接HDFS...
  13. 后缀的形容词_后缀:ing 名词、形容词或介词后缀
  14. binder.java 565_Android跨进程抛异常的原理的实现
  15. 手游联运系统后台有什么功能?后台功能大全!
  16. MySQL学习笔记[学习资料来源于B站黑马测试]
  17. QQ Account
  18. (转)逃脱者可获生机(上)
  19. 今日头条开通,分享我爱的数码科技
  20. Web应用程序系统的多用户权限控制设计及实现-首页模块【5】

热门文章

  1. 最近录制了一些视频,搭建和测试了一下视频平台
  2. Java+selenium之WebDriver的抛出异常分析(七)
  3. plsql连接本地oracle数据库,而远程主机却无法连接,出现无监听程序的解决方法(转)
  4. 【解决方案】SpringCloud项目优雅发版、部署
  5. linux----------今天又遇到一个奇葩的问题,就是linux文件的权限已经是777了但是还是没有写入权限,按照下面的命令就解决了
  6. 拼多多黄铮长文自述:用常识判断未来
  7. 【报告分享】中国数据智能应用趋势报告:解码数据中台最佳实践,企业数字化转型新引擎.pdf(附下载链接)...
  8. 【干货】如何从0到1构建用户画像系统.pdf(附76页pdf下载链接)
  9. 数据挖掘竞赛,算法刷题网址汇总
  10. 一文速览!多模态预训练中的 Prompt 范式