PAT_ (dp) 1007 1045 1040 1068

目录

1007 Maximum Subsequence Sum (25分)

1045 Favorite Color Stripe (30分)

1040 Longest Symmetric String (25分) 最长回文子串

1068 Find More Coins (30分)


1007 Maximum Subsequence Sum (25分)

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

 思路:dp,最长公共子序列

#include <iostream>
#include <vector>
using namespace std;
int main() {
    int n;
    scanf("%d", &n);
    vector<int> v(n);

    int left = 0, right = n - 1, sum = -1, temp = 0, tempindex = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &v[i]);
        temp = temp + v[i];
        if (temp < 0) {
            temp = 0;
            tempindex = i + 1;
        } else if (temp > sum) {
            sum = temp;
            left = tempindex;
            right = i;
        }
    }
    if (sum < 0) sum = 0;
    printf("%d %d %d", sum, v[left], v[right]);
    return 0;
}

1045 Favorite Color Stripe (30分)

Eva is trying to make her own color stripe out of a given one.

She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.

Note that the solution might not be unique, but you only have to tell her the maximum length.

For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order

as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤200) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (≤200) followed by M Eva's favorite color numbers given in her favorite order.

Finally the third line starts with a positive integer L (≤10​^4​​) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a separated by a space.

Output Specification:

For each test case, simply print in a line the maximum length of Eva's favorite stripe.

Sample Input:

6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6

Sample Output:

7
题⽬⼤意:给出m中颜⾊作为喜欢的颜⾊(同时也给出顺序),然后给出⼀串⻓度为L的颜⾊序列,现 在要去掉这个序列中的不喜欢的颜⾊,然后把剩下序列作为⼀个⼦序列,使得这个⼦序列表示的颜⾊顺 符合⾃⼰喜欢的颜⾊的顺序,不⼀定要所有喜欢的颜⾊都出现。
 

方法1:LIS

a: 2 3 1 5 6   {1 2 3 4 5}
b: 2 2 4 1 5 5 6 3 1 1 5 6 

先剔除掉4,然后b中剩下的元素按照在a中出现的顺序变为{1 1 3 4 4 5 2 3 3 4 5} 再求这个数组的LIS 

比如:{1 1 3 4 4 5 5} -> {2 2 1 5 5 6 6}

#include <iostream>
#include <vector>
using namespace std;
int book[201], a[10001], dp[10001];
int main() {
     int n, m, x, l, num = 0, maxn = 0;
     scanf("%d %d", &n, &m);
     for(int i = 1; i <= m; i++) {
         scanf("%d", &x);
         book[x] = i;
     }
     scanf("%d", &l);
     for(int i = 0; i < l; i++) {
         scanf("%d", &x);
         if(book[x] >= 1)
         a[num++] = book[x];
     }
    //对a数组求LIS
     for(int i = 0; i < num; i++) {
         dp[i] = 1;
         for(int j = 0; j < i; j++)
             if(a[i] >= a[j])
                 dp[i] = max(dp[i], dp[j] + 1);
                 maxn = max(dp[i], maxn);
             }
         printf("%d", maxn);
         return 0; 
}

方法2:LCS

主串:{2 3 1 5 6 }

target串:{2 2 4 1 5 5 6 3 1 1 5 6}.

对经典LCS的状态转移方程稍作修改,使主串能够与target的多个字符匹配

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#define pb push_back 
using namespace std;
const int maxn=10005;
int n,m,l;
int a[205],b[maxn],dp[205][maxn];

int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		scanf("%d",&a[i]);
	}
	scanf("%d",&l);
	for(int i=0;i<l;i++){
		scanf("%d",&b[i]);
	}
	int maxx;
	for(int i=0;i<m;i++){
		for(int j=0;j<l;j++){
			maxx=max(dp[i][j+1],dp[i+1][j]);
			if(a[i]==b[j]){
				dp[i+1][j+1]=maxx+1;	
			}else{
				dp[i+1][j+1]=maxx;
			}
		}
	}
	printf("%d\n",dp[m][l]);
	return 0;	
}

 

1040 Longest Symmetric String (25分) 最长回文子串

Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11.

Input Specification:

Each input file contains one test case which gives a non-empty string of length no more than 1000.

Output Specification:

For each test case, simply print the maximum length in a line.

Sample Input:

Is PAT&TAP symmetric?

Sample Output:

11

leetcode 5. 最长回文子串

#include <iostream>
using namespace std;
int dp[1010][1010];
int main() {
 	string s;
 	getline(cin, s);
 	int length = s.length(), maxlen = 1, end;
 	//dp
 	for(int len = 1; len<=length; len++) {
 		for(int start=0; start<length; start++){
 			end=start+len-1;
 			if(end>=length) break;
 			dp[start][end]=(len==1 || len==2 && s[start]==s[end] || dp[start+1][end-1] && s[start]==s[end]);
 			if(dp[start][end] && len>maxlen)  maxlen=len;	
 		}
 	}
 	cout<<maxlen;
 	return 0; 
}

 

1068 Find More Coins (30分)

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 10​4​​ coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤10​4​​, the total number of coins) and M (≤10​2​​, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the face values V​1​​≤V​2​​≤⋯≤V​k​​ such that V​1​​+V​2​​+⋯+V​k​​=M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output "No Solution" instead.

Note: sequence {A[1], A[2], ...} is said to be "smaller" than sequence {B[1], B[2], ...} if there exists k≥1 such that A[i]=B[i] for all i<k, and A[k] < B[k].

Sample Input 1:

8 9
5 9 8 7 2 3 4 1

Sample Output 1:

1 3 5

Sample Input 2:

4 8
7 2 4 3

Sample Output 2:

No Solution

题意:⽤n个硬币买价值为m的东⻄,输出使⽤⽅案,使得正好⼏个硬币加起来价值为m。从⼩到 ⼤排列,输出最⼩的那个排列⽅案。

思路:背包问题

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int dp[10010], w[10010];
bool choice[10010][110];
int cmp1(int a, int b){return a > b;}
int main() {
     int n, m;
     scanf("%d%d", &n, &m);
     for(int i = 1; i <= n; i++)
         scanf("%d", &w[i]);
     sort(w + 1, w + n + 1, cmp1);
     for(int i = 1; i <= n; i++) {
         for(int j = m; j >= w[i]; j--) {
             if(dp[j] <= dp[j-w[i]] + w[i]) {
                 choice[i][j] = true;
                 dp[j] = dp[j-w[i]] + w[i];
             }
         }
      }
     if(dp[m] != m) printf("No Solution");
     else {
         vector<int> arr;
         int v = m, index = n;
         while(v > 0) {
             if(choice[index][v] == true) {
             arr.push_back(w[index]);
             v -= w[index];
             }
             index--;
         }
         for(int i = 0; i < arr.size(); i++) {
             if(i != 0) printf(" ");
             printf("%d", arr[i]);
         }
     }
     return 0; 
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值