Column Addition(dp 加法删列)

问题 H: Column Addition

时间限制: 1 Sec   内存限制: 128 MB
提交: 207   解决: 41
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

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[k][i]表示以i位作为最高位时,可以得到的最长的式子。最后用n减掉最长的。k表示是否进位

i位的状态有j得到,j是i的低位;

注意:若j需要进位才能正确,则要求dp[1][j]>0才能更新i状态

【代码】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
const int INF=0x3f3f3f3f;

int dp[2][1010];
char a[3030],b[2303],c[3030];
int main()
{
    int n;
    while(cin>>n,n)
    {
        scanf("%s",a+1);
        scanf("%s",b+1);
        scanf("%s",c+1);
        for(int i=1;i<=n;i++)a[i]-='0';
        for(int i=1;i<=n;i++)b[i]-='0';
        for(int i=1;i<=n;i++)c[i]-='0';

        memset(dp,0,sizeof(dp));
        for(int i=n;i>=1;i--)
        {
            for(int j=n+1;j>i;j--)
            {
                if(a[i]+b[i]==c[i])
                    dp[0][i]=max(dp[0][i],dp[0][j]+1);
                if(a[i]+b[i]+1==c[i]&&dp[1][j])
                    dp[0][i]=max(dp[0][i],dp[1][j]+1);
                if(a[i]+b[i]==c[i]+10)
                    dp[1][i]=max(dp[1][i],dp[0][j]+1);
                if(a[i]+b[i]+1==c[i]+10&&dp[1][j])
                    dp[1][i]=max(dp[1][i],dp[1][j]+1);
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            ans=max(ans,dp[0][i]);
        }
        printf("%d\n",n-ans);
    }
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值