• 算两个字符串之间的编辑距离


    package com.sunmap.utils;

    public class MyUtil {

    // 编辑距离
    public static double calcEditorDistance(String s1, String s2) {
    // TODOAuto-generatedmethodstub
    double d[][];// matrix
    int n;// lengthofs
    int m;// lengthoft
    int i;// iteratesthroughs
    int j;// iteratesthrought
    char s_i;// ithcharacterofs
    char t_j;// jthcharacteroft
    double cost;// cost
    // Step1
    n = s1.length();
    m = s2.length();
    if (n == 0) {
    return m;
    }
    if (m == 0) {
    return n;
    }
    d = new double[n + 1][m + 1];
    // Step2
    for (i = 0; i <= n; i++) {
    d[i][0] = i;
    }
    for (j = 0; j <= m; j++) {
    d[0][j] = j;
    }
    // Step3
    for (i = 1; i <= n; i++) {
    s_i = s1.charAt(i - 1);
    // Step4
    for (j = 1; j <= m; j++) {
    t_j = s2.charAt(j - 1);
    // Step5
    if (s_i == t_j) {
    cost = 0;
    } else {
    cost = 1;
    }
    // Step6
    d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1,
    d[i - 1][j - 1] + cost);
    }
    }
    // Step7
    return d[n][m];
    }

    // 求最小值
    private static double Minimum(double a, double b, double c) {
    // TODOAuto-generatedmethodstub
    double mi;
    mi = a;
    if (b < mi) {
    mi = b;
    }
    if (c < mi) {
    mi = c;
    }
    return mi;
    }
    }

    想的都是好
  • 相关阅读:
    java9新特性-9-语法改进:try语句
    10.04 FZSZ模拟Day1 总结
    10.03模拟总结
    HNOI2012 永无乡
    ZJOI2007 报表统计
    HNOI2004 宠物收养场
    HNOI2002 营业额统计
    Splay 区间反转
    Splay基本操作
    HEOI2016 树
  • 原文地址:https://www.cnblogs.com/freezone/p/4901197.html
Copyright © 2020-2023  润新知