📌  相关文章
📜  用于构建DFA的程序,该程序检查字符串是否以“ 01”或“ 10”结尾

📅  最后修改于: 2021-04-27 17:38:20             🧑  作者: Mango

DFA或确定性有限自动机是一种有限状态机,如果达到最终状态,则该字符串会接受字符串(在某些特定条件下),否则将其拒绝。

问题给定“0′” 1′‘01’或‘10’别人拒绝的字符串。不论是否接受,也要打印状态图。由于在DFA中没有内存的概念,因此我们一次只能检查一个字符,从第0个字符开始。此问题的输入集为{0,1} 。对于输入集中的每个字符,DFA的每个状态都会重定向到另一个有效状态。

DFA机器对于以上问题陈述,我们必须首先构建DFA机器。 DFA机器类似于具有各种状态和转换的流程图。与上述问题相对应的DFA机器如下所示, Q3和Q4为最终状态
DFA图片

例子:

Input: 010101
Output:
State transitions are q0->q1->q3->q4
->q3->q4->q3->YES

Explanation : 010101 ends with "01".

Input: 0100
Output:
State transitions are q0->q1->q3->q4->q1->NO
Explanation : 0100 ends with "00", 
which is not equal to any of "01" or "10".

算法:

对于给定的DFA计算机:

执行:

C++
// CPP Program to DFA that accepts string ending
// with 01 or 10.
  
#include 
using namespace std;
  
// Various states of DFA machine are defined
// using functions.
void q1(string, int);
void q2(string, int);
void q3(string, int);
void q4(string, int);
  
// End position is checked using the string
// length value.
// q0 is the starting state.
// q1 and q2 are intermediate states.
// q3 and q4 are final states.
void q1(string s, int i)
{
    cout << "q1->";
  
    if (i == s.length()) {
        cout << "NO \n";
        return;
    }
  
    // state transitions
    // 0 takes to q1, 1 takes to q3
    if (s[i] == '0')
        q1(s, i + 1);
    else
        q3(s, i + 1);
}
  
void q2(string s, int i)
{
    cout << "q2->";
    if (i == s.length()) {
        cout << "NO \n";
        return;
    }
  
    // state transitions
    // 0 takes to q4, 1 takes to q2
    if (s[i] == '0')
        q4(s, i + 1);
    else
        q2(s, i + 1);
}
  
void q3(string s, int i)
{
    cout << "q3->";
    if (i == s.length()) {
        cout << "YES \n";
        return;
    }
  
    // state transitions
    // 0 takes to q4, 1 takes to q2
    if (s[i] == '0')
        q4(s, i + 1);
    else
        q2(s, i + 1);
}
  
void q4(string s, int i)
{
    cout << "q4->";
    if (i == s.length()) {
        cout << "YES \n";
        return;
    }
  
    // state transitions
    // 0 takes to q1, 1 takes to q3
    if (s[i] == '0')
        q1(s, i + 1);
    else
        q3(s, i + 1);
}
  
void q0(string s, int i)
{
    cout << "q0->";
    if (i == s.length()) {
        cout << "NO \n";
        return;
    }
  
    // state transitions
    // 0 takes to q1, 1 takes to q2
    if (s[i] == '0')
        q1(s, i + 1);
    else
        q2(s, i + 1);
}
  
// Driver Code
int main()
{
    string s = "010101";
    // all state transitions are printed.
    // if string is accpetable, YES is printed.
    // else NO is printed
    cout << "State transitions are ";
    q0(s, 0);
}


Java
// Java Program to DFA that accepts string ending 
// with 01 or 10. 
class GFG 
{
      
