简体   繁体   中英

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.

I made the algorithm to find the possible combinations but I look online and it will take more then 100 years. 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):

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). But using an interesting algorithm, the problem can be solved in under a second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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