1. 程式人生 > >CSU-2034 Column Addition

CSU-2034 Column Addition

sin mxd int war each 題解 保留 p2s pbr

CSU-2034 Column Addition

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,給出三個n位數,你可以同時去掉這三個數中的同一位數,使第一個數加第二個數等於第三個數,問最小需要去掉幾位。

題解

這是一個DP題,貪心的去是不對的,隨便舉個例子即可證明貪心錯誤。

\(dp[i]\)為到第i位最多可以保留多少位,用一個變量ans1存儲最大能保留多少位。設a[i]為第一串數字的第i個數,b[i]為第二串數字的第i個數,ans[i]為第三串數字的第i個數,從n到1處理加法。

若(a[i]+b[i])%10==ans[i],說明這一位可以保留,將f[i]設為1,如果有進位,將i處進位(k[i])設為1,如果沒有進位,k[i] = 0, 更新答案最大值,只在不進位時更新最大值是有原因的,因為如果進位的話,你無法確定這一位是否該留,比如99 99 98,均滿足相等,但都有進位,一個也不能留

然後從n到i枚舉j,如果(a[i]+b[i]+k[j])%10==1,那麽f[i]=max(f[i], f[j] + 1),同樣有進位的話更新k[i],無進位時更新一下最大能保留多少位,最後取輸出n-ans1即可

#include<bits/stdc++.h>
using namespace std;
int a[1050], b[1050], ans[1050], f[1050];
int k[1050];
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        if (n == 0) break;
        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", &ans[i]);
        }
        int ans1 = 0;
        memset(f, 0, sizeof(f));
        memset(k, 0, sizeof(k));
        for (int i = n; i >= 1; i--) {
            if ((a[i] + b[i]) % 10 == ans[i]) {
                f[i] = 1;
                if (a[i] + b[i] - 10 == ans[i]) k[i] = 1;
                else {
                    k[i] = 0;
                    ans1 = max(ans1, f[i]);
                }
            }
            for (int j = n; j > i; j--) {
                if ((a[i] + b[i] + k[j]) % 10 == ans[i]) {
                    f[i] = max(f[i], f[j] + 1);
                    if (a[i] + b[i] + k[j] - 10 == ans[i]) k[i] = 1;
                    else {
                        k[i] = 0;
                        ans1 = max(ans1, f[i]);
                    }
                }
            }
        }
        printf("%d\n", n - ans1);
    }
    return 0;
}
/**********************************************************************
    Problem: 2034
    User: Artoriax
    Language: C++
    Result: AC
    Time:148 ms
    Memory:2044 kb
**********************************************************************/

CSU-2034 Column Addition