HDU 4433 (DP)

/*dp[i][j][k]表示第i個數之前的數全部匹配,第i+1上變化了j,i+2上變化了k的最小
值*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 1007;
const int INF = 0x3f3f3f3f;
char s1[maxn], s2[maxn];
int dp[maxn][11][11];

int main() {
    while(~scanf("%s%s", s1, s2)) {
        int len = strlen(s1);
        for(int i = 0; i <= len; ++i)
            for(int j = 0; j < 10; ++j)
                for(int k = 0; k < 10; ++k) dp[i][j][k] = INF;

        dp[0][0][0] = 0;
        for(int i = 0; i < len; ++i) {
            for(int j = 0; j < 10; ++j) {
                for(int k = 0; k < 10; ++k) {
                    int tmp = (s2[i]-s1[i]-j+20)%10;
                    for(int a = 0; a <= tmp; ++a) {
                        for(int b = 0; b <= a; ++b) {
                            dp[i+1][(a+k)%10][b] = min(dp[i][j][k]+tmp, dp[i+1][(a+k)%10][b]);
                        }
                    }
                    tmp = 10-tmp;
                    for(int a = 0; a <= tmp; ++a) {
                        for(int b = 0; b <= a; ++b) {
                            dp[i+1][(10-a+k)%10][(10-b)%10] = min(dp[i][j][k]+tmp, dp[i+1][(10-a+k)%10][(10-b)%10]);
                        }
                    }
                }
            }
        }
        printf("%d\n", dp[len][0][0]);
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章