📜  构造接受奇数个 0 和奇数个 1 的 DFA 的程序

📅  最后修改于: 2021-09-06 11:28:44             🧑  作者: Mango

给定一个二进制字符串S ,任务是为 DFA Machine 编写一个程序,该程序接受一个奇数为01的字符串。

例子:

方法:下面是针对给定问题设计的 DFA 机器。为 DFA 状态构建一个转换表并分析每个状态之间的转换。以下是步骤:

  • 有 4 个状态q 0 , q 1 , q 2 , q 3其中q 0是初始状态, q 3是最终状态。
  • 上述DFA的转换表如下:
Current state Final state
0 1
        q0 q1 q2
        q1 q0 q3
        q2 q3 q0
        q3 q2 q1
  • 通过此表,了解 DFA 中的转换。
  • 如果在读取整个字符串后达到最终状态( q 3 ),则该字符串被接受,否则不被接受。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check whether the given
// string is accepted by DFA or not
void checkValidDFA(string s)
{
    // Stores initial state of DFA
    int initial_state = 0;
 
    // Stores final state of DFA
    int final_state;
 
    // Stores previous state of DFA
    int previous_state = 0;
 
    // Iterate through the string
    for (int i = 0; i < s.length(); i++) {
 
        // Checking for all combinations
        if ((s[i] == '0'
             && previous_state == 0)
            || (s[i] == '1'
                && previous_state == 3)) {
            final_state = 1;
        }
        else if ((s[i] == '0'
                  && previous_state == 3)
                 || (s[i] == '1'
                     && previous_state == 0)) {
            final_state = 2;
        }
        else if ((s[i] == '0'
                  && previous_state == 1)
                 || (s[i] == '1'
                     && previous_state == 2)) {
            final_state = 0;
        }
        else if ((s[i] == '0'
                  && previous_state == 2)
                 || (s[i] == '1'
                     && previous_state == 1)) {
            final_state = 3;
        }
 
        // Update the previous_state
        previous_state = final_state;
    }
 
    // If final state is reached
    if (final_state == 3) {
        cout << "Accepted" << endl;
    }
 
    // Otherwise
    else {
        cout << "Not Accepted" << endl;
    }
}
 
// Driver Code
int main()
{
    // Given string
    string s = "010011";
 
    // Function Call
    checkValidDFA(s);
 
    return 0;
}


Python3
# Python3 program for the above approach
 
# Function to check whether the given
# is accepted by DFA or not
def checkValidDFA(s):
     
    # Stores initial state of DFA
    initial_state = 0
 
    # Stores final state of DFA
    final_state = 0
 
    # Stores previous state of DFA
    previous_state = 0
 
    # Iterate through the string
    for i in range(len(s)):
         
        # Checking for all combinations
        if ((s[i] == '0' and previous_state == 0) or
            (s[i] == '1' and previous_state == 3)):
            final_state = 1
        elif ((s[i] == '0' and previous_state == 3) or
              (s[i] == '1' and previous_state == 0)):
            final_state = 2
        elif ((s[i] == '0' and previous_state == 1) or
              (s[i] == '1' and previous_state == 2)):
            final_state = 0
        elif ((s[i] == '0' and previous_state == 2) or
              (s[i] == '1' and previous_state == 1)):
            final_state = 3
 
        # Update the previous_state
        previous_state = final_state
 
    # If final state is reached
    if (final_state == 3):
        print("Accepted")
         
    # Otherwise
    else:
        print("Not Accepted")
 
# Driver Code
if __name__ == '__main__':
     
    # Given string
    s = "010011"
 
    # Function Call
    checkValidDFA(s)
 
# This code is contributed by mohit kumar 29


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
   
// Function to check whether the given
// string is accepted by DFA or not
static void checkValidDFA(String s)
{
     
    // Stores initial state of DFA
    int initial_state = 0;
 
    // Stores final state of DFA
    int final_state = 0;
 
    // Stores previous state of DFA
    int previous_state = 0;
 
    // Iterate through the string
    for(int i = 0; i < s.length(); i++)
    {
         
        // Checking for all combinations
        if ((s.charAt(i) == '0' && previous_state == 0) ||
            (s.charAt(i) == '1' && previous_state == 3))
        {
            final_state = 1;
        }
        else if ((s.charAt(i) == '0' && previous_state == 3) ||
                 (s.charAt(i) == '1' && previous_state == 0))
        {
            final_state = 2;
        }
        else if ((s.charAt(i) == '0' && previous_state == 1) ||
                 (s.charAt(i) == '1' && previous_state == 2))
        {
            final_state = 0;
        }
        else if ((s.charAt(i) == '0' && previous_state == 2) ||
                 (s.charAt(i) == '1' && previous_state == 1))
        {
            final_state = 3;
        }
 
        // Update the previous_state
        previous_state = final_state;
    }
 
    // If final state is reached
    if (final_state == 3)
    {
        System.out.println("Accepted");
    }
 
    // Otherwise
    else
    {
        System.out.println("Not Accepted");
    }
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given string
    String s = "010011";
 
    // Function Call
    checkValidDFA(s);
}
}
 
// This code is contributed by bgangwar59


C#
// C# program for the above approach
using System;
  
class GFG{
     
// Function to check whether the given
// string is accepted by DFA or not
static void checkValidDFA(string s)
{
     
    // Stores initial state of DFA
    //int initial_state = 0;
  
    // Stores final state of DFA
    int final_state = 0;
  
    // Stores previous state of DFA
    int previous_state = 0;
  
    // Iterate through the string
    for(int i = 0; i < s.Length; i++)
    {
         
        // Checking for all combinations
        if ((s[i] == '0' && previous_state == 0) ||
            (s[i] == '1' && previous_state == 3))
        {
            final_state = 1;
        }
        else if ((s[i] == '0' && previous_state == 3) ||
                 (s[i] == '1' && previous_state == 0))
        {
            final_state = 2;
        }
        else if ((s[i] == '0' && previous_state == 1) ||
                 (s[i] == '1' && previous_state == 2))
        {
            final_state = 0;
        }
        else if ((s[i] == '0' && previous_state == 2) ||
                 (s[i] == '1' && previous_state == 1))
        {
            final_state = 3;
        }
  
        // Update the previous_state
        previous_state = final_state;
    }
  
    // If final state is reached
    if (final_state == 3)
    {
        Console.WriteLine("Accepted");
    }
  
    // Otherwise
    else
    {
        Console.WriteLine("Not Accepted");
    }
}
  
// Driver Code
public static void Main()
{
     
    // Given string
    string s = "010011";
  
    // Function Call
    checkValidDFA(s);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
Accepted

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live