Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

lcs的变形啊

Posted by 1004115224 at 2012-08-26 15:30:33 on Problem 3356
状态转移方程:
if(a[i-1]==b[j-1])f[i][j]=min(min(f[i-1][j-1],f[i-1][j]+1),f[i][j-1]+1);
                else f[i][j]=min(min(f[i-1][j-1]+1,f[i-1][j]+1),f[i][j-1]+1);
全部代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
int m,n,f[1001][1001];
using namespace std;
int main()
{

    string a,b;
    while(scanf("%d",&m)!=EOF)
    {
        cin>>a;
        scanf("%d",&n);
        cin>>b;
        for(int i=0;i<=m;i++)f[i][0]=i;
        for(int j=0;j<=n;j++)f[0][j]=j;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[i-1]==b[j-1])f[i][j]=min(min(f[i-1][j-1],f[i-1][j]+1),f[i][j-1]+1);
                else f[i][j]=min(min(f[i-1][j-1]+1,f[i-1][j]+1),f[i][j-1]+1);
            }
        }
        cout<<f[m][n]<<endl;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator