Description
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.
Input
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.
Output
For each test case, print a single line containing the minimum number of columns needed to be erased.
Sample Input
3 123 456 579 5 12127 45618 51825 2 24 32 32 5 12299 12299 25598 0
Sample Output
0 2 2 1
题目意思
模拟计算器,给n位数的加法,给出两加数和结果,但结果中有些列的答案是错误滴,问你至少需要去掉多少列(两加数的结果的列同时去电)之后才能使加法得出的结果正确。
一道hin简单的DP题,但多校时候由于状态不佳(naocan),犯了一系列谭浩强问题,使得WA了很多发,在此向队友赔罪,求轻点打。
思路
模拟大数,从最后一位向前加,从DP[n-1]推到DP[0],这个DP[1010][2]是个二维的,DP[i][0]表示在i位时不产生进位的情况下最多有多少列成立,DP[i][1]表示在i位时产生进位的情况下最多有多少列成立。因为第一位不能再产生进位了,所以最后最多有DP[0][0]列成立。
然后回答n-DP[0][0]即可。
时间复杂度O(n).
code:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
char readchar(){
char cc;
cc=getchar();
while(cc=='\n'||cc=='\r'||cc==' '){
cc=getchar();
}
return cc;
}
int a[1010], b[1010], c[1010];
int dp[1010][2], n;
void read(){
for (int i=0;i<n;++i)
a[i]=readchar()-'0';
for (int i=0;i<n;++i)
b[i]=readchar()-'0';
for (int i=0;i<n;++i)
c[i]=readchar()-'0';
return ;
}
int main(){
while(scanf("%d",&n)){
if (n==0)
break;
read();
memset(dp,0,sizeof(dp));
for (int i=n-1;i>=0;--i){
if ((a[i]+b[i])%10==c[i]){
if (a[i]+b[i]>9){
dp[i][1]=dp[i+1][0]+1;
}else{
dp[i][0]=dp[i+1][0]+1;
}
}
if (dp[i+1][1]){
if ((a[i]+b[i]+1)%10==c[i]){
if ((a[i]+b[i]+1>9)){
dp[i][1]=dp[i+1][1]+1;
}else{
dp[i][0]=dp[i+1][1]+1;
}
}
}
dp[i][1]=max(dp[i+1][1],dp[i][1]);
dp[i][0]=max(dp[i+1][0],dp[i][0]);
}
printf("%d\n",n-dp[0][0]);
}
return 0;
}