处女座与复读机(dp)
作者:互联网
链接:https://ac.nowcoder.com/acm/contest/327/G
dp[i][j] 代表让a的前i-1个字母和b的前j-1个字母所需要改变的次数。
之前在书上看见过这类题,可是忘了,学过的知识需要好好复习。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[105][105] = {0},n,m;
string a,b;
int main(){
cin>>a>>b;
n = a.size();
m = b.size();
for(int i = 1;i<=n;++i){
for(int j = 1;j<=m;++j){
dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + 1;
dp[i][j] = min(dp[i][j],dp[i-1][j-1] + (a[i-1] != b[j-1]));
}
}
if(dp[n][m] > 2) cout<<"NO\n";
else cout<<"YES\n";
return 0;
}
标签:int,字母,复读机,105,处女座,include,dp,size 来源: https://blog.csdn.net/dukig/article/details/89150778