二、阅读程序2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream> 
#include<cstring> 
using namespace std; 
string a,b; 
int f[2010][2010]; 
int Dfs(int i, int j){  
    if(f[i][j]!=-1)  
        return f[i][j];  
    if(i==0)  
        return f[i][j]=j; 
    if(j==0)  
        return f[i][j]=i; 
    int c=1; 
    if(a[i-1]==b[j-1])  
        c=0;   
    return f[i][j]=min(min(Dfs(i-1,j)+1,Dfs(i,j-1)+1),Dfs(i-1,j-1)+c); 
} 
int main(){ 
    cin>>a>>b; 
    memset(f,-1,sizeof(f)); 
    int len1=a.length(),len2=b.length(); 
    Dfs(len1,len2); 
    cout<<f[len1][len2]; 
return 0; 
} 
Scroll to Top