51Nod 1183

来源:互联网 发布:c语言优先级排序 编辑:程序博客网 时间:2024/06/05 15:05


仔细想想。

dp递推式:

if(s1[i] != s2[j])       dp[i][j] = min(dp[i - 1][j - 1],min(dp[i - 1][j],dp[i][j - 1])) + 1;else dp[i][j] = dp[i - 1][j - 1];


#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<algorithm>#include<map>using namespace std;const int maxn = 5000 + 10;#define INF 0x3f3f3f3f#define clr(x,y) memset(x,y,sizeof x )typedef long long ll;#define eps 10e-10const ll Mod = 1000000007;typedef pair<ll, ll> P;char s1[maxn],s2[maxn];int dp[maxn][maxn];int n;int main(){    while( ~ scanf("%s%s",s1 + 1,s2 + 1))    {        int n = strlen(s1 + 1),m = strlen(s2 + 1);        dp[0][0] = 0;        for(int i = 1; i <= n; i ++)            dp[i][0] = i;        for(int i = 1; i <= m; i ++)            dp[0][i] = i;        for(int i = 1; i <= n; i ++)        {            for(int j = 1; j <= m; j ++)            {                if(s1[i] != s2[j])                dp[i][j] = min(dp[i - 1][j - 1],min(dp[i - 1][j],dp[i][j - 1])) + 1;                else dp[i][j] = dp[i - 1][j - 1];            }        }//        for(int i = 1; i <= n; i ++)//        {//            for(int j = 1; j <= m; j ++)//                printf("%10d",dp[i][j]);//            cout << endl;//        }        printf("%d\n",dp[n][m]);    }    return 0;}


原创粉丝点击