计算字符串的相似度

地址:题目地址

是一个经典dp,最小编辑距离算法 Edit Distance(经典DP)
代码:

#include
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int dp[1005][1005];

int main()
{
    string s1,s2;
    while(cin >> s1 >> s2){
        memset(dp,inf,sizeof(dp));
        int len1 = s1.size();
        int len2 = s2.size();
        for(int i = 0;i <= len1;++i)
            dp[i][0] = i;
        for(int i = 0;i <= len2;++i)
            dp[0][i] = i;
        for(int i = 1;i <= len1;++i){
            for(int j = 1;j <= len2;++j){
                int flag = 0;
                if(s1[i - 1] == s2[j - 1])
                    flag = 0;
                else
                    flag = 1;
                dp[i][j] = min(min(dp[i - 1][j] + 1,dp[i][j - 1] + 1),dp[i - 1][j - 1] + flag);
            }
        }
        cout << "1/" << dp[len1][len2] + 1 << endl;
     }
    return 0;
}

算法讲解:
博客地址:https://blog.csdn.net/baodream/article/details/80417695

你可能感兴趣的:(ACM__动态规划)