Skip to content

Commit 24a1f02

Browse files
committed
add: tutorial 3
1 parent 90ae827 commit 24a1f02

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

Tutorial 3/1.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
#include <stdio.h>
3+
4+
using namespace std;
5+
6+
int dfa[][2] = {
7+
{-1, 1},
8+
{2, 1},
9+
{2, 3},
10+
{3, 4},
11+
{-1, 4}
12+
};
13+
14+
int main()
15+
{
16+
// freopen("input", "r", stdin);
17+
string s;
18+
cin >> s;
19+
int n = s.length();
20+
21+
int state = 0, index = 0, input;
22+
bool accepted = false;
23+
24+
while ( index < n )
25+
{
26+
input = s[index] - '0';
27+
state = dfa[state][input];
28+
if ( state < 0 )
29+
{
30+
accepted = false;
31+
break;
32+
}
33+
34+
if ( state == 3 || state == 4 )
35+
accepted = true;
36+
else
37+
accepted = false;
38+
index++;
39+
}
40+
41+
if ( accepted )
42+
cout << "Input " << s << " is ACCEPTED";
43+
else
44+
cout << "Input " << s <<" is not ACCEPTED";
45+
}

Tutorial 3/2.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <stdio.h>
3+
4+
using namespace std;
5+
6+
int dfa[][2] = {
7+
{1, 0},
8+
{1, 2},
9+
{2, 2}
10+
};
11+
12+
int main()
13+
{
14+
freopen("input", "r", stdin);
15+
string s;
16+
cin >> s;
17+
int n = s.length();
18+
19+
int state = 0;
20+
int index = 0, input;
21+
bool accepted = false;
22+
23+
while ( index < n )
24+
{
25+
input = s[index] - '0';
26+
state = dfa[state][input];
27+
if ( state < 0 )
28+
{
29+
accepted = false;
30+
break;
31+
}
32+
33+
if ( state == 2 )
34+
accepted = true;
35+
else
36+
accepted = false;
37+
index++;
38+
}
39+
40+
if ( accepted )
41+
cout << "Input " << s << " is ACCEPTED";
42+
else
43+
cout << "Input " << s <<" is not ACCEPTED";
44+
}

Tutorial 3/3.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
#include <stdio.h>
3+
4+
using namespace std;
5+
6+
int dfa[][2] = {
7+
{0, 1},
8+
{0, 2},
9+
{0, 2}
10+
};
11+
12+
13+
int main()
14+
{
15+
freopen("input", "r", stdin);
16+
string s;
17+
cin >> s;
18+
int n = s.length();
19+
20+
int state = 0;
21+
int index = 0, input;
22+
bool accepted = false;
23+
24+
while ( index < n )
25+
{
26+
input = s[index] - 'a';
27+
state = dfa[state][input];
28+
if ( state < 0 )
29+
{
30+
accepted = false;
31+
break;
32+
}
33+
34+
if ( state == 2 )
35+
accepted = true;
36+
else
37+
accepted = false;
38+
index++;
39+
}
40+
41+
if ( accepted )
42+
cout << "Input " << s << " is ACCEPTED";
43+
else
44+
cout << "Input " << s <<" is not ACCEPTED";
45+
}

Tutorial 3/input

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bb

0 commit comments

Comments
 (0)