Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int matrix[4][9] = {
  5.     { 1, -1,  1,  -1,   5, -1,   7,   7,   5},
  6.     {-1,  2, -1,  -1,  -1,  6,   6,  -1,  -1},
  7.     { 8, -1,  3,   3,  -1, -1,   7,   6,   3},
  8.     { 0,  0,  0,  -2,   0,  0,  -2,  -2,   0}
  9. };
  10.  
  11. int main() {
  12.     FILE *input = fopen("in.txt","r");
  13.     char currentChar;
  14.     int currentState = 0;
  15.     int validStrCounter = 0;
  16.     int wrongFlag = 0;
  17.     while ( ( currentChar = getc(input) ) != EOF ) {
  18.         switch ( currentChar ) {
  19.             case 'a':
  20.                 currentState = matrix[0][currentState];
  21.             break;
  22.             case 'b':
  23.                 currentState = matrix[1][currentState];
  24.             break;
  25.             case 'c':
  26.                 currentState = matrix[2][currentState];
  27.             break;
  28.             case ' ':
  29.                 wrongFlag = 0;
  30.                 currentState = matrix[3][currentState];
  31.             break;
  32.         }
  33.         if (currentState == -1) {
  34.             currentState = 0;
  35.             wrongFlag = 1;
  36.         }
  37.         if (currentState == -2) {
  38.             if (wrongFlag != 1)
  39.                 validStrCounter++;
  40.             currentState = 0;
  41.         }
  42.     }
  43.     cout << "Number of valid strings = " << validStrCounter << endl;
  44.     fclose(input);
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement