Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从具有特定元素的大型集合中查找所有序列子集_Java_String_Algorithm_Loops_For Loop - Fatal编程技术网

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

Java 从具有特定元素的大型集合中查找所有序列子集,java,string,algorithm,loops,for-loop,Java,String,Algorithm,Loops,For Loop,问题定义如下: 输入元素,假设为m 大集合,假设{a,b,c,d,…z} 我想找到包含输入字m的2-5个元素的所有长度子集。 条件:元素的顺序应保持不变 输出: {l,m},{m,n} {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}。。。等等 我能够通过以下代码获得从输入字开始的子集: ArrayList<String> phrases = new ArrayList<>();

问题定义如下: 输入元素,假设为m

大集合,假设{a,b,c,d,…z}

我想找到包含输入字m的2-5个元素的所有长度子集。 条件:元素的顺序应保持不变

输出:

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

  • {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}。。。等等

我能够通过以下代码获得从输入字开始的子集:

    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;
}
ArrayList短语=新建ArrayList();
对于(intj=1;j你写了:“元素的序列应该保持不变”。所以,我假设你不是指集合,因为集合没有顺序,而是类似列表和序列的东西

我的建议是:首先找到长度为2的序列,然后是长度为3的序列,依此类推。这可以通过以下方式完成,比如说长度为4。在大“集合”中找到输入
m
的索引。然后从以
m
作为结果的第一个元素的序列开始。这是
jklm
。将“窗口”向右移动一步,直到找到的序列以
m
开始。因此,您将得到
klmn
lmno
mnop

这可以通过将当前找到的序列的开头作为索引来实现。它必须用
m
减去4作为当前长度加1的索引来初始化。然后你必须迭代4次。

你写:“元素序列应该保留”所以,我假设你不是指集合,因为集合没有顺序,而是类似列表和序列的东西

我的建议是:首先找到长度为2的序列,然后是长度为3的序列,依此类推。这可以通过以下方式完成,比如说长度为4。在大“集合”中找到输入
m
的索引。然后从以
m
作为结果的第一个元素的序列开始。这是
jklm
。将“窗口”向右移动一步,直到找到的序列以
m
开始。因此,您将得到
klmn
lmno
mnop


这可以通过将当前找到序列的开头作为索引来实现。它必须以
m
减去4作为当前长度加1的索引来初始化。然后你必须迭代4次。

正如另一个答案和注释所指出的

  • 首先获取输入单词的索引
  • 将集合的子列表从输入字开始向左和向右递增一步,直到达到其各自的端点(列表的第一个和最后一个元素),或者直到达到最大长度(5)
  • 您应该期望一个包含字符串的列表列表

    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;
    }
    

    正如另一份答复和评论所指出的那样

  • 首先获取输入单词的索引
  • 将集合的子列表从输入字开始向左和向右递增一步,直到达到其各自的端点(列表的第一个和最后一个元素),或者直到达到最大长度(5)
  • 您应该期望一个包含字符串的列表列表

    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;
    }
    

    k和i从哪里来?对于长度=2..5{对于位置=max(0,序列中字母的位置-(长度-1)…序列中字母的位置{如果存在,从位置位置开始追加长度的子序列}}k和i从哪里来?对于长度=2..5{对于位置=max(0,序列中字母的位置-(长度-1)…字母在序列中的位置{append subsequence of length start at position position if exists}}对不起,没有正确格式化代码的最后一部分,我已经尽力了,但是我做不到。对不起,没有正确格式化代码的最后一部分,我已经尽力了,但是我做不到。