3 条题解

  • 0
    @ 2025-11-2 18:24:56

    luoguP2758

    #include<bits/stdc++.h>
    #define endl '\n'
    using namespace std;
    const int N=30+5,INF=0x3f3f3f3f,MOD=1e9+7;
    const int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
    typedef long long LL;
    string a,b;
    int lena,lenb,k;
    int main(){
    	ios::sync_with_stdio(false);
    	cin.tie(nullptr);cout.tie(nullptr);
    	cin>>a>>b;
    	lena = a.size(),lenb = b.size();
    	a = " "+a,b = " "+b;
    	vector<vector<int>>dp(lena+1,vector<int>(lenb+1));
    	for(int i=0;i<=lena;i++)dp[i][0] = i;
    	for(int i=0;i<=lenb;i++)dp[0][i] = i;
    	for(int i=1;i<=lena;i++){
    		for(int j=1;j<=lenb;j++){
    			k = 1;
    			if(a[i]==b[j])k = 0;
    			dp[i][j] = min({dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+k});
    		}
    	}
    	cout<<dp[lena][lenb];
    	return 0;
    }
    

    信息

    ID
    1407
    时间
    1000ms
    内存
    128MiB
    难度
    7
    标签
    递交数
    254
    已通过
    68
    上传者