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

📅  最后修改于: 2021-05-24 17:43:31             🧑  作者: 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


输出:
Accepted










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