dawrehxyz

EnVackerDagPer

Nov 21st, 2016
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3.  
  4. public class DFA
  5. {
  6.     private int currentState;
  7.     private int maxCount;
  8.     private final int stateCount;
  9.     private final int startState;
  10.     private ArrayList<Integer> acceptedStates = new ArrayList<>();
  11.     private ArrayList<Transition> transitions = new ArrayList<>();
  12.     private ArrayList<String> acceptedStrings = new ArrayList<>();
  13.  
  14.     public DFA(int stateCount, int startState)
  15.     {
  16.         this.stateCount = stateCount;
  17.         this.startState = startState;
  18.         currentState = startState;
  19.     }
  20.  
  21.     public void setAccepting(int state)
  22.     {
  23.         acceptedStates.add(state);
  24.     }
  25.  
  26.     public void addTransition(int from, int to, char c)
  27.     {
  28.         transitions.add(new Transition(from,to,c));
  29.     }
  30.  
  31.     public List<String> getAcceptingStrings(int maxCount)
  32.     {
  33.         this.maxCount = maxCount;
  34.         work(currentState, currentState, "", false);
  35.         work(currentState, currentState, "", true);
  36.         return acceptedStrings;
  37.     }
  38.  
  39.     private void work(int currentState, int previousState, String word, boolean cycle)
  40.     {
  41.         if(acceptedStrings.size() >= maxCount)
  42.             return;
  43.  
  44.         for(int a = 0; a < acceptedStates.size(); a++)
  45.         {
  46.             if(currentState == acceptedStates.get(a) && word != "" && !duplicate(word))
  47.             {
  48.                 acceptedStrings.add(word);
  49.                 break;
  50.             }
  51.         }
  52.  
  53.         for(int a = 0; a < transitions.size() && acceptedStrings.size() < maxCount; a++)
  54.         {
  55.             Transition transition = transitions.get(a);
  56.  
  57.             if(currentState == transition.from)
  58.             {
  59.                 if(transition.from == transition.to && !cycle) continue;
  60.  
  61.                 word += transition.c;
  62.                 transition.visited = true;
  63.                 work(transition.to, transition.from, word, false);
  64.                 work(transition.to, transition.from, word, true);
  65.                 transition.visited = false;
  66.             }
  67.         }
  68.     }
  69.  
  70.     private boolean duplicate(String s)
  71.     {
  72.         for(int a = 0; a < acceptedStrings.size(); a++)
  73.         {
  74.             if(acceptedStrings.get(a).compareTo(s) == 0)
  75.             {
  76.                 return true;
  77.             }
  78.         }
  79.         return false;
  80.     }
  81. }
  82.  
  83. class Transition
  84. {
  85.     int from;
  86.     int to;
  87.     char c;
  88.     boolean visited;
  89.  
  90.     Transition(int from, int to, char c)
  91.     {
  92.         this.from = from;
  93.         this.to = to;
  94.         this.c = c;
  95.         visited = false;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment