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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
| #include <iostream>
#include <cstdlib>
#include <vector>
#include <map>
// ��������� ��������
typedef enum
{
S1, S2, S3
}
State;
// ��������� ������� ��������
typedef enum
{
Alfa, Beta, Gamma
}
InputSymbol;
// �������� �������� ��������
typedef enum
{
Y1 = 1, Y2 = 4, Y3 = 7
}
OutputSymbol;
// ���������� ��� ���� (���������, ������� ������)
typedef std::pair<State, InputSymbol> InputPair;
// ���������� ��� ���� (���������, �������� ������)
typedef std::pair<State, OutputSymbol> OutputPair;
// ����� ��������
class StateMachine
{
public:
// ��� �������� ����� �������� ��������� ���������
StateMachine(const State &state)
{
// ������ ��������� ���������
this->state = state;
// �������� �������� ��������
this->counter = 0;
// �������� ������� ���������
this->sigma = {
{{State::S1, InputSymbol::Alfa}, {State::S3, OutputSymbol::Y3}},
{{State::S1, InputSymbol::Beta}, {State::S2, OutputSymbol::Y2}},
{{State::S1, InputSymbol::Gamma}, {State::S1, OutputSymbol::Y1}},
{{State::S2, InputSymbol::Alfa}, {State::S3, OutputSymbol::Y2}},
{{State::S2, InputSymbol::Beta}, {State::S3, OutputSymbol::Y2}},
{{State::S2, InputSymbol::Gamma}, {State::S1, OutputSymbol::Y1}},
{{State::S3, InputSymbol::Alfa}, {State::S3, OutputSymbol::Y3}},
{{State::S3, InputSymbol::Beta}, {State::S3, OutputSymbol::Y1}},
{{State::S3, InputSymbol::Gamma}, {State::S1, OutputSymbol::Y1}}
};
}
// ��������� �� ���� �������� ����� word
void process(const std::vector<InputSymbol> &word)
{
for (int i = 0; i < word.size(); ++i)
{
// ������������ ������ ������ �������� ����� ����������
process(word[i]);
}
}
// ���������� ����� �� �������
const int &getCounter() const
{
return this->counter;
}
private:
void process(const InputSymbol& input)
{
// ���������� ������� � ������� ���������
OutputPair out = this->sigma[{this->state, input}];
// ��������� ��������� ��������
this->state = out.first;
// ��������� ������� �������
if (out.first == State::S1 && out.second == OutputSymbol::Y1)
++this->counter;
}
// ���������� ��� ������� ������� ���������
// � ��������� S1 � ���� �������� ������ Y1
int counter;
// ������� ��������� ��������
State state;
// ������� ���������
std::map<InputPair, OutputPair> sigma;
};
int main()
{
// ���������� ���������� ���������
int state,
// ���������� ���������� ���������� ��������
r;
// ���������� ���������� �������� �����
std::vector<InputSymbol> word;
// ��������� ��������� ��������� �������� � ���������� ��������
// �� ������� �����
std::cin >> state >> r;
while (r--)
{
// ���������� ��������� ���������� ��� ���������� �������� �������
int tmp;
// ��������� ������� ������
std::cin >> tmp;
// ���������� ��� � �����
word.push_back((InputSymbol) tmp);
}
// �������� ������� ��������
StateMachine machine((State) state);
// ������ ��������
machine.process(word);
// ����� ����������
std::cout << machine.getCounter();
return EXIT_SUCCESS;
} |