Java滚动数组计算编辑距离操作示例

 更新时间:2019年12月02日 10:12:08   作者:在线疯狂  
这篇文章主要介绍了Java滚动数组计算编辑距离操作,涉及java字符串与数组的遍历、计算、转换等相关操作技巧,需要的朋友可以参考下

本文实例讲述了Java滚动数组计算编辑距离操作。分享给大家供大家参考,具体如下:

编辑距离(Edit Distance),也称Levenshtein距离,是指由一个字符串转换为另一个字符串所需的最少编辑次数。

下面的代码摘自org.apache.commons.lang.StringUtils

用法示例:

StringUtils.getLevenshteinDistance(null, *)       = IllegalArgumentException
StringUtils.getLevenshteinDistance(*, null)       = IllegalArgumentException
StringUtils.getLevenshteinDistance("","")        = 0
StringUtils.getLevenshteinDistance("","a")       = 1
StringUtils.getLevenshteinDistance("aaapppp", "")    = 7
StringUtils.getLevenshteinDistance("frog", "fog")    = 1
StringUtils.getLevenshteinDistance("fly", "ant")    = 3
StringUtils.getLevenshteinDistance("elephant", "hippo") = 7
StringUtils.getLevenshteinDistance("hippo", "elephant") = 7
StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz") = 8
StringUtils.getLevenshteinDistance("hello", "hallo")  = 1

Java代码:

public static int getLevenshteinDistance(String s, String t) {
  if (s == null || t == null) {
    throw new IllegalArgumentException("Strings must not be null");
  }
  int n = s.length(); // length of s
  int m = t.length(); // length of t
  if (n == 0) {
    return m;
  } else if (m == 0) {
    return n;
  }
  if (n > m) {
    // swap the input strings to consume less memory
    String tmp = s;
    s = t;
    t = tmp;
    n = m;
    m = t.length();
  }
  int p[] = new int[n+1]; //'previous' cost array, horizontally
  int d[] = new int[n+1]; // cost array, horizontally
  int _d[]; //placeholder to assist in swapping p and d
  // indexes into strings s and t
  int i; // iterates through s
  int j; // iterates through t
  char t_j; // jth character of t
  int cost; // cost
  for (i = 0; i<=n; i++) {
    p[i] = i;
  }
  for (j = 1; j<=m; j++) {
    t_j = t.charAt(j-1);
    d[0] = j;
    for (i=1; i<=n; i++) {
      cost = s.charAt(i-1)==t_j ? 0 : 1;
      // minimum of cell to the left+1, to the top+1, diagonally left and up +cost
      d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);
    }
    // copy current distance counts to 'previous row' distance counts
    _d = p;
    p = d;
    d = _d;
  }
  // our last action in the above loop was to switch d and p, so p now 
  // actually has the most recent cost counts
  return p[n];
}

实际上,上述代码的空间复杂度还可以进一步简化,使用一维数组替换滚动数组。

Java代码:

public int minDistance(String s, String t) {
  if (s == null || t == null) {
    throw new IllegalArgumentException("Strings must not be null");
  }
  int n = s.length(); // length of s
  int m = t.length(); // length of t
  if (n == 0) {
    return m;
  } else if (m == 0) {
    return n;
  }
  if (n > m) {
    // swap the input strings to consume less memory
    String tmp = s;
    s = t;
    t = tmp;
    n = m;
    m = t.length();
  }
  int d[] = new int[n+1]; // cost array, horizontally
  // indexes into strings s and t
  int i; // iterates through s
  int j; // iterates through t
  char t_j; // jth character of t
  int cost; // cost
  for (i = 0; i<=n; i++) {
    d[i] = i;
  }
  for (j = 1; j<=m; j++) {
    t_j = t.charAt(j-1);
    int pre = d[0];
    d[0] = j;
    for (i=1; i<=n; i++) {
      int temp = d[i];
      cost = s.charAt(i-1)==t_j ? 0 : 1;
      // minimum of cell to the left+1, to the top+1, diagonally left and up +cost
      d[i] = Math.min(Math.min(d[i-1]+1, d[i]+1), pre+cost);
      pre = temp;
    }  
  }
  return d[n];
}

更多关于java相关内容感兴趣的读者可查看本站专题:《Java数组操作技巧总结》、《Java字符与字符串操作技巧总结》、《Java数学运算技巧总结》、《Java数据结构与算法教程》及《Java操作DOM节点技巧总结

希望本文所述对大家java程序设计有所帮助。

相关文章

  • Spring框架的JdbcTemplate使用

    Spring框架的JdbcTemplate使用

    它是 Spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装。本文就来介绍一下Spring框架的JdbcTemplate使用,感兴趣的可以了解一下
    2021-09-09
  • 解决IntellIJ IDEA提示内存不足的图文教程

    解决IntellIJ IDEA提示内存不足的图文教程

    现在越来越多的人投入了 IntellIJ Idea 的怀抱, 它给我们的日常开发带来了诸多便利,但是我们在开发过程中,总是能碰到idea内存不足问题,所以本文给大家介绍了解决IntellIJ IDEA提示内存不足的图文教程,需要的朋友可以参考下
    2025-03-03
  • 详解Java JDK动态代理

    详解Java JDK动态代理

    这篇文章主要介绍了Java JDK动态代理的相关资料,帮助大家更好的理解和学习Java 代理的有关知识,感兴趣的朋友可以了解下
    2020-08-08
  • Java使用反射创建对象示例

    Java使用反射创建对象示例

    这篇文章主要介绍了Java使用反射创建对象,结合实例形式分析了java使用反射创建对象的具体实现方法及相关操作技巧,需要的朋友可以参考下
    2019-07-07
  • web容器中实例化spring相关配置解析

    web容器中实例化spring相关配置解析

    这篇文章主要介绍了web容器中实例化spring相关配置解析,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • Spring Security实现添加图片验证功能

    Spring Security实现添加图片验证功能

    这篇文章主要为大家介绍了Spring Security实现添加图片验证功能详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Linux Ubuntu系统下配置JDK环境、MySQL环境全过程

    Linux Ubuntu系统下配置JDK环境、MySQL环境全过程

    众所周知Ubuntu是一种基于Linux的操作系统,它提供了一个稳定、安全和易于使用的环境,下面这篇文章主要给大家介绍了关于Linux Ubuntu系统下配置JDK环境、MySQL环境的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-07-07
  • 解决spirngboot连接redis报错:READONLY You can‘t write against a read only replica的问题

    解决spirngboot连接redis报错:READONLY You can‘t write against 

    docker部署的redis,springboot基本每天来连redis都报错:READONLY You can't write against a read only replica,重启redis后,可以正常连接。但是每天都重启redis,不现实,也很麻烦,今天给大家分享解决方式,感兴趣的朋友一起看看吧
    2023-06-06
  • minio的原理、部署、操作方法(十分钟精通MinIO)

    minio的原理、部署、操作方法(十分钟精通MinIO)

    MinIO是一个简单易用的云存储服务,可以将文件上传到互联网上,方便在不同地方访问,部署MinIO需要拉取镜像、启动端口、设置账号密码和映射文件地址,在Spring Boot中集成MinIO,需要添加依赖、创建客户端并进行单元测试,感兴趣的朋友一起看看吧
    2025-02-02
  • idea2020.1无法自动加载maven依赖的jar包问题及解决方法

    idea2020.1无法自动加载maven依赖的jar包问题及解决方法

    这篇文章主要介绍了idea2020.1无法自动加载maven依赖的jar包问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07

最新评论