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

提示

[ 提交][ 状态]

这题没做出来,的确是自己盲点,直到问杨哥哥才知道是怎么回事,

这题我是不知道怎么操作的,后来想想非常类似最长上升子序列的做法,最长上升子序列的基础上多加点判断罢了。

代码是杨哥哥提供的:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(~scanf("%d",&n),n){
        int a[1005],b[1005],c[1005];
        for(int i=1;i<=n;i++){
            scanf("%1d",&a[i]);
        }for(int i=1;i<=n;i++){
            scanf("%1d",&b[i]);
        }for(int i=1;i<=n;i++){
            scanf("%1d",&c[i]);
        }
        int dp[1005]={0},ans=0,maxz=0,pos[1005]={0};
        for(int i=n;i>=1;i--){
            if(a[i]+b[i]+pos[i]==c[i]||a[i]+b[i]+pos[i]-10==c[i]){
                dp[i]=1;

                if(a[i]+b[i]+pos[i]==c[i])  pos[i]=0;
                else            pos[i]=1;

                if(pos[i]==0)   ans=max(ans,dp[i]);
            }
            for(int j=n;j>i;j--){
                if(((a[i]+b[i]+pos[j]-10)==c[i]||(a[i]+b[i]+pos[j]==c[i]))&&dp[i]<dp[j]+1){
                    dp[i]=dp[j]+1;

                    if(a[i]+b[i]+pos[j]==c[i])  pos[i]=0;
                    else    pos[i]=1;
                    if(pos[i]==0)  ans=max(ans,dp[i]);
                }
            }
        }
        printf("%d\n",n-ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值