2021-01-27 dp入门

dp 算法入门

我感觉和递归差差不多

A - Common Subsequence

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, …, xm> another sequence Z = <z1, z2, …, zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Input
abcfbc abfcab
programming contest
abcd mnp
Output
4
2
0
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0

题意

找最长相同子序列

题解

从前往后找最大的,递归,
if(s1[i-1] == s2[j-1]) maxlen[i][j] = maxlen[i-1][j-1] + 1;
else maxlen[i][j] = max(maxlen[i-1][j],maxlen[i][j-1]);

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int MAXN=1009;
const int inf=0x3f3f3f3f;//无穷大 
#define endl "\n"
#define pii pair<int, int>
int maxlen[MAXN][MAXN], s1len, s2len;
char s1[MAXN], s2[MAXN];
int main(){
	while(~scanf("%s%s",s1,s2)){
		s1len = strlen(s1);
		s2len = strlen(s2);
		memset(maxlen,0,sizeof maxlen);
		for(int i=1; i<=s1len; i++){
			for(int j=1; j<=s2len; j++){
				if(s1[i-1] == s2[j-1]) maxlen[i][j] = maxlen[i-1][j-1] + 1;
				else maxlen[i][j] = max(maxlen[i-1][j],maxlen[i][j-1]);
			}
		}
		printf("%d\n",maxlen[s1len][s2len]);
	}
	return 0;
}

F - Catching Cheaters

You are given two strings A and B representing essays of two students who are suspected cheaters. For any two strings C, D we define their similarity score S(C,D) as 4⋅LCS(C,D)−|C|−|D|, where LCS(C,D) denotes the length of the Longest Common Subsequence of strings C and D.

You believe that only some part of the essays could have been copied, therefore you’re interested in their substrings.

Calculate the maximal similarity score over all pairs of substrings. More formally, output maximal S(C,D) over all pairs (C,D), where C is some substring of A, and D is some substring of B.

If X is a string, |X| denotes its length.

A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.

Pay attention to the difference between the substring and subsequence, as they both appear in the problem statement.

You may wish to read the Wikipedia page about the Longest Common Subsequence problem.

Input
The first line contains two positive integers n and m (1≤n,m≤5000) — lengths of the two strings A and B.

The second line contains a string consisting of n lowercase Latin letters — string A.

The third line contains a string consisting of m lowercase Latin letters — string B.

Output
Output maximal S(C,D) over all pairs (C,D), where C is some substring of A, and D is some substring of B.

Examples
Input
4 5
abba
babab
Output
5
Input
8 10
bbbbabab
bbbabaaaaa
Output
12
Input
7 7
uiibwws
qhtkxcn
Output
0
Note
For the first case:

abb from the first string and abab from the second string have LCS equal to abb.

The result is S(abb,abab)=(4⋅|abb|) - |abb| - |abab| = 4⋅3−3−4=5.

题意

同样是最长子序列,他求贡献值,可以取部分 求最大贡献值

思路

刚开始以为不能部分的就在A的上面加发现样例过不了就改了
如果相同,贡献值会加2,看码吧

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int MAXN=5020;
const int inf=0x3f3f3f3f;//无穷大 
#define endl "\n"
#define pii pair<int, int>
int maxlen[MAXN][MAXN], s1len, s2len;
char s1[MAXN], s2[MAXN];
int main(){
	scanf("%d%d",&s1len,&s2len);
	scanf("%s%s",s1,s2);
	memset(maxlen,0,sizeof maxlen);
	int m1 = 0;
	for(int i=1; i<=s1len; i++){
		for(int j=1; j<=s2len; j++){
			if(s1[i-1] == s2[j-1]) maxlen[i][j] = max(2,maxlen[i-1][j-1]+2);//子序列加一,s(i,j)加2 ,刚开始都没有 所以maxlen[i-1][j-1]可能是负数,所以直接舍弃前面的然后直接是2 
			else maxlen[i][j] = max(maxlen[i-1][j],maxlen[i][j-1])-1;//子序列不变,s(i,j)减1 
			m1 = max(m1, maxlen[i][j]);
		}
	}
	printf("%d\n",m1);
	return 0;
}

