NEU(1262: ASCII Sequence II)动态规划

来源:互联网 发布:php utf8转gbk 编辑:程序博客网 时间:2024/06/08 07:29

http://acm.neu.edu.cn/hustoj/problem.php?id=1262

#include <iostream>#include <stdio.h>#include <string>#include <string.h>#include <algorithm>#include <math.h>#include <fstream>#include <vector>#include <map>#include <queue>#include <stack>#include <math.h>#include <stdlib.h>#include <set>#include <math.h>#include <sstream>using namespace std ;const int maxn = 1005;int dp[maxn][maxn];int min(int a,int b){    return a>b?b:a;}string s1,s2;int main(){    while(cin>>s1>>s2){        int len_s1 = s1.size();        int len_s2 = s2.size();        int sum = 0;        for(int i = 0;i < len_s2;i++){            sum += abs(s1[i] - s2[i]);            dp[i][i] = sum;        }        dp[0][0] = abs(s2[0] - s1[0]);        for(int i = 1;i < len_s1;i++)            dp[0][i] = min(dp[0][i-1],abs(s2[0] - s1[i]));        for(int i = 1;i < len_s2;i++){            for(int j = i+1;j < len_s1;j++){                dp[i][j] = min(dp[i][j-1],dp[i-1][j-1] + abs(s2[i] - s1[j]));            }        }        /*for(int i = 0;i < len_s2;i++){            for(int j = 0;j < len_s1;j++)                printf("%d ",dp[i][j]);            printf("\n");        }*/        printf("%d\n",dp[len_s2 - 1][len_s1 - 1]);    }    return 0;} 


0 0