简体   繁体   English

Java非重复序列算法

[英]Java Non repeating sequence algorithm

Hey everyone So I am making an application that with loop through an array of 1625 words & create a 12 word string. 大家好,所以我正在制作一个具有1625个单词的循环并创建12个单词的字符串的应用程序。 The problem is that a single word can't show up twice in the string. 问题是单个单词不能在字符串中出现两次。

(Numbered) ex.  
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, 10, 11  
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, 10, 12  
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, 10, ...  
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, 10, 1625  
then   
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, 11, 10  
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, 11, 12  
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, 11, ...  
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, 11, 1625  
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, ..., ...  
0, 1, 2,  3, 4, 5, 6, 7, 8, 9, 1625, 1624  
then   
0, 1, 2,  3, 4, 5, 6, 7, 8, 10, 9, 11  
0, 1, 2,  3, 4, 5, 6, 7, 8, 10, 9, 12  
0, 1, 2,  3, 4, 5, 6, 7, 8, 10, 9, ...  
0, 1, 2,  3, 4, 5, 6, 7, 8, 10, 9, 1625  
0, 1, 2,  3, 4, 5, 6, 7, 8, 10, 9, 12    
And so on......  

I am having a very hard time figuring out the math behind this algorithm as I would prefer to avoid "if" statements. 我很难弄清楚该算法背后的数学原理,因为我希望避免使用“ if”语句。 If someone would be able to help me that would be great. 如果有人能够帮助我,那就太好了。

(Note the numbers above are the indexes in the array that holds words) (请注意,上面的数字是包含单词的数组中的索引)

EDIT: SIMPLE OUTPUT: 编辑:简单输出:
0, 1, 2 0、1、2
0, 1, 3 0、1、3
0, 1, 4 0、1、4
0, 1, 5 0、1、5
0, 1, 6 0、1、6
0, 1, ... 0、1,...
0, 1, 9 0、1、9
0, 2, 1 0、2、1
0, 2, 3 0、2、3
0, 2, 4 0、2、4
0, 2, 5 0、2、5
0, 2, ... 0、2 ...
0, 9, ... 0、9 ...
0, 9, 1 0、9、1
0, 9, 2 0、9、2
0, 9, 3 0、9、3
0, 9, ... 0、9 ...
1, 0, 2 1 0 2
1, 0, 3 1 0 3
1, 0, 4 1 0 4
1, 0, ... 1,0,...
1, 2, 3 一二三
And so on 等等

Its not a very optional solution as I feel its brute force way of doing it. 它不是一个非常可选的解决方案,因为我认为它是蛮力的。
Would this work for you? 这对您有用吗? You can replace the TOTAL_SIZE variable to 1626 (as you want 1625 to get printed as well.) and CHUNK_SIZE to 12. 您可以将TOTAL_SIZE变量替换为1626(也希望同时打印CHUNK_SIZE ),并将CHUNK_SIZE为12。

public class TestClass {
    static int TOTAL_SIZE = 5;
    static int CHUNK_SIZE = 3;
    static int[] wordArray = new int[TOTAL_SIZE];

    public static void main(String args[]) {
        for(int i = 0; i < TOTAL_SIZE; i++) {
            wordArray[i] = i;
        }

        List<Integer> newList = new ArrayList<>();
        int[] visitedIndexes = new int[TOTAL_SIZE];
        printAllValues(newList, visitedIndexes);
    }


    private static void printAllValues(List<Integer> newList, int[] visitedIndexes) {
        if(newList.size() == CHUNK_SIZE) {
            newList.stream().forEach(System.out::print);
            System.out.println(""); // new line
            return;
        }

        for (int i = 0; i < TOTAL_SIZE; i++) {
            if (visitedIndexes[i] != 1) {
                visitedIndexes[i] = 1;
                newList.add(wordArray[i]);
                printAllValues(newList, visitedIndexes);
                newList.remove((Integer) wordArray[i]);
                visitedIndexes[i] = 0;
            }
        }
    }
}

Output : For array size of 5 and word size of 3. 输出:数组大小为5,字大小为3。

012
013
014
021
023
024
031
032
034
041
042
043
102
103
104
120
123
124
130
132
134
140
142
143
201
203
204
210
213
214
230
231
234
240
241
243
301
302
304
310
312
314
320
321
324
340
341
342
401
402
403
410
412
413
420
421
423
430
431
432

Description : 说明:

  1. Take each value from the original list. 从原始列表中获取每个值。
  2. For each character appended, populate every combination of remaining characters. 对于附加的每个字符,填充其余字符的每个组合。
  3. Retry this logic, by removing the skipping this appended character and trying out every possible combination. 重试此逻辑,方法是删除跳过此附加字符并尝试所有可能的组合。

This logic can be optimized: 可以优化此逻辑:

  1. by checking up front as to how many values are left in for(int i = 0; i < TOTAL_SIZE; i++) loop and break out if there aren't enough values to go ahead (ie, if((list.size() + (TOTAL_SIZE-i)) < CHUNK_SIZE) break; 通过for(int i = 0; i < TOTAL_SIZE; i++)检查for(int i = 0; i < TOTAL_SIZE; i++)还剩下多少值,并循环检查是否没有足够的值继续前进(即if((list.size() + (TOTAL_SIZE-i)) < CHUNK_SIZE) break;

Also, for a huge data the recursion might just blow up. 另外,对于海量数据,递归可能会失败。

This code can run on small set. 此代码可以在较小的集合上运行。 But I am not sure how long it take in huge data in your case. 但是我不确定您需要花费多长时间来处理大量数据。 Hope it help! 希望对您有所帮助!

static int STRING_SIZE = 3; //12 in your  case
static int MAX_VAL = 10; //1625 in your case
static Set<Integer> set = new LinkedHashSet<Integer>();

public static void main(String[] args) {
    fillSet();
}

static void fillSet() {
    if (set.size() == STRING_SIZE) {
        System.out.println(set);
        return;
    }
    for (int i = 0; i < MAX_VAL; i++) {
        if (set.contains(i))
            continue;
        set.add(i);

        fillSet();

        set.remove(i);
    }

}

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

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