D - Bone Collector

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 231).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14

题意

用10体积的背包装最多质量的骨头

思路

本来用贪心,但是没用,然后就查到了01背包
别人思路

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
const int MAXN=120*120;
const int inf=0x3f3f3f3f;//无穷大 
#define endl "\n"
#define pii pair<int, int>
int n, m, a[1010], v[1010], d[1010][1010];
int T;
int main(){
    cin>>T;
    while(T--){
    	memset(d,0,sizeof d);
    	cin>>n>>m;
        for(int i=1 ; i<=n ; i++){
        	cin>>a[i];
		}
        for(int i=1 ; i<=n ; i++){
        	cin>>v[i];
		}
		for(int i=1 ;i<=n; i++){
			for(int j=0;j<=m;j++){
				if(j>=v[i]){
					d[i][j] = max(d[i-1][j],(d[i-1][j-v[i]]+a[i]));
				}
				else d[i][j] = d[i-1][j];
			}
		}
		cout<<d[n][m]<<endl;
	}
	
	return 0;
}

C - 滑雪

Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-…-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Sample Output
25

题意

就从高点(a[i]较大的)往低点(a[i]较小的)最多能走几步

思路

本来想从小往上加, 用dfs, 但好像不太行,就按照题意,从高往低走,以后一定要先看MAXN!!!

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
const int MAXN=120*120;
const int inf=0x3f3f3f3f;//无穷大 
#define endl "\n"
#define pii pair<int, int>
int n, m, a[111][111], d[111][111];
int T;
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
int dfs(int x, int y){//找最大降序列 
	if(d[x][y] != 0) return d[x][y];
	d[x][y] = 1;//子序列最少是1 
	for(int i=0 ;i<4; i++){
		int xx = x+dx[i], yy = y+dy[i];
		if(xx>=1 && xx<=n && yy>=1 && yy<=m && a[x][y]>a[xx][yy]){
			d[x][y] = max(dfs(xx,yy)+1, d[x][y]);
		}
	}
	return d[x][y];
}
int main(){
    cin>>n>>m;
    for(int i=1; i<=n ;i++) {
    	for(int j=1;j<=m;j++){
    		cin>>a[i][j];d[i][j] = 0;
		}
	}
	int m1 = -1;
	for(int i=1; i<=n ;i++) {
    	for(int j=1;j<=m;j++){
    		d[i][j] = dfs(i,j);
    		m1 = max(m1,d[i][j]);
		}
	}
	printf("%d\n",m1);
	return 0;
}

B就太简单了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统根据B/S,即所谓的电脑浏览器/网络服务器方式,运用Java技术性,挑选MySQL作为后台系统。系统主要包含对客服聊天管理、字典表管理、公告信息管理、金融工具管理、金融工具收藏管理、金融工具银行卡管理、借款管理、理财产品管理、理财产品收藏管理、理财产品银行卡管理、理财银行卡信息管理、银行卡管理、存款管理、银行卡记录管理、取款管理、转账管理、用户管理、员工管理等功能模块。 文中重点介绍了银行管理的专业技术发展背景和发展状况,随后遵照软件传统式研发流程,最先挑选适用思维和语言软件开发平台,依据需求分析报告模块和设计数据库结构,再根据系统功能模块的设计制作系统功能模块图、流程表和E-R图。随后设计架构以及编写代码,并实现系统能模块。最终基本完成系统检测和功能测试。结果显示,该系统能够实现所需要的作用,工作状态没有明显缺陷。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。进入银行卡列表,管理员可以进行查看列表、模糊搜索以及相关维护等操作。用户进入系统可以查看公告和模糊搜索公告信息、也可以进行公告维护操作。理财产品管理页面,管理员可以进行查看列表、模糊搜索以及相关维护等操作。产品类型管理页面,此页面提供给管理员的功能有:新增产品类型,修改产品类型,删除产品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值