Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Deterministic Finite Automaton
- A deterministic finite automaton (DFA) also known as deterministic finite state machine , is a finite state machine that accepts/rejects finite strings of symbols and only produces a unique computation (or run) of the automaton for each input string. Source: Wikipedia
- Conversion to C/C++ or Java Code
- we can easily convert any DFA to Code , here is the machine that will accept string that contain "nano" sub string and will reject all other strings
- #include<iostream>
- #include<conio.h>
- using namespace std;
- int main()
- {
- string str ;
- cout<<"Enter String to Test: "<<endl;
- cin>>str;
- int state=1; // start state
- for(int i=0;i<str.length();i++)
- {
- char c = str[i];
- switch (state) {
- case (1): {
- if (c == 'n' || c == 'N') {
- state = 2;
- }
- }
- break;
- case (2): {
- if (c == 'a' || c == 'A') {
- state = 3;
- } else if (c == 'n' || c == 'N') {
- state = 2;
- }
- }
- break;
- case (3): {
- if (c == 'n' || c == 'N') {
- state = 4;
- } else {
- state = 1;
- }
- }
- break;
- case (4): {
- if (c == 'o' || c == 'O') {
- state = 5;
- } else if (c == 'n' || c == 'N') {
- state = 2;
- } else {
- state = 1;
- }
- }
- break;
- case (5): {
- state = 5;
- }
- break;
- }
- }
- if (state == 5) {
- cout<<"DFA Accepts this string"<<endl;
- } else {
- cout<<"DFA does not accepts this string"<<endl;
- }
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement