dscelab

p1

Apr 14th, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. enum State { S0, S1, S2, S3 };
  6.  
  7. bool acceptsString(const string& input) {
  8.     State currentState = S0;
  9.     for (char c : input) {
  10.         switch (currentState) {
  11.             case S0:
  12.                 if (c == '1') currentState = S1;
  13.                 break;
  14.             case S1:
  15.                 if (c == '1') currentState = S2;
  16.                 else currentState = S0;
  17.                 break;
  18.             case S2:
  19.                 if (c == '1') currentState = S3;
  20.                 else currentState = S0;
  21.                 break;
  22.             case S3:
  23.                 break; // stay in S3
  24.         }
  25.     }
  26.     return currentState == S3;
  27. }
  28.  
  29. int main() {
  30.     string input;
  31.     cout << "Enter a binary string: ";
  32.     cin >> input;
  33.     if (acceptsString(input)) {
  34.         cout << "Accepted (contains 111)" << endl;
  35.     } else {
  36.         cout << "Rejected (does not contain 111)" << endl;
  37.     }
  38.     return 0;
  39. }
  40.  
Add Comment
Please, Sign In to add comment