-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_Task.cpp
More file actions
56 lines (48 loc) · 1.11 KB
/
assignment_Task.cpp
File metadata and controls
56 lines (48 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
using namespace std;
bool match_RE(string s) {
int n = s.length();
int i = 0;
// a(a*+b*)ba*
// Must start with 'a'
if (i >= n || s[i] != 'a') return false;
i++;
// (a*+b*) => zero or more a's OR zero or more b's
// Try matching a* first
int save = i;
bool matched = false;
// Try a*
int j = i;
while (j < n && s[j] == 'a') j++;
// then must have 'b'
if (j < n && s[j] == 'b') {
int k = j + 1;
// then a*
while (k < n && s[k] == 'a') k++;
if (k == n) matched = true;
}
if (matched) return true;
// Try b*
j = i;
while (j < n && s[j] == 'b') j++;
// then must have 'b'
if (j < n && s[j] == 'b') {
int k = j + 1;
// then a*
while (k < n && s[k] == 'a') k++;
if (k == n) return true;
}
return false;
}
int main() {
string s;
cout << "Enter a string: ";
cin >> s;
cout << "Checking string..." << endl;
if (match_RE(s))
cout << "Accepted" << endl;
else
cout << "Rejected" << endl;
return 0;
}