📌  相关文章
📜  程序用于构建DFA,以接受以相同字符开头和结尾的字符串

📅  最后修改于: 2021-05-04 23:24:39             🧑  作者: Mango

给定一个由字符ab组成的字符串,请检查该字符串是否以相同的字符开头和结尾。如果是,则打印“是”,否则打印“否”。

例子:

方法:

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

在DFA中,没有记忆的概念,因此我们必须通过字符来检查字符串的字符,与第0字符开始。该问题的输入字符集为{a,b} 。为了使DFA有效,必须为每个状态下的输入集的每个符号定义一个转换规则,以转换为有效状态。

DFA机器,接受以相同字符开头和结尾的所有字符串
对于上述问题,我们必须首先构建DFA计算机。 DFA机器类似于具有各种状态和转换的流程图。与上述问题相对应的DFA机器如下所示,Q1和Q3是最终状态:

此DFA机器的工作原理是
机器的工作情况取决于第一个字符是“ a”还是“ b”。

  • 情况1:字符串以“ a”开头
    1. 假设输入字符串的第一个字符为“ a”,然后在读取“ a”时,控件将移至机器的上级分支。
    2. 现在,它在字符串定义处必须以“ a”结尾才能被接受。
    3. 在状态Q1处,如果再次出现’a’,则它将继续在相同状态下盘旋,因为对于机器而言,最后读取的字符可能是字符串的最后一个字符。
    4. 如果它得到一个“ b”,则它必须离开最终状态,因为以“ b”结尾的字符串是不可接受的。因此它进入状态Q2
    5. 在这里,如果得到一个“ a”,它将再次进入最终状态Q1,否则对于连续的“ b”,它将继续盘旋。
  • 情况2:字符串以“ b”开头
    1. 假设输入字符串的第一个字符为“ b”,然后在读取“ b”时,控件将移至机器的上级分支。
    2. 现在,它在字符串定义处必须以“ b”结尾才能被接受。
    3. 在状态Q3处,如果再次出现’b’,则它将继续在相同状态下盘旋,因为对于机器而言,最后读取的字符可能是字符串的最后一个字符。
    4. 如果它得到一个“ a”,则它必须离开最终状态,因为以“ a”结尾的字符串是不可接受的。因此它进入状态Q4
    5. 在这里,如果得到一个“ b”,它将再次进入最终状态Q3,否则对于连续的“ a”,它将继续盘旋。

设计DFA机器的方法:

  1. 定义制作状态图所需的最小状态数。 Q0,Q1,Q2,Q3,Q4是定义的状态。对各种状态使用功能。
  2. 列出所有有效的过渡。这里的“ a”和“ b”是有效符号。每个状态必须对每个有效符号都有一个过渡。
  3. 通过应用基本条件定义最终状态。 Q1和Q3被定义为最终状态。如果字符串输入在这些状态中的任何一个处结束,则将其接受,否则将被拒绝。
  4. 使用状态函数调用定义所有状态转换。
  5. 为字符串的末尾定义一个返回条件。如果通过执行该过程,程序到达字符串的末尾,则根据程序所处的状态进行输出。

下面是上述方法的实现。

C++
// C++ Program for DFA that accepts string
// if it starts and ends with same character
  
#include 
using namespace std;
  
// States of DFA
void q1(string, int);
void q2(string, int);
void q3(string, int);
void q4(string, int);
  
// Function for the state Q1
void q1(string s, int i)
{
  
    // Condition to check end of string
    if (i == s.length()) {
        cout << "Yes \n";
        return;
    }
  
    // State transitions
    // 'a' takes to q1, and
    // 'b' takes to q2
    if (s[i] == 'a')
        q1(s, i + 1);
    else
        q2(s, i + 1);
}
  
// Function for the state Q2
void q2(string s, int i)
{
    // Condition to check end of string
    if (i == s.length()) {
        cout << "No \n";
        return;
    }
  
    // State transitions
    // 'a' takes to q1, and
    // 'b' takes to q2
    if (s[i] == 'a')
        q1(s, i + 1);
    else
        q2(s, i + 1);
}
  
// Function for the state Q3
void q3(string s, int i)
{
    // Condition to check end of string
    if (i == s.length()) {
        cout << "Yes \n";
        return;
    }
  
    // State transitions
    // 'a' takes to q4, and
    // 'b' takes to q3
    if (s[i] == 'a')
        q4(s, i + 1);
    else
        q3(s, i + 1);
}
  
// Function for the state Q4
void q4(string s, int i)
{
    // Condition to check end of string
    if (i == s.length()) {
        cout << "No \n";
        return;
    }
  
    // State transitions
    // 'a' takes to q4, and
    // 'b' takes to q3
    if (s[i] == 'a')
        q4(s, i + 1);
    else
        q3(s, i + 1);
}
  
// Function for the state Q0
void q0(string s, int i)
{
  
    // Condition to check end of string
    if (i == s.length()) {
        cout << "No \n";
        return;
    }
  
    // State transitions
    // 'a' takes to q1, and
    // 'b' takes to q3
    if (s[i] == 'a')
        q1(s, i + 1);
    else
        q3(s, i + 1);
}
  
// Driver Code
int main()
{
    string s = "abbaabb";
  
    // Since q0 is the starting state
    // Send the string to q0
    q0(s, 0);
}


Java
// Java Program for DFA that accepts string 
// if it starts and ends with same character
class GFG 
{
      
