📌  相关文章
📜  构建一个 DFA 以接受以“01”开头或结尾的二进制字符串

📅  最后修改于: 2021-09-06 06:51:16             🧑  作者: Mango

给定二进制字符串str ,任务是构建一个 DFA,如果字符串以“01”开头或以“01”结尾,则该字符串接受该字符串。

DFA 或确定性有限自动机是一种有限状态机,如果字符串达到最终状态,则接受字符串(在某些特定条件下),否则拒绝它。
在DFA中,没有记忆的概念,因此我们必须通过字符来检查字符串的字符,与第0字符开始。问题的输入字符集是 {0, 1}。为了使 DFA 有效,必须为每个状态的输入集的每个符号定义一个转换规则到有效状态。因此,按照以下步骤来设计 DFA:

  • 在这种情况下,以 01 开头或以 01 结尾或都以 01 开头和 01 结尾的字符串应该是可以接受的。
  • 制作一个初始状态并将其输入字母表,即 0 和 1 转换为两个不同的状态。
  • 在每次转换后检查是否接受字符串以忽略错误。
  • 首先,为最小长度字符串制作 DfA,然后逐步进行。
  • 根据字符串的接受度定义 Final State(s)。

设计 DFA 的分步方法:

  • 步骤 1:使初始状态为“A”。最小可能的字符串是 01,这是可以接受的。为此,将 0 从状态“A”转移到状态“B”,然后将 1 从状态“B”转移到状态“C”,并注意这个状态“C”作为最终状态。

  • 第二步:现在,我们已经设计了以 01 开头的 DFA。要接受所有以 01 开头的字符串,如 011、010、010000、01111、010101000010001 等,我们需要将 0 和 1 的自循环放入状态“C”。这个自循环包含 0 和 1 的所有组合。

  • 第 3 步:现在,我们需要考虑以“01”结尾的字符串。我们已经完成了状态“A”的 0 转换,然后是状态“A”的输入 1。以 01 结尾的最小可能字符串为 101。为此,将状态“A”的输入 1 转换为状态“D”,然后将输入 0 从状态“D”转换为状态“E”,然后然后将输入 1 从状态“E”转换为状态“F”,并注意这个“F”状态是最终状态。

  • 第 4 步:还有一种可能是任意数量的 1 以 01 开头然后以 01 结尾。为此,在状态“D”上进行 1 的自循环,并且任意数量的 0 可以出现在 1 中的 1 之前结尾。为此,将自循环设为 0 并设置为“E”。

  • 第 5 步:到目前为止,我们已经完成了以 1 开头并以 01 结尾的字符串。现在,我们需要考虑以 0 开头并以 01 结尾的字符串。为此,将输入 0 从状态“转换” B”表示“E”。

  • 第 6 步:现在我们只剩下状态“F”的输入字母。将输入 1 从状态“F”转移到状态“D”,然后将输入 0 从状态“F”转移到状态“E”。

上述DFA的Transition表和Transition规则:

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 Classes Live