Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int matrix[4][9] = {
- { 1, -1, 1, -1, 5, -1, 7, 7, 5},
- {-1, 2, -1, -1, -1, 6, 6, -1, -1},
- { 8, -1, 3, 3, -1, -1, 7, 6, 3},
- { 0, 0, 0, -2, 0, 0, -2, -2, 0}
- };
- int main() {
- FILE *input = fopen("in.txt","r");
- char currentChar;
- int currentState = 0;
- int validStrCounter = 0;
- int wrongFlag = 0;
- while ( ( currentChar = getc(input) ) != EOF ) {
- switch ( currentChar ) {
- case 'a':
- currentState = matrix[0][currentState];
- break;
- case 'b':
- currentState = matrix[1][currentState];
- break;
- case 'c':
- currentState = matrix[2][currentState];
- break;
- case ' ':
- wrongFlag = 0;
- currentState = matrix[3][currentState];
- break;
- }
- if (currentState == -1) {
- currentState = 0;
- wrongFlag = 1;
- }
- if (currentState == -2) {
- if (wrongFlag != 1)
- validStrCounter++;
- currentState = 0;
- }
- }
- cout << "Number of valid strings = " << validStrCounter << endl;
- fclose(input);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement