简体   繁体   English

从具有特定元素的大集合中查找所有顺序子集

[英]finding all sequential subsets from a large set with a particular element

The problem is defined as follows: Input Element, assume m 问题定义如下:输入元素,假设为m

Large set, assume {a, b, c, d, ..z} 大集合,假设{a,b,c,d,.. z}

I want to find all the subsets of length ranging from 2-5 elements containing the input word, m. 我想找到所有长度不超过2-5个元素的子集,包含输入字m。 Condition: The sequence of elements should remain same. 条件:元素序列应保持相同。

Output: 输出:

  • {l, m}, {m,n}, {l,m},{m,n},

  • {k, l, m}, {l, m, n}, {m, n, o}, {k,l,m},{l,m,n},{m,n,o},

  • {j, k, l, m}, {k, l, m, n}, {l, m, n, o}, {m, n, o, p}... and so on {j,k,l,m},{k,l,m,n},{l,m,n,o},{m,n,o,p} ......等等

I was able to get the subsets starting at the input word by the following code: 我可以通过以下代码从输入字开始获取子集:

    ArrayList<String> phrases = new ArrayList<>();

    for (int j=1; j<=k-i; j++)  {
        String newSet = set[i] +" ";
        for (int x=1; x<=j; x++)    {
            newSet=newSet+set[i+x]+" ";
        }
        phrases.add(newSet.trim());
    }
    return phrases;
}

You write: "The sequence of elements should remain". 你写道:“元素的顺序应该保留”。 So, I assume that you don't mean sets because sets don't have an order, but rather something like lists and sequences. 所以,我假设你不是指集合,因为集合没有订单,而是列表和序列之类的东西。

My proposal is the following: Find the sequences with length 2 first, then the ones with length 3 and so on. 我的建议如下:首先查找长度为2的序列,然后查找长度为3的序列,依此类推。 This can be done for, let's say length 4 the following way. 这可以通过以下方式表示长度4。 Find the index of input m in the big "set". 在大“集合”中找到输入m的索引。 Then start with the sequence that ends with m as first element of the result. 然后从以m结尾的序列开始,作为结果的第一个元素。 This is jklm . 这是jklm Move the "window" one step to the right until the found sequence begins with m . 将“窗口”向右移动一步,直到找到的序列以m开头。 So, you get klmn , lmno and mnop . 所以,你得到klmnlmnomnop

This can be done by maintaining the begin of the current found sequence as index. 这可以通过将当前找到的序列的开头维持为索引来完成。 It has to initialized with the index of m minus 4 as the current length plus 1. Then you have to iterate 4 times. 它必须用m减去4的索引初始化为当前长度加1.然后你必须迭代4次。

As pointed out by the other answer and also the comments, 正如其他答案和评论所指出的,

  1. first get the index of the input word 首先得到输入词的索引
  2. take sublists of your set both to the left and right from the input word incrementing one step until you reach their respective ends(the first and the last elements of the list) or until you reach your maximum length which is 5. 从输入字左右两侧获取集合的子列表,增加一步,直到到达各自的末尾(列表的第一个和最后一个元素)或直到达到最大长度为5。
  3. you should expect a list of lists containing strings. 您应该期望包含字符串的列表列表。

     private static List<List<String>> getSubSet(List<String> set, String word){ List<List<String>> phrases = new ArrayList<>(); int indexOfWord = set.indexOf(word); int len = 1; while(indexOfWord-len>=0 || indexOfWord+len<=set.size()){ if (indexOfWord-len>=0) phrases.add(set.subList(indexOfWord-len, indexOfWord+1)); if (indexOfWord+len<set.size()) phrases.add(set.subList(indexOfWord, indexOfWord+len+1)); len++; if(len>4) break; } return phrases; } 

    To see a sample test case, I have taken strings of single elements just like you: 要查看示例测试用例,我已经像您一样采用了单个元素的字符串:

    public static void main(String[] args) { List list= Arrays.asList("a","b","c","d","e","f","g","h","i","j"); public static void main(String [] args){List list = Arrays.asList(“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”, “I”, “J”); String inWord = "c"; 字符串inWord =“c”; List> phrases = getSubSet(list,inWord); List> phrase = getSubSet(list,inWord); for(List p: phrases){ System.out.println(p); for(List p:phrase){System.out.println(p); } } }}

Output is: 输出是:

[b, c]
[c, d]
[a, b, c]
[c, d, e]
[c, d, e, f]
[c, d, e, f, g]

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

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