-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNFA.java
More file actions
81 lines (66 loc) · 2.33 KB
/
NFA.java
File metadata and controls
81 lines (66 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Scanner;
import java.lang.String;
public class NFA {
static Scanner sc = new Scanner(System.in);
static String stateDiagram[][] = {
{ "000010010", "000110111" },
{ "000000010", "000010111" },
{ "000011011", "000010011" },
{ "000010111", "000001000" },
{ "000010000", "000100000" },
{ "001010011", "010000000" },
{ "100010011", "000110111" },
{ "000100000", "001010011" },
{ "010000000", "100000000" }
};
static String startingStates = "000010011";
static String acceptingStates = "001000100";
static String currentState = startingStates;
static boolean accepted = false;
static String or(String a, String b) {
String output = "";
for(int i = 0; i < a.length(); i++) {
output += (Character.getNumericValue(a.charAt(i)) + Character.getNumericValue(b.charAt(i)) > 0) ? "1" : "0";
}
return output;
}
static String and(String a, String b) {
String output = "";
for(int i = 0; i < a.length(); i++) {
output += (a.charAt(i) == b.charAt(i)) ? a.charAt(i) : "0";
}
return output;
}
static boolean compute(String input) {
for(int i = input.length() - 1; i >= 0; i--) {
String tempState = currentState;
currentState = "000000000";
if(input.charAt(i) == '0') {
for(int j = 0; j < tempState.length(); j++) {
currentState = or(currentState, stateDiagram[Character.getNumericValue(tempState.charAt(j))][0]);
}
} else {
for(int j = 0; j < tempState.length(); j++) {
currentState = or(currentState, stateDiagram[Character.getNumericValue(tempState.charAt(j))][1]);
}
}
}
for(int i = 0; i < currentState.length(); i++) {
accepted |= (acceptingStates.charAt(i) == '1') && (currentState.charAt(i) == '1');
}
return accepted;
}
public static void main(String[] args) {
System.out.println("Enter the binary number to process:");
// String input = sc.nextLine();
// String input2 = sc.nextLine();
//
// System.out.println(or(input, input2));
boolean result = compute(sc.nextLine());
if(result) {
System.out.println("ACCEPTED\nThe binary number can be partitioned into substrings such that each substring has value congruent to 1 mod 3 or 2 mod 5.");
} else {
System.out.println("REJECTED\nThe binary number can NOT be partitioned into substrings such that each substring has value congruent to 1 mod 3 or 2 mod 5.");
}
}
}