/*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]);
}
}
HDU 4433 (DP)
最新推荐文章于 2017-10-12 13:27:46 发布