题目描述
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;
}