Prerequisite – Introduction of Finite Automata

C++ Program to construct an NFA which accepts the set of strings over an alphabet {0, 1, 2} such that the final digit has appeared before.

**Examples :**

Input :01101Output :AcceptedInput :012Output :Not AcceptedInput :2Output :Not AcceptedInput :0122Output :Accepted

**Explanation: **

In the first example, 01101, the last digit ‘1’ occurred at letter number 2 and 3 of the string. Hence it is accepted. In second example, 012, the occurrence of ‘2’ is only at the last place. Hence it is rejected. Similarly with the third example, 2 is rejected. In the last example, the last digit ‘2’ occured befor end of the string hence it is Accepted.

**Approach: **

- Construct a start state.
- Construct 3 states for input of 0, 1 and 2.
- Repeat the loops in all state for all the inputs.
- Connect all the state with a final state.

**NFA State Transaction Diagram: **

**Implementation :**

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function of state one or starting state ` `void` `q1(string s, ` `int` `pos, ` `int` `len); ` ` ` `// function of state two ` `void` `q2(string s, ` `int` `pos, ` `int` `len); ` ` ` `// function of state three ` `void` `q3(string s, ` `int` `pos, ` `int` `len); ` ` ` `// function of state four ` `void` `q4(string s, ` `int` `pos, ` `int` `len); ` ` ` `// function of state five ` `void` `q5(string s, ` `int` `pos, ` `int` `len); ` ` ` `// See diagram for help ` ` ` `vector<string> states; ` `int` `accepted = 0; ` ` ` `// Uncomment this function and the function calls to see ` `// the path of string from the start state to end state ` `/* ` `void printVector() ` `{ ` ` ` `for (auto i = states.begin(); i != states.end(); i++) ` ` ` `cout << *i << " "; ` ` ` `cout << endl; ` `} ` `*/` `void` `q5(string s, ` `int` `pos, ` `int` `len) ` `{ ` ` ` `states.push_back(` `"Q5->"` `); ` ` ` `if` `(pos == len) { ` ` ` `// printVector(); ` ` ` `accepted = 1; ` ` ` `} ` ` ` `else` `{ ` ` ` `states.push_back(` `"Dead"` `); ` ` ` `// printVector(); ` ` ` `states.pop_back(); ` ` ` `} ` ` ` `states.pop_back(); ` ` ` `return` `; ` `} ` ` ` `void` `q4(string s, ` `int` `pos, ` `int` `len) ` `{ ` ` ` `states.push_back(` `"Q4->"` `); ` ` ` `if` `(pos == len) { ` ` ` `// printVector(); ` ` ` `states.pop_back(); ` ` ` `return` `; ` ` ` `} ` ` ` `if` `(s[pos] == ` `'2'` `) ` ` ` `q5(s, pos + 1, len); ` ` ` `q4(s, pos + 1, len); ` ` ` `states.pop_back(); ` ` ` `return` `; ` `} ` ` ` `void` `q3(string s, ` `int` `pos, ` `int` `len) ` `{ ` ` ` `states.push_back(` `"Q3->"` `); ` ` ` `if` `(pos == len) { ` ` ` `// printVector(); ` ` ` `states.pop_back(); ` ` ` `return` `; ` ` ` `} ` ` ` `if` `(s[pos] == ` `'1'` `) ` ` ` `q5(s, pos + 1, len); ` ` ` `q3(s, pos + 1, len); ` ` ` `states.pop_back(); ` ` ` `return` `; ` `} ` ` ` `void` `q2(string s, ` `int` `pos, ` `int` `len) ` `{ ` ` ` `states.push_back(` `"Q2->"` `); ` ` ` `if` `(pos == len) { ` ` ` `// printVector(); ` ` ` `states.pop_back(); ` ` ` `return` `; ` ` ` `} ` ` ` `if` `(s[pos] == ` `'0'` `) ` ` ` `q5(s, pos + 1, len); ` ` ` `q2(s, pos + 1, len); ` ` ` `states.pop_back(); ` ` ` `return` `; ` `} ` ` ` `void` `q1(string s, ` `int` `pos, ` `int` `len) ` `{ ` ` ` `states.push_back(` `"Q1->"` `); ` ` ` `if` `(pos == len) { ` ` ` `// printVector(); ` ` ` `states.pop_back(); ` ` ` `return` `; ` ` ` `} ` ` ` `if` `(s[pos] == ` `'0'` `) ` ` ` `q2(s, pos + 1, len); ` ` ` `else` `if` `(s[pos] == ` `'1'` `) ` ` ` `q3(s, pos + 1, len); ` ` ` `else` `if` `(s[pos] == ` `'2'` `) ` ` ` `q4(s, pos + 1, len); ` ` ` ` ` `q1(s, pos + 1, len); ` ` ` `states.pop_back(); ` ` ` `return` `; ` `} ` ` ` `int` `main() ` `{ ` ` ` `string s; ` ` ` `// cin >> s; ` ` ` `s = ` `"01101"` `; ` ` ` ` ` `int` `pos = 0; ` ` ` `q1(s, pos, s.length()); ` ` ` ` ` `if` `(accepted) ` ` ` `cout << ` `"Accepted"` `<< endl; ` ` ` `else` ` ` `cout << ` `"Not Accepted"` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- Conversion of Epsilon-NFA to NFA
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Program to build DFA that accepts the languages ending with "01" over the characters {0, 1}
- DFA in LEX code which accepts even number of zeros and even number of ones
- Program to build a DFA that accepts strings starting and ending with different character
- NFA machines accepting all strings that ends or not ends with substring 'ab'
- Design a DFA that accepts a string containing 3 a's and 3 b's
- Conversion from NFA to DFA
- Program to Implement NFA with epsilon move to DFA Conversion
- Hypothesis (language regularity) and algorithm (L-graph to NFA) in TOC
- Regular expression to ∈-NFA
- Difference between DFA and NFA
- NFA for language at least one of the symbols occurring an odd number of times
- Pushdown Automata Acceptance by Final State
- DFA of a string in which 2nd symbol from RHS is 'a'
- DFA of a string in which 3rd symbol from RHS is ‘a’
- Program to construct a DFA which accept the language L = {a
^{n}b^{m}| n mod 2=0, m≥1} - Construct DFA which interpreted as binary number is divisible by 2, 3, 4
- Construct a DFA which accept the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}
- Construct a DFA which accept the language L = {a
^{n}b^{m}| n > =1, (m) mod 3 = 1}

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.