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就太简单了