using
System;
using
System.Collections.Generic;
namespace
FiniteAutomata
{
class
Program
{
static
void
Main(
string
[] args)
{
string
s =
"01101"
;
int
pos = 0;
List<
string
> states =
new
List<
string
>();
int
accepted = 0;
Q1(s, pos, s.Length, states,
ref
accepted);
if
(accepted == 1)
Console.WriteLine(
"Accepted"
);
else
Console.WriteLine(
"Not Accepted"
);
}
static
void
Q1(
string
s,
int
pos,
int
len, List<
string
> states,
ref
int
accepted)
{
states.Add(
"Q1->"
);
if
(pos == len)
{
states.RemoveAt(states.Count - 1);
return
;
}
if
(s[pos] ==
'0'
)
Q2(s, pos + 1, len, states,
ref
accepted);
else
if
(s[pos] ==
'1'
)
Q3(s, pos + 1, len, states,
ref
accepted);
else
if
(s[pos] ==
'2'
)
Q4(s, pos + 1, len, states,
ref
accepted);
Q1(s, pos + 1, len, states,
ref
accepted);
states.RemoveAt(states.Count - 1);
}
static
void
Q2(
string
s,
int
pos,
int
len, List<
string
> states,
ref
int
accepted)
{
states.Add(
"Q2->"
);
if
(pos == len)
{
states.RemoveAt(states.Count - 1);
return
;
}
if
(s[pos] ==
'0'
)
Q5(s, pos + 1, len, states,
ref
accepted);
Q2(s, pos + 1, len, states,
ref
accepted);
states.RemoveAt(states.Count - 1);
}
static
void
Q3(
string
s,
int
pos,
int
len, List<
string
> states,
ref
int
accepted)
{
states.Add(
"Q3->"
);
if
(pos == len)
{
states.RemoveAt(states.Count - 1);
return
;
}
if
(s[pos] ==
'1'
)
Q5(s, pos + 1, len, states,
ref
accepted);
Q3(s, pos + 1, len, states,
ref
accepted);
states.RemoveAt(states.Count - 1);
}
static
void
Q4(
string
s,
int
pos,
int
len, List<
string
> states,
ref
int
accepted)
{
states.Add(
"Q4->"
);
if
(pos == len)
{
states.RemoveAt(states.Count - 1);
return
;
}
if
(s[pos] ==
'2'
)
Q5(s, pos + 1, len, states,
ref
accepted);
Q4(s, pos + 1, len, states,
ref
accepted);
states.RemoveAt(states.Count - 1);
}
static
void
Q5(
string
s,
int
pos,
int
len, List<
string
> states,
ref
int
accepted)
{
states.Add(
"Q5->"
);
if
(pos == len)
{
accepted = 1;
}
else
{
states.Add(
"Dead"
);
states.RemoveAt(states.Count - 1);
}
states.RemoveAt(states.Count - 1);
return
;
}
}
}