HDU 4433 (DP)

来源:互联网 发布:mac usb转接口驱动 编辑:程序博客网 时间:2024/06/08 02:26
/*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]);    }}
0 0
原创粉丝点击