Advertisement
dawrehxyz

Untitled

Nov 22nd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3.  
  4. public class DFA
  5. {
  6.     private boolean cyclesInPath;
  7.     private int currentState;
  8.     private int maxCount;
  9.     private boolean stopAddingStrings;
  10.     private ArrayList<Integer> acceptedStates = new ArrayList<>();
  11.     private ArrayList<Transition> transitions = new ArrayList<>();
  12.     private ArrayList<String> acceptedStrings = new ArrayList<>();
  13.  
  14.     private int tempFrom;
  15.     private int tempTo;
  16.     private int tempC;
  17.  
  18.     public DFA(int stateCount, int startState)
  19.     {
  20.         cyclesInPath = false;
  21.         currentState = startState;
  22.         tempFrom = -1;
  23.         tempTo = -1;
  24.         tempC = '-';
  25.         stopAddingStrings = false;
  26.     }
  27.  
  28.     public void setAccepting(int state)
  29.     {
  30.         acceptedStates.add(state);
  31.     }
  32.  
  33.     public void addTransition(int from, int to, char c)
  34.     {
  35.         if(tempFrom == -1 && tempTo == -1 && tempC == '-')
  36.         {
  37.             transitions.add(new Transition(from, to));
  38.             transitions.get(transitions.size() - 1).addC(c);
  39.             tempFrom = from;
  40.             tempTo = to;
  41.             tempC = c;
  42.         }
  43.         else if(from == tempFrom && to == tempTo)
  44.             transitions.get(transitions.size() - 1).addC(c);
  45.         else
  46.         {
  47.             transitions.add(new Transition(from, to));
  48.             transitions.get(transitions.size() - 1).addC(c);
  49.             tempFrom = from;
  50.             tempTo = to;
  51.             tempC = c;
  52.         }
  53.     }
  54.  
  55.     public List<String> getAcceptingStrings(int maxCount)
  56.     {
  57.         this.maxCount = maxCount;
  58.         work(currentState, 0, "");
  59.         return acceptedStrings;
  60.     }
  61.  
  62.     private void work(int currentState, int cycle, String path)
  63.     {
  64.         if(acceptedStrings.size() >= maxCount)
  65.             return;
  66.  
  67.         for(int a = 0; a < acceptedStates.size(); a++)
  68.         {
  69.             if(currentState == acceptedStates.get(a) && path != "")
  70.             {
  71.                 stopAddingStrings = false;
  72.                 addValidStrings(path, "");
  73.                 for(int b = 0; b < transitions.size(); b++)
  74.                 {
  75.                     transitions.get(b).visited = 0;
  76.                 }
  77.                 break;
  78.             }
  79.         }
  80.  
  81.         for(int a = 0; a < transitions.size() && acceptedStrings.size() < maxCount; a++)
  82.         {
  83.             if(a > 0 && !cyclesInPath) break;
  84.             for(int b = 0; b < transitions.size() && acceptedStrings.size() < maxCount; b++)
  85.             {
  86.                 Transition transition = transitions.get(b);
  87.                 if(currentState == transition.from)
  88.                 {
  89.                     if(cycle < transition.visited)
  90.                     {
  91.                         cyclesInPath = true;
  92.                         return;
  93.                     }
  94.                     transition.visited++;
  95.                     work(transition.to, a, path + b);
  96.                 }
  97.                 /*if(b == transitions.size() - 1)
  98.                 {
  99.                     return;
  100.                 }*/
  101.             }
  102.         }
  103.     }
  104.  
  105.     //Adds all valid strings for the current path.
  106.     private void addValidStrings(String path, String word)
  107.     {
  108.         int index = path.charAt(0) - '0';
  109.         path = path.substring(1);
  110.         for(int a = 0; a < transitions.get(index).chars.size(); a++)
  111.         {
  112.             String tempword = word;
  113.             for(int b = 0; b <= a; b++)
  114.             {
  115.                 tempword += transitions.get(index).chars.get(b);
  116.             }
  117.             if(path.compareTo("") != 0)
  118.             {
  119.                 addValidStrings(path,tempword);
  120.                 if(stopAddingStrings) return;
  121.             }
  122.             else
  123.             {
  124.                 if(acceptedStrings.size() >= maxCount)
  125.                 {
  126.                     stopAddingStrings = true;
  127.                     return;
  128.                 }
  129.                 acceptedStrings.add(tempword);
  130.             }
  131.         }
  132.     }
  133.  
  134.     private boolean duplicate(String s)
  135.     {
  136.         for(int a = 0; a < acceptedStrings.size(); a++)
  137.         {
  138.             if(acceptedStrings.get(a).compareTo(s) == 0)
  139.             {
  140.                 return true;
  141.             }
  142.         }
  143.         return false;
  144.     }
  145. }
  146.  
  147. class Transition
  148. {
  149.     ArrayList<Character> chars;
  150.     int from;
  151.     int to;
  152.     int visited;
  153.  
  154.     Transition(int from, int to)
  155.     {
  156.         chars = new ArrayList<>();
  157.         this.from = from;
  158.         this.to = to;
  159.         visited = 0;
  160.     }
  161.  
  162.     public void addC(char c)
  163.     {
  164.         chars.add(c);
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement