如何减少置换时间?

How to decrese permutation time?

我真的不希望得到答案,但我认为这更像是一场头脑风暴,所以请给你最好的主意:)

所以我想编写一个程序来排列所有 ASCII 个字符,我无法连接 1000 台计算机来计算这个,因为不幸的是我只有一台计算机所以我需要某种算法加快进程。

我制定了算法来找到可能的组合,但我在网上查看,这需要 100 多年的时间。请帮忙。

这是查找排列的代码(它没有找到所有 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;
            }
        }
    }
}

完成排列需要很长时间。在解决需要查看所有可能解决方案的问题时,通常有一些方法可以削减许多排列或有趣的算法以更快地获得解决方案。

下面是一个类似问题的示例: https://projecteuler.net/problem=67 尝试所有组合是不可能的(计算机需要 200 亿年)。但是使用一个有趣的算法,这个问题可以在一秒钟内解决。