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
这题就是给你一个竖式,然后看去除多少列,能使这个竖式正确。
这题其实很好写,唉 ,DP写少了, 其实这个是个很经典的DP
求最长上升子序列的变形,思想一样,只是判断条件不同而已。
这么裸的DP换个样子我就认不出了。菜是原罪啊!!!!
注意
a[i]+b[i]-10==c[i] 只在这个条件下更新ans的值是有原因的,
因为你如果此时存在进位的情况 你并不能判断他到底是不是该去还是留。
求出最长的对的式子,用n-ans答案就出来了
我觉得我要开始我的基础DP训练了 , 这个真的不能再拖了
1 #include <iostream> 2 #include <map> 3 #include <set> 4 #include <string> 5 #include <cstring> 6 #include <cstdio> 7 #include <algorithm> 8 #include <cmath> 9 #include <queue> 10 #include <vector> 11 using namespace std; 12 const int maxn =1005; 13 int a[maxn],b[maxn],c[maxn],dp[maxn],k[maxn]; 14 int n; 15 int main() { 16 //freopen("DATA.txt","r",stdin ); 17 while(scanf("%d",&n),n){ 18 memset(dp,0,sizeof(dp)); 19 memset(k,0,sizeof(k)); 20 for (int i=0 ;i<n ;i++ ) 21 scanf("%1d",&a[i]); 22 for (int i=0 ;i<n ;i++ ) 23 scanf("%1d",&b[i]); 24 for (int i=0 ;i<n ;i++ ) 25 scanf("%1d",&c[i]); 26 int ans=0; 27 for (int i=n-1 ;i>=0 ;i--) { 28 if (a[i]+b[i]==c[i]) { 29 dp[i]=1; 30 k[i]=0; 31 if (dp[i]>ans) ans=dp[i]; 32 } 33 if (a[i]+b[i]-10==c[i]) { 34 dp[i]=1; 35 k[i]=1; 36 } 37 for (int j=n-1 ;j>i ;j--) { 38 if (a[i]+b[i]+k[j]==c[i] && dp[i]<dp[j]+1) { 39 k[i]=0; 40 dp[i]=dp[j]+1; 41 if (dp[i]>ans) ans=dp[i]; 42 } 43 if (a[i]+b[i]+k[j]-10==c[i] && dp[i]<dp[j]+1 ) { 44 k[i]=1; 45 dp[i]=dp[j]+1; 46 } 47 } 48 } 49 printf("%d\n",n-ans); 50 } 51 return 0; 52 }