Cappuccino90

Dyck Pfad Algorithmus v3.0

Oct 6th, 2015
171
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class Truthtable {
  6.     static ArrayList<String> array = new ArrayList<String>();
  7.     static int counter = 0;
  8.  
  9.     private static void printTruthTable(int n) {
  10.         double zstVorher = System.currentTimeMillis();
  11.         n *= 2;
  12.  
  13.         int rows = (int) Math.pow(2, n);
  14.  
  15.         String tempString = "";
  16.  
  17.         for (int i = 0; i < rows; i++) {
  18.             for (int j = n - 1; j >= 0; j--) {
  19.  
  20.                 int temp = i / (int) Math.pow(2, j) % 2;
  21.  
  22.                 tempString = tempString.concat((temp == 1) ? "U" : "D");
  23.             }
  24.             if (tempString != "")
  25.                 checkString(tempString);
  26.             tempString = "";
  27.         }
  28.  
  29.         for (String s : array) {
  30.             System.out.println(s);
  31.         }
  32.  
  33.         double zstNachher = System.currentTimeMillis();
  34.         System.out.println("\nZeit benötigt: " + ((zstNachher - zstVorher)) + " msec oder  "
  35.                 + ((zstNachher - zstVorher) / 1000) + " Sekunden");
  36.         System.out.println(counter + " Möglichkeiten für n=" + n / 2 + " (" + n + " Stellen)");
  37.     }
  38.  
  39.     public static void checkString(String s) {
  40.         if ((s.contains("DUDUDU") && (countDUDU(s, "DUDU") <= 1))
  41.                 || (((countDUDU(s, "DUDU") == 2)) && !(s.contains("DUDUDU")))) {
  42.  
  43.             if (s.startsWith("U") && s.endsWith("D")) {
  44.                 if (checkAmountDU(s)) {
  45.                     if (countDUDU(s, "D") == countDUDU(s, "U")) {
  46.                         counter++;
  47.                         array.add(s);
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.     }
  53.  
  54.     public static int countDUDU(String s, String search) {
  55.  
  56.         Pattern p = Pattern.compile(search);
  57.         Matcher m = p.matcher(s);
  58.         int count = 0;
  59.         while (m.find()) {
  60.             count += 1;
  61.         }
  62.         return count;
  63.     }
  64.  
  65.     public static boolean checkAmountDU(String s) {
  66.         for (int i = 1; i <= s.length(); i++) {
  67.             if (!(countDUDU(s.substring(0, i), "D") <= countDUDU(s.substring(0, i), "U"))) {
  68.                 return false;
  69.             }
  70.         }
  71.         return true;
  72.     }
  73.  
  74.     public static void main(String[] args) {
  75.         // hier dein tatsächliches n angeben ! (n=5 hat 3 Möglichkeiten z.B.)
  76.         printTruthTable(9);
  77.     }
  78.  
  79. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment