繁体   English   中英

查找两个字符串之间最短路径的数据结构

[英]Datastrutcture for finding shortest path between two strings

我正在创建一个程序,该程序将使用5000个字符串的单词列表,并找到从一个字符串到另一个字符串的最短路径。 例如,abc-> bac可以显示“ abc,bbc,bac”。

我很确定自己想做什么,唯一不确定的是什么数据结构应该代表我的单词表。 目标是搜索(BFS)尽可能快地运行,因此牺牲一些空间是没有问题的。 我在考虑BST或邻接表,但是由于我不是datastrutcutres的时间复杂性方面的专家,因此我想在开始调整代码之前先确定一下。 谁能推荐其中一个结构而不是另一个? 还是我可能错过了一个显然可以替代这种情况的数据结构?

看起来您正在寻找的是Levenshtein距离这是Rosetta代码实现 ,您应该能够更改它以满足您的需要:

public class Levenshtein {

    public static int distance(String a, String b) {
        a = a.toLowerCase();
        b = b.toLowerCase();
        // i == 0
        int [] costs = new int [b.length() + 1];
        for (int j = 0; j < costs.length; j++)
            costs[j] = j;
        for (int i = 1; i <= a.length(); i++) {
            // j == 0; nw = lev(i - 1, j)
            costs[0] = i;
            int nw = i - 1;
            for (int j = 1; j <= b.length(); j++) {
                int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
                nw = costs[j];
                costs[j] = cj;
            }
        }
        return costs[b.length()];
    }

    public static void main(String [] args) {
        String [] data = { "kitten", "sitting", "saturday", "sunday", "rosettacode", "raisethysword" };
        for (int i = 0; i < data.length; i += 2)
            System.out.println("distance(" + data[i] + ", " + data[i+1] + ") = " + distance(data[i], data[i+1]));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM