简体   繁体   English

如何确定给定单词是否介于其他两个单词之间?

[英]How to determine whether a given word comes between two other words?

For simplicity, let's say that I have two sets of words, sorted into alphabetical order. 为了简单起见,假设我有两组单词,按字母顺序排序。 One set starts at "aardvark" and ends at "melon", and the other starts at "melon" and ends at "zebra". 一组以“土豚”开始,以“瓜”结束,另一组以“瓜”开始,以“斑马”结束。 The word "melon" appears in both sets. 两组中均出现“瓜”一词。

If I were to take an input word, say "banana", what would be a good (and efficient) way of determining which set of words it should belong to? 如果我要输入一个单词,说“ banana”,那么确定它应属于哪一组单词的一种好(有效)方法是什么? Note: this isn't a question about whether the word "banana" already exists in one set, but rather a question about how to determine which set the word should exist in. 注意:这不是关于“香蕉”一词是否已经存在的问题,而是关于如何确定该单词存在于哪一组的问题。

If there is an algorithm that someone knows, great. 如果有某人知道的算法,那就太好了。 If they can provide some version in Java, even better! 如果他们可以提供Java版本,那就更好了!

Edit: Should also point out, whilst my example only has 2 sets, I want the algorithm to work with n sets. 编辑:还应该指出,虽然我的示例只有2套,但我希望该算法可以使用n套。

For two sets: 对于两套:

If word is your word (eg "banana" ): 如果word是您的单词(例如"banana" ):

int cmp = word.compareTo("melon");
if (cmp < 0) {
  // it belongs to the first set
} else if (cmp > 0) {
  // it belongs to the second set
} else {
  // the word is "melon"
}

For n sets: 对于n套:

Place the dividing words into an ArrayList<String> (call it dividers ) in alphabetical order: 按字母顺序将分隔词放入ArrayList<String> (称为dividers )中:

ArrayList<String> dividers = new ArrayList<String>();
//... populate `dividers` ...
Collections.sort(dividers);

Now you can use Collections.binarySearch() to figure out which set the word belongs to: 现在,您可以使用Collections.binarySearch()找出该单词属于哪个集合:

int pos = Collections.binarySearch(dividers, word);
if (pos >= 0) {
  // the word is the divider between sets `pos` and `pos+1`
} else {
  int num = -(pos + 1);
  // the word belong to set number `num`
}

(Here, the sets are numbered from zero.) (在这里,集合从零开始编号。)

Let's say you have n sets. 假设您有n套。 Construct a list of the "partition" words in sorted order. 按排序顺序构造“分区”单词的列表。

Then the set it belongs to is simply: 那么它所属的集合就是:

List<String> partitions = Arrays.asList("melon", "strawberry");
int setIndex = -(Collections.binarySearch(partitions, "banana")) - 1;

This works because Collections.binarySearch returns the insertion position (-1) if it cannot find the key in the list. 之所以可行,是因为Collections.binarySearch在列表中找不到键时会返回插入位置(-1)。 If it might collide with one of the partition words then you should first check whether the result is negative. 如果它可能与分区词之一冲突,则应首先检查结果是否为负。

Edit 编辑

I edited to remove the requirement for the "book-end" values ("aardvark" and "zebra") as they actually only complicated things. 我进行了编辑,以删除对“ book-end”值(“ aardvark”和“ zebra”)的要求,因为它们实际上只是复杂的事情。

Just check the first letter and see if it's between (first letter of set 1) and (first letter of last element of set 1). 只需检查第一个字母,看看它是否介于(第1组的第一个字母)和(第1组的最后一个元素的第一个字母)之间。 If it's equal to both first letters, move on to the second letters. 如果它等于两个首字母,请继续输入第二个字母。 If it doesn't fit in that set move to the next set. 如果不适合该组,则移至下一组。 This is BigO(n*m), where n is the number of sets and m is the number of letters in your input word. 这是BigO(n * m),其中n是套数,m是输入单词中的字母数。 Not too bad IMO. IMO还不错。

String mid = firstList.get(firstList.size()-1);
assert(mid.equals(secondList.get(0)));
if(newString.compareTo(mid) < 0) // belongs in first
else // belongs in second.

Obviously, you may need to adapt some of the method calls depending on how you're holding them. 显然,您可能需要根据对它们的持有方式来调整某些方法调用。

如果使用二进制堆存储列表,则确定在何处插入单词将花费O(log n)

    final int n = 99; // whatever

    final SortedSet<String>[] allMySets = new SortedSet[ n ];

    // put your sets into allMySets, no particular order required.

    final String searchWord = "banana";

    int i;

    for ( i = 0; i < allMySets.length; i++ ) {

        final SortedSet< String > ss = allMySets[i];

        if ( searchWord.compareTo( ss.first() ) >= 0 && searchWord.compareTo( ss.last() ) <= 0 ) {
            System.out.println("Word " + searchWord + " belongs to set #" + i);
            break;
        }

    }

    if ( i == allMySets.length ) {
        System.out.println("No matching set found.");
        // Maybe handle border case here...
    }

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

相关问题 如何检查和显示其他两个特定单词之间的单词? - How can I check and display the word between two other specific words? 如何确定两个圆形扇区是否相互重叠 - How to determine whether two circular sectors overlap with each other Java:如何检查给定时间是否介于两次之间? - Java: How to check whether given time lies between two times? 如何通过在相邻字符之间添加空格将一个单词分为两个单词 - how to split a word into two words by adding a space between adjacent characters 如何使用递归算法确定数组是否包含两个总和为给定整数的元素? - How do I use a recursive algorithm to determine whether the array contains two elements that sum to a given integer? 给定两个字符串,确定1是否是递归的另一个子字符串 - Given two Strings, determine if 1 is a substring of the other recursively 如何检查给定的字符串是否为单词 - How to check whether given string is a word 正则表达式:在两个单词之间捕获一个单词 - Regex: Capture a word between two words 如何确定给定文件是否为xml验证文件 - How to determine whether a given file is an xml valide file 如何检查字符串列表中的特定单词是否包含在字符串中,但不应该在任何其他单词之间? - How to check if a particular word from a string list contains in a string, but it should not be between any other words?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM