Skip to content

Instantly share code, notes, and snippets.

@tamtom
Created February 17, 2016 06:47
Show Gist options
  • Save tamtom/045263509a350e2e0da8 to your computer and use it in GitHub Desktop.
Save tamtom/045263509a350e2e0da8 to your computer and use it in GitHub Desktop.
import java.util.HashSet;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author omar
*/
public class Dfa {
public static void main(String[] args) {
HashSet<String> mAcceptedStates = new HashSet();
mAcceptedStates.add("s1");
mAcceptedStates.add("s2");
mAcceptedStates.add("s4");
// fill the table
String table[][] = new String[][]{{"s1", "s2", "s4"}, {"s2", "s2", "s3"}, {"s3", "s2", "s3"}, {"s4", "s5", "s4"}, {"s5", "s5", "s4"}};
Scanner in = new Scanner(System.in);
String word = in.next();
int start = 0;
int from = 0, to = 0;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
from = start;
to = c == 'a' ? 1 : 2;
System.out.print("s" + (from + 1) + " --" + c + "-> " + table[from][to] + " ");
if (i % 2 == 0 && i != 0) {
System.out.println("");
}
start = Integer.parseInt(table[from][to].charAt(1) + "") - 1;
}
String endState = table[from][to];
System.out.println("");
System.out.println("result " + endState);
System.out.println(mAcceptedStates.contains(table[from][to]) ? " yes " + endState + " is accepted state" : "no " + endState + " is not accepted state ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment