简体   繁体   English

如何减少排列时间?

[英]How to decrese permutation time?

I don't really hope to get an answer but I think of this more like a brainstorm so give you're best idea please :) 我不太希望得到答案,但是我认为这更像是头脑风暴,所以请给你最好的主意:)

So I want to make a program that makes permutations of all the ASCII characters, and I can't connect 1000 computer to calculate this because unfortunately I have only one computer so I need some kind of algorithm to speed up the process. 因此,我想制作一个可以对所有ASCII字符进行排列的程序,并且我无法连接1000台计算机来计算此值,因为不幸的是我只有一台计算机,因此我需要某种算法来加快处理速度。

I made the algorithm to find the possible combinations but I look online and it will take more then 100 years. 我制定了算法来查找可能的组合,但我在网上查找时,将需要100多年的时间。 PLEASE HELP. 请帮忙。

This is the code to find permutations (it doesn't find all ASCII character permutations, but has a string with all letters and numbers and from that string he makes the permutations): 这是查找置换的代码(它无法找到所有ASCII字符置换,但是具有一个包含所有字母和数字的字符串,他从该字符串中进行置换):

import java.io.*;

public class doIt extends AI {

    public void check() {
        String letters = "qwertzuioplkjhgfdsayxcvbnm0123456789-_";
        permute(letters);

    }

    public void permute(String letters) {

        int length = letters.length();
        boolean[] used = new boolean[length];
        StringBuffer str = new StringBuffer(length);

        permutation(str, letters, used, length, 0);
    }

    public void permutation(StringBuffer str, String letters, boolean[] used, int length, int position) {

        if (position == length) {
            try {
                File one = new File("G:/AllDateBases/Combinations.txt");
                PrintWriter pw = new PrintWriter(new FileWriter("G:/AllDateBases/Combinations.txt", true));
                pw.println(str.toString());
                pw.close();
            } catch (IOException e) {
                System.out.println("Error");
            }
            return;
        } else {
            for (int i = 0; i < length; i++) {

                if (used[i]) continue;

                str.append(letters.charAt(i));
                used[i] = true;

                permutation(str, letters, used, length, position + 1);

                str.deleteCharAt(str.length() - 1);
                used[i] = false;
            }
        }
    }
}

Going through permutations takes a long time. 进行排列需要很长时间。 When solving problems that require looking through all the possible solutions, there are often ways to cut out many permutations, or interesting algorithms to get a solution faster. 解决需要仔细研究所有可能解决方案的问题时,通常可以采用多种方法来减少许多排列,或者使用有趣的算法来更快地获得解决方案。

Here's an example of a problem like this: https://projecteuler.net/problem=67 trying all the combinations is impossible (it would take a computer 20 billion years). 这是一个这样的问题示例: https : //projecteuler.net/problem=67尝试所有组合都是不可能的(这将花费200亿年的计算机时间)。 But using an interesting algorithm, the problem can be solved in under a second. 但是使用有趣的算法,问题可以在一秒钟之内解决。

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

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