    // Function for the state Q1 
    static void q1(String s, int i) 
    { 
      
        // Condition to check end of string 
        if (i == s.length()) 
        { 
            System.out.println("Yes"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q1, and 
        // 'b' takes to q2 
        if (s.charAt(i) == 'a') 
            q1(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    // Function for the state Q2 
    static void q2(String s, int i) 
    { 
        // Condition to check end of string 
        if (i == s.length())
        { 
            System.out.println("No"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q1, and 
        // 'b' takes to q2 
        if (s.charAt(i) == 'a') 
            q1(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    // Function for the state Q3 
    static void q3(String s, int i) 
    { 
        // Condition to check end of string 
        if (i == s.length()) 
        { 
            System.out.println("Yes"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q4, and 
        // 'b' takes to q3 
        if (s.charAt(i) == 'a') 
            q4(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    // Function for the state Q4 
    static void q4(String s, int i) 
    { 
        // Condition to check end of string 
        if (i == s.length())
        { 
            System.out.println("No"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q4, and 
        // 'b' takes to q3 
        if (s.charAt(i) == 'a') 
            q4(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    // Function for the state Q0 
    static void q0(String s, int i) 
    { 
      
        // Condition to check end of string 
        if (i == s.length()) 
        { 
            System.out.println("No"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q1, and 
        // 'b' takes to q3 
        if (s.charAt(i) == 'a') 
            q1(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    // Driver Code 
    public static void main (String[] args)
    { 
        String s = "abbaabb"; 
      
        // Since q0 is the starting state 
        // Send the string to q0 
        q0(s, 0); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 Program for DFA that accepts string 
# if it starts and ends with same character
  
# Function for the state Q1
def q1(s, i):
  
    # Condition to check end of string
    if (i == len(s)):
        print("Yes");
        return;
      
    # State transitions
    # 'a' takes to q1, and
    # 'b' takes to q2
    if (s[i] == 'a'):
        q1(s, i + 1);
    else:
        q2(s, i + 1);
  
# Function for the state Q2
def q2(s, i):
      
    # Condition to check end of string
    if (i == len(s)):
        print("No");
        return;
      
    # State transitions
    # 'a' takes to q1, and
    # 'b' takes to q2
    if (s[i] == 'a'):
        q1(s, i + 1);
    else:
        q2(s, i + 1);
  
# Function for the state Q3
def q3(s, i):
      
    # Condition to check end of string
    if (i == len(s)):
        print("Yes");
        return;
      
    # State transitions
    # 'a' takes to q4, and
    # 'b' takes to q3
    if (s[i] == 'a'):
        q4(s, i + 1);
    else:
        q3(s, i + 1);
  
# Function for the state Q4
def q4(s, i):
      
    # Condition to check end of string
    if (i == s.length()):
        print("No");
        return;
      
    # State transitions
    # 'a' takes to q4, and
    # 'b' takes to q3
    if (s[i] == 'a'):
        q4(s, i + 1);
    else:
        q3(s, i + 1);
  
# Function for the state Q0
def q0(s, i):
  
    # Condition to check end of string
    if (i == len(s)):
        print("No");
        return;
      
    # State transitions
    # 'a' takes to q1, and
    # 'b' takes to q3
    if (s[i] == 'a'):
        q1(s, i + 1);
    else:
        q3(s, i + 1);
  
# Driver Code
if __name__ == '__main__':
    s = "abbaabb";
  
    # Since q0 is the starting state
    # Send the string to q0
    q0(s, 0);
      
# This code is contributed by Rajput-Ji


C#
// C# Program for DFA that accepts string 
// if it starts and ends with same character
using System;
  
class GFG 
{
      
    // Function for the state Q1 
    static void q1(string s, int i) 
    { 
      
        // Condition to check end of string 
        if (i == s.Length)
        { 
            Console.WriteLine("Yes"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q1, and 
        // 'b' takes to q2 
        if (s[i] == 'a') 
            q1(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    // Function for the state Q2 
    static void q2(string s, int i) 
    { 
        // Condition to check end of string 
        if (i == s.Length)
        { 
            Console.WriteLine("No"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q1, and 
        // 'b' takes to q2 
        if (s[i] == 'a') 
            q1(s, i + 1); 
        else
            q2(s, i + 1); 
    } 
      
    // Function for the state Q3 
    static void q3(string s, int i) 
    { 
        // Condition to check end of string 
        if (i == s.Length) 
        { 
            Console.WriteLine("Yes"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q4, and 
        // 'b' takes to q3 
        if (s[i] == 'a') 
            q4(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    // Function for the state Q4 
    static void q4(string s, int i) 
    { 
        // Condition to check end of string 
        if (i == s.Length)
        { 
            Console.WriteLine("No"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q4, and 
        // 'b' takes to q3 
        if (s[i] == 'a') 
            q4(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    // Function for the state Q0 
    static void q0(string s, int i) 
    { 
      
        // Condition to check end of string 
        if (i == s.Length) 
        { 
            Console.WriteLine("No"); 
            return; 
        } 
      
        // State transitions 
        // 'a' takes to q1, and 
        // 'b' takes to q3 
        if (s[i] == 'a') 
            q1(s, i + 1); 
        else
            q3(s, i + 1); 
    } 
      
    // Driver Code 
    public static void Main ()
    { 
        string s = "abbaabb"; 
      
        // Since q0 is the starting state 
        // Send the string to q0 
        q0(s, 0); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
No

时间复杂度: O(N)