【问题标题】:How to do a partial match of BinarySearch()如何进行 BinarySearch() 的部分匹配
【发布时间】:2012-09-19 10:08:42
【问题描述】:

我正在寻找一种使用二进制搜索进行部分匹配的方法。这是我的代码:

public void checkCardIndexForMatches(List<String> wordsToCheck) throws IOException {
    String[] cardIndexCache = cardIndexCreator.getCardIndexCache();

    for (String text: wordsToCheck){
        int i = Arrays.binarySearch(cardIndexCache, text.getText().toLowerCase().trim());

        if (i > 0){
            text.setCardIndexMatch(true);
        }
        //check if partial match
        //                  else if 
    }
}

到目前为止,它非常简单 - 基本上有一个外部文件被输入,文件中的每一行都作为数组存储在 cardIndexCache 中。当用户希望能够匹配数组中的“短语”(一个短语是多个单词,例如 Mohammed Ali)时,问题就出现了。 wordsToCheck 参数中的单词仅作为单个单词传入。所以第一个词将是 Mohammed 但二分查找失败,因为它不知道第二个词是什么。我想不出一种简单的方法来让二进制搜索表明一个单词有可能成为匹配项(第一部分匹配,只需附加下一个单词并查看是否匹配)。

非常感谢任何想法!

【问题讨论】:

  • 您知道 binarySearch 不只是在未找到项目时返回 -1 吗?它返回 -(如果项目存在,该项目所在的索引)。如果您不想自己实际实现二进制搜索或尝试,您可以使用该返回值来伪造它。
  • 感谢您的提示!我会试一试,看看我的进展如何......
  • 二进制搜索用于精确大小写搜索.. 对按字母顺序排列的字符串列表进行二进制搜索由数字索引引导,最终是精确大小写搜索.. 做一个好的搜索算法,你建立一个 trie,并做一个最大公共前缀作为近似值,或者找到另一种更困难的方法

标签: java data-structures binary-tree


【解决方案1】:

这是我编写的一个 Trie 以及一个基本上可以找到最大公共前缀的搜索功能,相似性搜索是可能的,但成本很高..


class TNode
{
    public MapList<Char,TNode> next;

    public TNode ()
    {
        next = new MapList<Char,TNode>();
    }
}

class Trie
{
    TNode head;

    public Trie ()
    {
        head = new TNode();
    }

    public void insert (String t)
    {
        TNode cur = head;

        for (Char c : t.toCharArray())
        {
            if (!cur.next.containsKey(c))
            {
                cur.next.put(c,new TNode());
            }
            cur = cur.next.get(c);
        }
    }

    public boolean remove (String t)
    {
        Stack<Pair<Char,TNode>> path = new Stack<Pair<Char,TNode>>();
        TNode cur = head;
        Pair<Char,TNode> n = null;

        for (Char c : t.toCharArray())
        {
            if (!cur.next.containsKey(c))
            {
                return false;
            }
            path.push(c,cur);
            cur = cur.next.get(c);
        }

        while (path.size() > 0)
        {
            n = path.pop();
            if (n.getSecond().next.get(n.getFirst()).next.size() > 1)
            {
                break;
            }
            else
            {
                n.getSecond().next.remove(n.getFirst());
            }
        }
    }

    public boolean search (String t)
    {
        TNode cur = head;

        for (Char c : t.toCharArray())
        {
            if (!cur.next.containsKey(c))
            {
                return false;
            }
            cur = cur.next.get(c);
        }

        return true;
    }

    public String searchMaxPrefix (String t)
    {
        TNode cur = head;
        String match = "";

        for (Char c : t.toCharArray())
        {
            if (cur.next.containsKey(c))
            {
                match += c;
            }
            else
            {
                break;
            }
        }

        return match;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多