📜  可以识别数字0的DFA是输入{0,1}的3的倍数

📅  最后修改于: 2021-08-27 05:15:47             🧑  作者: Mango

有限自动机称为有限状态机,可以接受,否则不可接受。在输入字母“ 0”和“ 1”上。

  • 确定初始状态。
  • 转换发生在每个输入字母上。
  • 确定是否应该应用自循环。
  • 马克的最终状态。

逐步设计DFA:
步骤1:
将初始状态设置为“ A”,则字符串中可能不会有任何“ 0”,而只有“ 1”是可以接受的,因为0被3整除。因此,在这种情况下,可以将1的罐头数目设为出现在此处,为此将“ 1”的自环置于初始状态“ A”。

第2步:
创建输入字母“ 0”从状态“ A”到状态“ B”的过渡。

步骤3:
在一个“ 0”之后,可以存在任意数量的1,即不存在“ 1”或一个以上的“ 1”。为此,将“ 1”的自循环置于状态“ B”。

第四步:
现在,创建输入字母“ 0”从状态“ B”到状态“ C”的转换,并且在字符串中找到两个0之后,可以在字符串找到任意数量的1,为此,将自身循环“ 1”放置在初始状态“ C”上。

步骤5:
在第三个“ 0”转换之前,我们需要考虑一下逻辑,以便在此转换之后,机器将接受具有被3整除的零数的字符串。对于从状态“ C”到状态“ A”的转换“ o”。由于第三个零到达状态“ A”,因此使状态“ A”成为最终状态。

以上DFA的转换表:

States Input (0) Input (1)
—> A * B A
B C B
C A C

在上表中,->代表初始状态,*代表最终状态。在本文中,初始状态和最终状态是相同的,即最终状态。
以上DFA的转换规则:

实施:

Java
// Java code for the above DFA
import java.util.*;
 
class GFG{
      
// Function for the state A
static void checkStateA(String n)
{
     
    // Check length of n
    // is 0 then print
    // String accepted
    if (n.length() == 0)
        System.out.print("String accepted");
     
    // If 1 is found call function
    // checkStateA otherwise if 0
    // is found call function stateB
    else
    {
        if (n.charAt(0) == '1')
            checkStateA(n.substring(1));
        else
            stateB(n.substring(1));
    }
}
   
// Function for the state B
static void stateB(String n)
{
     
    // Check length of n
    // is 0 then print
    // String not accepted
    if (n.length() == 0)
        System.out.print("String not accepted");
     
    // If 1 is found call function
    // stateB otherwise if 0
    // is found call function stateC   
    else
    {
        if (n.charAt(0) == '1')
            stateB(n.substring(1));
        else
            stateC(n.substring(1));
    }
}
  
// Function for the state C
static void stateC(String n)
{
     
    // Check length of n
    // is 0 then print
    // String not accepted
    if (n.length() == 0)
        System.out.print("String not accepted");
     
    // If 1 is found call function
    // stateC otherwise if 0
    // is found call function checkStateA
    else
    {
        if (n.charAt(0) == '1')
            stateC(n.substring(1));
        else
            checkStateA(n.substring(1));
    }
}
  
// Driver code
public static void main(String []args)
{
    Scanner sc = new Scanner(System.in);
     
    // Take String input
    String n = sc.nextLine();
     
    // Call checkStateA to
    // check the inputted String
    checkStateA(n);
}
}
 
// This code is contributed by pratham76


Python3
# Python3 code for the above DFA
def checkStateA(n):
     
    # check length of n
    # is 0 then print
    # string accepted
    if(len(n)== 0):
        print("string accepted")
         
    # if 1 is found call function
    # checkStateA otherwise if 0
    # is found call function stateB
    else:   
        if(n[0]=='1'):
            checkStateA(n[1:])
        else:
            stateB(n[1:])
  
  
def stateB(n):
     
    # check length of n
    # is 0 then print
    # string not accepted
    if(len(n)== 0):
        print("string not accepted")
         
    # if 1 is found call function
    # stateB otherwise if 0
    # is found call function stateC   
    else:   
        if(n[0]=='1'):
            stateB(n[1:])
        else:
            stateC(n[1:])
        
         
def stateC(n):
     
    # check length of n
    # is 0 then print
    # string not accepted
    if(len(n)== 0):
        print("string not accepted")
         
    # if 1 is found call function
    # stateC otherwise if 0
    # is found call function checkStateA
    else:   
        if(n[0]=='1'):
            stateC(n[1:])
        else:
            checkStateA(n[1:])
         
# take string input
n = input()
 
# call checkStateA
# to check the inputted string
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)
{
  // check length of n
  // is 0 then print
  // string accepted
  if(n.Length == 0)
    Console.Write("string accepted");
 
  // if 1 is found call function
  // checkStateA otherwise if 0
  // is found call function stateB
  else
  {
    if(n[0] == '1')
      checkStateA(n.Substring(1));
    else
      stateB(n.Substring(1));
  }
}
  
// Function for the state B
static void stateB(string n)
{
  // check length of n
  // is 0 then print
  // string not accepted
  if(n.Length == 0)
    Console.Write("string not accepted");
 
  // if 1 is found call function
  // stateB otherwise if 0
  // is found call function stateC   
  else{
    if(n[0] == '1')
      stateB(n.Substring(1));
    else
      stateC(n.Substring(1));
  }
}
 
// Function for the state C
static void stateC(string n)
{
  // check length of n
  // is 0 then print
  // string not accepted
  if(n.Length == 0)
    Console.Write("string not accepted");
 
  // if 1 is found call function
  // stateC otherwise if 0
  // is found call function checkStateA
  else
  {
    if(n[0] == '1')
      stateC(n.Substring(1));
    else
      checkStateA(n.Substring(1));
  }
}
 
// Driver code
public static void Main(string []args)
{
  // take string input
  string n = Console.ReadLine();
 
  // call checkStateA
  // to check the inputted string
  checkStateA(n);
}
}
 
// This code is contributed by rutvik_56