Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. Deterministic Finite Automaton
  2. 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
  3. Conversion to C/C++ or Java Code
  4. 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
  5. #include<iostream>
  6. #include<conio.h>
  7. using namespace std;
  8. int main()
  9. {
  10. string str ;
  11. cout<<"Enter String to Test: "<<endl;
  12. cin>>str;
  13. int state=1; // start state
  14. for(int i=0;i<str.length();i++)
  15. {
  16. char c = str[i];
  17. switch (state) {
  18. case (1): {
  19. if (c == 'n' || c == 'N') {
  20. state = 2;
  21. }
  22. }
  23. break;
  24. case (2): {
  25. if (c == 'a' || c == 'A') {
  26. state = 3;
  27. } else if (c == 'n' || c == 'N') {
  28. state = 2;
  29. }
  30. }
  31. break;
  32. case (3): {
  33. if (c == 'n' || c == 'N') {
  34. state = 4;
  35. } else {
  36. state = 1;
  37. }
  38. }
  39. break;
  40. case (4): {
  41. if (c == 'o' || c == 'O') {
  42. state = 5;
  43. } else if (c == 'n' || c == 'N') {
  44. state = 2;
  45. } else {
  46. state = 1;
  47. }
  48. }
  49. break;
  50. case (5): {
  51. state = 5;
  52. }
  53. break;
  54. }
  55. }
  56. if (state == 5) {
  57. cout<<"DFA Accepts this string"<<endl;
  58. } else {
  59. cout<<"DFA does not accepts this string"<<endl;
  60. }
  61. getch();
  62. return 0;
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement