Column Addition(最长上升子序列的变形 DP)

题目描述

A multi-digit column addition is a formula on adding two integers written like this:
A multi-digit column addition is written on the blackboard, but the sum is not necessarily correct. We can erase any  number of the columns so that the addition becomes correct. For example, in the following addition, we can obtain a  correct addition by erasing the second and the forth columns.
Your task is to find the minimum number of columns needed to be erased such that the remaining formula becomes a  correct addition.

输入

There are multiple test cases in the input. Each test case starts with a line containing the single integer n, the number of  digit columns in the addition (1 ⩽ n ⩽ 1000). Each of the next 3 lines contain a string of n digits. The number on the third  line is presenting the (not necessarily correct) sum of the numbers in the first and the second line. The input terminates  with a line containing “0” which should not be processed.

输出

For each test case, print a single line containing the minimum number of columns needed to be erased.

样例输入

3
123
456
579
5
12127
45618
51825
2
24
32
32
5
12299
12299
25598
0
样例输出

0
2
2
1

题意:这题就是给你一个竖式,然后看去除多少列,能使这个竖式正确。(最长上升子序列的变形)
题解:
开个dp数组,dp[i]代表从最后一列到第i列最多可以保留的列数(上升子序列),需要注意的是进位问题,在这里我来了一个jw数组来记录i位置是否有进位,最后找出dp中的最大保留数,将n与其做差即可。

 
 

注意:

a[i]+b[i]-10==c[i] 只在这个条件下不更新ans的值是有原因的,因为你如果此时存在进位的情况  你并不能判断他到底是不是该去还是留。求出最长的对的式子,用n-ans答案就出来了
 
 
DP代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn =1005;
int a[maxn],b[maxn],c[maxn],dp[maxn],k[maxn];
int n;
int main() 
{
    while(scanf("%d",&n),n)
    {
        memset(dp,0,sizeof(dp));
        memset(k,0,sizeof(k));
        for (int i=0 ;i<n ;i++ )
            scanf("%1d",&a[i]);
        for (int i=0 ;i<n ;i++ )
            scanf("%1d",&b[i]);
        for (int i=0 ;i<n ;i++ )
            scanf("%1d",&c[i]);
    
        int ans=0;
        for (int i=n-1 ;i>=0 ;i--) 
        {
            if (a[i]+b[i]==c[i]) 
            {
                dp[i]=1;
                k[i]=0;
                if (dp[i]>ans) ans=dp[i];
            }
            if (a[i]+b[i]-10==c[i]) 
            {
                dp[i]=1;
                k[i]=1;
            }
            for (int j=n-1 ;j>i ;j--) 
            {
                if (a[i]+b[i]+k[j]==c[i] && dp[i]<dp[j]+1) 
                {
                    k[i]=0;
                    dp[i]=dp[j]+1;
                    if (dp[i]>ans) ans=dp[i];
                }
                if (a[i]+b[i]+k[j]-10==c[i] && dp[i]<dp[j]+1 ) 
                {
                    k[i]=1;
                    dp[i]=dp[j]+1;
                }
            }
        }
        printf("%d\n",n-ans);
    }
    return 0;
}

最长上升子序列
#include<iostream>
using namespace std;
#define max(a,b) a>b?a:b
int main()
{
    int n, i, j, dp[101], x[101], max_len;
    while (cin >> n)
    {
        for (i = 0; i < n; i++)
            cin >> x[i];
        dp[0] = 1;//表示以x[0]为子序列最右边的长度位1
        for (i = 1; i < n; i++)
        {
            dp[i] = 1;//初始化每种情况最小值为1
            for (j = 0; j < i; j++)
            {
                if (x[i]>x[j] && dp[j] + 1>dp[i])//从0-i进行扫描,查找边界小于当前最优解长度相等的解优化最优解
                    dp[i] = dp[j] + 1;//如果允许子序列相邻元素相同  x[i]>=x[j]&&dp[j]+1>dp[i];
            }
        }
        for (i = max_len = 0; i < n; i++)
            max_len = max(max_len, dp[i]);//等到最大子序列长度
        cout << max_len << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41021816/article/details/80052332