1. 程式人生 > >Column Addition~DP(腦子抽了,當時沒有想到)

Column Addition~DP(腦子抽了,當時沒有想到)

PV ons vts nba can pua 12c pau 圖片

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 }

Column Addition~DP(腦子抽了,當時沒有想到)