    // End position is checked using the string 
    // length value. 
    // q0 is the starting state. 
    // q1 and q2 are intermediate states. 
    // q3 and q4 are final states. 
    static void q1(String s, int i) 
    { 
        System.out.print("q1->"); 
        if (i == s.length()) 
        { 
            System.out.println("NO"); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q1, 1 takes to q3 
        if (s.charAt(i) == '0') 
            q1(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    static void q2(String s, int i) 
    { 
        System.out.print("q2->"); 
        if (i == s.length())
        { 
            System.out.println("NO "); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q4, 1 takes to q2 
        if (s.charAt(i) == '0') 
            q4(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    static void q3(String s, int i) 
    { 
        System.out.print("q3->"); 
        if (i == s.length()) 
        { 
            System.out.println("YES"); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q4, 1 takes to q2 
        if (s.charAt(i) == '0') 
            q4(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    static void q4(String s, int i) 
    { 
        System.out.print("q4->"); 
        if (i == s.length()) 
        { 
            System.out.println("YES"); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q1, 1 takes to q3 
        if (s.charAt(i) == '0') 
            q1(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    static void q0(String s, int i) 
    { 
        System.out.print("q0->"); 
        if (i == s.length()) 
        { 
            System.out.println("NO"); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q1, 1 takes to q2 
        if (s.charAt(i) == '0') 
            q1(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    // Driver Code 
    public static void main (String[] args) 
    { 
        String s = "010101"; 
          
        // all state transitions are printed. 
        // if string is accpetable, YES is printed. 
        // else NO is printed 
        System.out.print("State transitions are "); 
        q0(s, 0); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 Program to DFA that accepts string ending 
# with 01 or 10. 
  
# End position is checked using the string 
# length value. 
# q0 is the starting state. 
# q1 and q2 are intermediate states. 
# q3 and q4 are final states. 
def q1(s, i) : 
  
    print("q1->", end=""); 
  
    if (i == len(s)) : 
        print("NO"); 
        return; 
  
    # state transitions 
    # 0 takes to q1, 1 takes to q3 
    if (s[i] == '0') :
        q1(s, i + 1); 
    else :
        q3(s, i + 1); 
  
def q2(s, i) : 
  
    print("q2->", end = ""); 
    if (i == len(s)) :
        print("NO"); 
        return; 
  
    # state transitions 
    # 0 takes to q4, 1 takes to q2 
    if (s[i] == '0') :
        q4(s, i + 1); 
    else :
        q2(s, i + 1); 
  
def q3(s, i) : 
  
    print("q3->", end = ""); 
    if (i == len(s)) : 
        print("YES"); 
        return; 
  
    # state transitions 
    # 0 takes to q4, 1 takes to q2 
    if (s[i] == '0') :
        q4(s, i + 1); 
    else :
        q2(s, i + 1); 
  
def q4(s, i) : 
  
    print("q4->", end = ""); 
    if (i == len(s)) : 
        print("YES"); 
        return; 
  
    # state transitions 
    # 0 takes to q1, 1 takes to q3 
    if (s[i] == '0') :
        q1(s, i + 1); 
    else :
        q3(s, i + 1); 
  
def q0( s, i) : 
      
    print("q0->", end = ""); 
    if (i == len(s)) : 
        print("NO"); 
        return; 
  
    # state transitions 
    # 0 takes to q1, 1 takes to q2 
    if (s[i] == '0') :
        q1(s, i + 1); 
    else :
        q2(s, i + 1);
  
# Driver Code 
if __name__ == "__main__" : 
    s = "010101"; 
      
    # all state transitions are printed. 
    # if string is accpetable, YES is printed. 
    # else NO is printed 
    print("State transitions are", end = " "); 
    q0(s, 0); 
  
# This code is contributed by AnkitRai01


C#
// C# Program to DFA that accepts string ending 
// with 01 or 10.
using System;
  
class GFG 
{
      
    // End position is checked using the string 
    // length value. 
    // q0 is the starting state. 
    // q1 and q2 are intermediate states. 
    // q3 and q4 are final states. 
    static void q1(string s, int i) 
    { 
        Console.Write("q1->"); 
        if (i == s.Length )
        { 
            Console.WriteLine("NO"); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q1, 1 takes to q3 
        if (s[i] == '0') 
            q1(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    static void q2(string s, int i) 
    { 
        Console.Write("q2->"); 
        if (i == s.Length)
        { 
            Console.WriteLine("NO "); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q4, 1 takes to q2 
        if (s[i] == '0') 
            q4(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    static void q3(string s, int i) 
    { 
        Console.Write("q3->"); 
        if (i == s.Length) 
        { 
            Console.WriteLine("YES"); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q4, 1 takes to q2 
        if (s[i] == '0') 
            q4(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    static void q4(string s, int i) 
    { 
        Console.Write("q4->"); 
        if (i == s.Length) 
        { 
            Console.WriteLine("YES"); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q1, 1 takes to q3 
        if (s[i] == '0') 
            q1(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    static void q0(string s, int i) 
    { 
        Console.Write("q0->"); 
        if (i == s.Length) 
        { 
            Console.WriteLine("NO"); 
            return; 
        } 
      
        // state transitions 
        // 0 takes to q1, 1 takes to q2 
        if (s[i] == '0') 
            q1(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    // Driver Code 
    public static void Main() 
    { 
        string s = "010101"; 
          
        // all state transitions are printed. 
        // if string is accpetable, YES is printed. 
        // else NO is printed 
        Console.Write("State transitions are "); 
        q0(s, 0); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
State transitions are q0->q1->q3->q4->q3->q4->q3->YES

复杂度: O(n),其中长度为n的字符串需要遍历n个状态。

一个问题陈述可能有多个DFA。