-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (60 loc) · 1.96 KB
/
Copy pathProgram.cs
File metadata and controls
66 lines (60 loc) · 1.96 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
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fs1
{
class State
{
public string Name;
public Dictionary<char, State> Transitions;
public bool IsAcceptState;
}
class Program
{
public static State a = new State()
{
Name = "a",
IsAcceptState = false,
Transitions = new Dictionary<char, State>()
};
static public State b = new State()
{
Name = "b",
IsAcceptState = false,
Transitions = new Dictionary<char, State>()
};
static public State c = new State()
{
Name = "c",
IsAcceptState = true,
Transitions = new Dictionary<char, State>()
};
static State InitialState = a;
static void Main(string[] args)
{
String s = "1110001";
a.Transitions['0'] = a;
a.Transitions['1'] = b;
b.Transitions['0'] = c;
b.Transitions['1'] = a;
c.Transitions['0'] = b;
c.Transitions['1'] = c;
bool? result = Run(s);
Console.WriteLine(result);
}
static public bool? Run(IEnumerable<char> s)
{
State current = InitialState;
foreach (var c in s) // цикл по всем символам
{
current = current.Transitions[c]; // меняем состояние на то, в которое у нас переход
if (current == null) // если его нет, возвращаем признак ошибки
return null;
// иначе переходим к следующему
}
return current.IsAcceptState; // результат true если в конце финальное состояние
}
}
}