给定二进制字符串str ,任务是构建一个DFA来接受字符串(如果该字符串以“ 01”开头或以“ 01”结尾)。
Input: str = “010000”
Output: Accepted
Explanation:
The given string starts with “01”.
Input: str = “1100111”
Output: Not Accepted
Explanation:
The given string neither starts with nor ends with “01”.
DFA或确定性有限自动机(DFA)是一种有限状态机,如果达到最终状态则接受字符串(在某些特定条件下),否则将其拒绝。
在DFA中,没有记忆的概念,因此我们必须通过字符来检查字符串的字符,与第0字符开始。问题的输入字符集为{0,1}。为了使DFA有效,必须为每个状态下的输入集的每个符号定义一个转换规则,以转换为有效状态。因此,请按照以下步骤设计DFA:
- 在这种情况下,以01开头或以01结尾或既以01开头又以01结尾的字符串都是可以接受的。
- 设置一个初始状态并将其输入字母(即0和1)转换为两个不同的状态。
- 每次转换后检查字符串是否被接受,以忽略错误。
- 首先,将DfA设置为最小长度的字符串,然后逐步进行操作。
- 根据字符串的接受定义最终状态。
逐步设计DFA的方法:
- 步骤1:将初始状态设为“ A”。可能的最小字符串是01,这是可接受的。为此,从状态“ A”到状态“ B”的转换为0,然后从状态“ B”到状态“ C”的转换为1,并注意该状态“ C”为最终状态。
- 第2步:现在,我们设计了以01开头的DFA。要接受以01开头的所有字符串,例如011、010、010000、01111、010101000010001等,我们需要将0和1的自循环状态“ C”。此自循环包含0和1的所有组合。
- 步骤3:现在,我们需要考虑以“ 01”结尾的字符串。我们已经完成了状态“ A”从0到0的转换,然后是状态“ A”的输入1。在以01结尾的最小可能字符串,值为101。为此,将状态“ A”的输入1转换为状态“ D”,然后将输入0的状态从状态“ D”转换为状态“ E”,并且然后执行输入1从状态“ E”到状态“ F”的转换,并注意该“ F”状态为最终状态。
- 第4步:还有一种可能性,任意数量的1都会以01开始,然后以01结束。为此,请在状态“ D”上进行1的自循环,并且在数量1之前可以有任意数量的零。结尾。为此,将自环设置为0,并将状态设置为“ E”。
- 步骤5:到目前为止,我们已经处理了以1开头,以01结尾的字符串。现在,我们需要考虑以0开头,以01结尾的字符串。为此,请从状态“ B”表示“ E”。
- 步骤6:现在我们只剩下状态“ F”的输入字母。从状态“ F”转换输入1到状态“ D”,然后从状态“ F”转换输入0到状态“ E”。
上述DFA的转换表和转换规则:
State | Input (0) | Input (1) |
---|---|---|
—>A | B | D |
B | E | C |
C* | C | C |
D | E | D |
E | E | F |
F* | E | D |
下面是上述方法的实现:
C++
// C++ program to check if a string
// either starts or ends with 01
#include
using namespace std;
void stateA(string);
void stateB(string);
void stateC(string);
void stateD(string);
void stateE(string);
void stateF(string);
// Function for transition
// state A
void checkstateA(string n)
{
// State transition to
// B if the character is
// 0
if(n[0] == '0')
stateB(n.substr(1));
// State transition to
// D if the character is
// 1
else
stateD(n.substr(1));
}
// Function for transition
// state B
void stateB(string n)
{
// Check if the string has
// ended
if (n.length() == 0)
cout << "string not accepted";
else
{
// State transition to C
// if the character is 1
if(n[0] == '1')
stateC(n.substr(1));
// State transition to D
// if the character is 0
else
stateD(n.substr(1));
}
}
// Function for transition
// state C
void stateC(string n)
{
cout << "String accepted";
}
// Function for transition
// state D
void stateD(string n)
{
if (n.length() == 0)
cout << "string not accepted";
else
{
// State transition to D
// if the character is 1
if (n[0] == '1')
stateD(n.substr(1));
// State transition to E
// if the character is 0
else
stateE(n.substr(1));
}
}
// Function for transition
// state E
void stateE(string n)
{
if (n.length() == 0)
cout << "string not accepted";
else
{
// State transition to E
// if the character is 0
if(n[0] == '0')
stateE(n.substr(1));
// State transition to F
// if the character is 1
else
stateF(n.substr(1));
}
}
// Function for transition
// state F
void stateF(string n)
{
if(n.length() == 0)
cout << "string accepred";
else
{
// State transition to D
// if the character is 1
if(n[0] == '1')
stateD(n.substr(1));
// State transition to E
// if the character is 0
else
stateE(n.substr(1));
}
}
// Driver code
int main()
{
string n = "0100101";
checkstateA(n);
return 0;
}
// This code is contributed by chitranayal
Java
// Java program to check if a string
// either starts or ends with 01
import java.util.*;
class GFG{
// Function for transition
// state A
static void checkstateA(String n)
{
// State transition to
// B if the character is
// 0
if (n.charAt(0) == '0')
stateB(n.substring(1));
// State transition to
// D if the character is
// 1
else
stateD(n.substring(1));
}
// Function for transition
// state B
static void stateB(String n)
{
// Check if the string has
// ended
if (n.length() == 0)
System.out.println("string not accepted");
else
{
// State transition to C
// if the character is 1
if (n.charAt(0) == '1')
stateC(n.substring(1));
// State transition to D
// if the character is 0
else
stateD(n.substring(1));
}
}
// Function for transition
// state C
static void stateC(String n)
{
System.out.println("String accepted");
}
// Function for transition
// state D
static void stateD(String n)
{
if (n.length() == 0)
System.out.println("string not accepted");
else
{
// State transition to D
// if the character is 1
if (n.charAt(0) == '1')
stateD(n.substring(1));
// State transition to E
// if the character is 0
else
stateE(n.substring(1));
}
}
// Function for transition
// state E
static void stateE(String n)
{
if (n.length() == 0)
System.out.println("string not accepted");
else
{
// State transition to E
// if the character is 0
if(n.charAt(0) == '0')
stateE(n.substring(1));
// State transition to F
// if the character is 1
else
stateF(n.substring(1));
}
}
// Function for transition
// state F
static void stateF(String n)
{
if (n.length() == 0)
System.out.println("string accepred");
else
{
// State transition to D
// if the character is 1
if (n.charAt(0) == '1')
stateD(n.substring(1));
// State transition to E
// if the character is 0
else
stateE(n.substring(1));
}
}
// Driver Code
public static void main(String args[])
{
String n = "0100101";
checkstateA(n);
}
}
// This code is contributed by jyoti369
Python3
# Python3 program to check if
# a string either starts or
# ends with 01
# Function for transition
# state A
def checkstateA(n):
# State transition to
# B if the character is
# 0
if(n[0]=='0'):
stateB(n[1:])
# State transition to
# D if the character is
# 1
else:
stateD(n[1:])
# Function for transition
# state B
def stateB(n):
# Check if the string has
# ended
if (len(n)== 0):
print("string not accepted")
else:
# State transition to C
# if the character is 1
if(n[0]=='1'):
stateC(n[1:])
# State transition to D
# if the character is 0
else:
stateD(n[1:])
# Function for transition
# state C
def stateC(n):
print("String accepted")
# Function for transition
# state D
def stateD(n):
if (len(n)== 0):
print("string not accepted")
else:
# State transition to D
# if the character is 1
if (n[0]=='1'):
stateD(n[1:])
# State transition to E
# if the character is 0
else:
stateE(n[1:])
# Function for transition
# state E
def stateE(n):
if (len(n)== 0):
print("string not accepted")
else:
# State transition to E
# if the character is 0
if(n[0]=='0'):
stateE(n[1:])
# State transition to F
# if the character is 1
else:
stateF(n[1:])
# Function for transition
# state F
def stateF(n):
if(len(n)== 0):
print("string accepred")
else:
# State transition to D
# if the character is 1
if(n[0]=='1'):
stateD(n[1:])
# State transition to E
# if the character is 0
else:
stateE(n[1:])
# Driver code
if __name__ == "__main__":
n = "0100101"
checkstateA(n)
C#
// C# program to check if
// a string either starts
// or ends with 01
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
// Function for transition
// state A
static void checkstateA(string n)
{
// State transition to
// B if the character is
// 0
if(n[0] == '0')
stateB(n.Substring(1));
// State transition to
// D if the character is
// 1
else
stateD(n.Substring(1));
}
// Function for transition
// state B
static void stateB(string n)
{
// Check if the string has
// ended
if (n.Length == 0)
{
Console.Write("string not accepted");
}
else
{
// State transition to C
// if the character is 1
if(n[0] == '1')
stateC(n.Substring(1));
// State transition to D
// if the character is 0
else
stateD(n.Substring(1));
}
}
// Function for transition
// state C
static void stateC(string n)
{
Console.Write("string accepted");
}
// Function for transition
// state D
static void stateD(string n)
{
if (n.Length == 0)
Console.Write("string not accepted");
else
{
// State transition to D
// if the character is 1
if (n[0] == '1')
stateD(n.Substring(1));
// State transition to E
// if the character is 0
else
stateE(n.Substring(1));
}
}
// Function for transition
// state E
static void stateE(string n)
{
if (n.Length == 0)
Console.Write("string not accepted");
else
{
// State transition to E
// if the character is 0
if(n[0] == '0')
stateE(n.Substring(1));
// State transition to F
// if the character is 1
else
stateF(n.Substring(1));
}
}
// Function for transition
// state F
static void stateF(string n)
{
if(n.Length == 0)
Console.Write("string accepted");
else
{
// State transition to D
// if the character is 1
if(n[0] == '1')
stateD(n.Substring(1));
// State transition to E
// if the character is 0
else
stateE(n.Substring(1));
}
}
// Driver code
public static void Main(string []args)
{
string n = "0100101";
checkstateA(n);
}
}
// This code is contributed by rutvik_56
Javascript
输出:
String accepted
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。