📌  相关文章
📜  构建DFA以接受包含“ i”次“ 01”和“ 2j次” 1”的二进制字符串

📅  最后修改于: 2021-05-06 07:39:12             🧑  作者: Mango

给定一个二进制字符串str ,任务是构建一个DFA,以接受给定的二进制字符串(如果它包含i的“ 01”倍和2j的“ 1”倍),即

L={(01)^i (1)^{2j} \text{ where }i\geq1\text{ and }j\geq1}

例子:

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

  1. 创建初始阶段,并将0和1转换为下一个可能的状态。
  2. 转换0总是跟随转换1。
  3. 设置一个初始状态并将其输入字母(即0和1)转换为两个不同的状态。
  4. 每次转换后检查字符串是否被接受,以忽略错误。
  5. 首先,将DfA设置为最小长度的字符串,然后逐步进行操作。
  6. 根据字符串的接受定义最终状态。

逐步设计DFA的方法:

  • 第1步:可接受的最小字符串为0111,即(01) 1 (11) 1 。因此,创建一个初始状态“ A”,该状态将0转换为状态“ B”,然后将1从“ B”转换为状态“ C”,然后将1从“ C”转换为“ D”,然后转换为1如图所示,从“ D”到“ E”使此阶段“ E”为最终状态。

  • 步骤2:现在,考虑具有连续(01)的字符串,然后是连续(11)的字符串以结束字符串。因此,当i> 1时,从状态“ C”到状态“ B”为“ 0”,从状态“ E”到状态“ D”为“ 1”。因此,现在可以接受诸如010111、011111、0101111111等的字符串。

  • 步骤3:我们已经完成了所有可能接受的字符串的处理。但是,很少有输入字母不会转换到任何状态。在这种情况下,所有这些类型的输入都将被发送到某些无效状态,以阻止其进一步的不可接受的转换。无效状态的输入字母将发送到无效状态本身。因此,DFA的最终设计是:

下面是上述方法的实现:

Java
// Java code for the above DFA
import java.util.*;
 
class GFG{
   
// Function for the state A
static void checkstatea(String n)
{
  if (n.length() % 2 != 0 ||
      n.length() < 4)
    System.out.print("string not accepted");
  else
  {   
    int i = 0;
     
    // State transition to B
    // if the character is 0
    if (n.charAt(i) == '0')
      stateb(n.substring(1));
    else
      System.out.print("string not accepted");
  }
}
  
// Function for the state B
static void stateb(String n)
{
  int i = 0;
   
  if (n.charAt(i) == '0')
    System.out.print("string not accepted");
  
  // State transition to C
  // if the character is 1
  else
    statec(n.substring(1));
}
   
// Function for the state C
static void statec(String n)
{
  int i = 0;
   
  // State transition to D
  // if the character is 1
  if (n.charAt(i) == '1')
    stated(n.substring(1));
  
  // State transition to B
  // if the character is 0
  else
    stateb(n.substring(1));
}
  
// Function for the state D
static void stated(String n)
{
  int i = 0;
   
  if (n.length() == 1)
  {
    if (n.charAt(i) == '1')
      System.out.print("string accepted");
    else
      System.out.print("string not accepted");
  }
  else
  {
     
    // State transition to E
    // if the character is 1
    if (n.charAt(i) == '1')
      statee(n.substring(1));
    else
      System.out.print("string not accepted");
  }
}
  
// Function for the state E    
static void statee(String n)
{
  int i = 0;
   
  if (n.length() == 1)
  {
    if (n.charAt(i) == '0')
      System.out.print("string not accepted");
    else
      System.out.print("string accepted");
  }
  else
  {
    if (n.charAt(i) == '0')
      System.out.print("string not accepted");
     
    stated(n.substring(1));
  }
}
      
// Driver code
public static void main(String []args)
{
   
  // Take string input
  String n ="011111";
  
  // Call stateA to check the input
  checkstatea(n);
}
}
 
// This code is contributed by pratham76


Python3
# Python3 program for the given
# language
 
# Function for the state A
def checkstatea(n):
    if(len(n)%2!=0 or len(n)<4):
        print("string not accepted")
    else:   
        i=0
 
        # State transition to B
        # if the character is 0
        if(n[i]=='0'):
            stateb(n[1:])
        else:
            print("string not accepted")
 
# Function for the state B
def stateb(n):
    i=0
    if(n[i]=='0'):
        print("string not accepted")
 
    # State transition to C
    # if the character is 1
    else:
        statec(n[1:])
 
# Function for the state C
def statec(n):
    i=0
 
    # State transition to D
    # if the character is 1
    if(n[i]=='1'):
        stated(n[1:])
 
    # State transition to B
    # if the character is 0
    else:
        stateb(n[1:])
 
# Function for the state D
def stated(n):
    i=0
    if(len(n)==1):
        if(n[i]=='1'):
            print("string accepted")
        else:
            print("string not accepted")
    else:
 
        # State transition to E
        # if the character is 1
        if(n[i]=='1'):
            statee(n[1:])
        else:
            print("string not accepted")  
 
# Function for the state E    
def statee(n):
    i=0
    if(len(n)==1):
        if(n[i]=='0'):
            print("string not accepted")
        else:
            print("string accepted")
          
    else:
        if(n[i]=='0'):
            print("string not accepted")
        stated(n[1:])
      
      
# Driver code
if __name__ == "__main__":
 
    n = "011111"
    checkstatea(n)


C#
// C# code for the above DFA
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
     
// Function for the state A
static void checkstatea(string n)
{
  if(n.Length % 2 != 0 ||
     n.Length < 4)
    Console.Write("string not accepted");
  else
  {   
    int i = 0;
 
    // State transition to B
    // if the character is 0
    if(n[i] == '0')
      stateb(n.Substring(1));
    else
      Console.Write("string not accepted");
  }
}
 
// Function for the state B
static void stateb(string n)
{
  int i = 0;
  if(n[i] == '0')
    Console.Write("string not accepted");
 
  // State transition to C
  // if the character is 1
  else
    statec(n.Substring(1));
}
  
// Function for the state C
static void statec(string n)
{
  int i = 0;
 
  // State transition to D
  // if the character is 1
  if(n[i] == '1')
    stated(n.Substring(1));
 
  // State transition to B
  // if the character is 0
  else
    stateb(n.Substring(1));
}
 
// Function for the state D
static void stated(string n)
{
  int i = 0;
  if(n.Length == 1)
  {
    if(n[i] == '1')
      Console.Write("string accepted");
    else
      Console.Write("string not accepted");
  }
  else
  {
    // State transition to E
    // if the character is 1
    if(n[i] == '1')
      statee(n.Substring(1));
    else
      Console.Write("string not accepted");
  }
}
 
// Function for the state E    
static void statee(string n)
{
  int i = 0;
  if(n.Length == 1)
  {
    if(n[i] == '0')
      Console.Write("string not accepted");
    else
      Console.Write("string accepted");
  }
  else
  {
    if(n[i] == '0')
      Console.Write("string not accepted");
    stated(n.Substring(1));
  }
}
     
// Driver code
public static void Main(string []args)
{
  // Take string input
  string n ="011111";
 
  // Call stateA to check the input
  checkstatea(n);
}
}
 
// This code is contributed by rutvik_56


输出:
string accepted