【C++】henuACM暑期培训Day14 动态规划

动态规划的代码很短,但是难在思路,主要需要找到状态和状态转移的式子,时间复杂度是O(n^2),不多说了,先看题

A题 Common Subsequence(HDU 1159)

题目

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

题意

就是求最长公共子序列 (LCS)

思路

其实一维数组和二维的都可以.
一维中 状态:dp[ i , j ]表示以a[ i ],b[ j ]结尾的LCS长度
状态转移:当a[ i ] = b[ j ]时,dp[ i , j ] = dp[ i-1 , j-1 ] +1.
否则:dp[ i , j ] = max ( dp[ i -1 , j ] , dp[ i ][ j -1 ] )
在这里插入图片描述
(图片来源网络)

上代码:

#include <iostream>
#include <string.h>
using namespace std;
const int maxx= 999;
int dp[maxx][maxx];

int main()
{
    
    
    string s1,s2;
    while(cin>>s1)
    {
    
    
        cin>>s2;
        memset(dp,0,sizeof dp);
        for(int i=0;i<s1.length();++i){
    
    
            for(int j=0;j<s2.length();++j){
    
    
                if(s1[i]==s2[j]){
    
    
                    if(i>0 && j>0){
    
    
                        dp[i][j]=dp[i-1][j-1]+1;
                    }
                    else{
    
    dp[i][j]=1;}
                }
                if(i>0){
    
    
                    dp[i][j]=max(dp[i-1][j],dp[i][j]);
                }
                if(j>0){
    
    
                    dp[i][j]=max(dp[i][j],dp[i][j-1]);
                }
            }
        }
        int ans=0;
        for(int i=0;i<s1.length();++i){
    
    
            for(int j=0;j<s2.length();++j){
    
    
                ans = max(ans,dp[i][j]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

B题 Super Jumping!!!(HDU 1087)

题目

Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.
在这里插入图片描述
The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.

Input

Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each case, print the maximum according to rules, and one line one case.

Simple Input

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

Simple Output

4
10
3

题意

就是求一个带着权值的上升子序列,使权值加起来最大,其与一般的求最长上升子序列(LIS)不一样的地方也是在这里。

思路

可以直接把dp作为答案,然后把每一步的answer累加上去

代码

#include <string.h>
#include <iostream>
using namespace std;
int n;
const int maxx=999;
int s[maxx];
int dp[maxx];

int main()
{
    
    
    while(cin>>n && n)
    {
    
    
        int ans =0;
        memset(dp,0,sizeof dp);
        for(int i=0;i<n;++i){
    
    
            cin>>s[i];
        }
        for(int i=0;i<n;++i){
    
    ///     重
                dp[i] = s[i]; ///     点
            for(int j=0;j<i;++j){
    
    ///     在
                if(s[i]>s[j] && dp[i]<dp[j]+s[i]){
    
      /// 这
                    dp[i] = dp[j]+s[i];  ///    里
                }
        }
        }
        for(int i=0;i<n;++i){
    
    
            ans = max(ans,dp[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44899247/article/details/97668321
今日推荐