📜  为正则表达式 C( A + B)+ 构建 DFA 的程序

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

给定一个字符串S ,任务是设计一个确定性有限自动机 (DFA) 来接受语言L = C (A + B)+ 。如果 DFA 接受给定的字符串,则打印“Yes” 。否则,打印“否”

例子:

方法:这个想法是解释给定的语言L = C (A + B)+并且对于 C(A+B)+形式的正则表达式,以下是DFA状态转换图

请按照以下步骤解决问题:

  • 如果给定字符串的长度小于等于1 ,则打印“No”
  • 如果第一个字符总是C ,则遍历剩余的字符串并检查是否有任何字符是AB
  • 如果在上述步骤中遍历时存在除AB之外任何字符,则打印“No”
  • 否则,打印“是”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find whether the given
// string is Accepted by the DFA
void DFA(string str, int N)
{
    // If n <= 1, then print No
    if (N <= 1) {
        cout << "No";
        return;
    }
 
    // To count the matched characters
    int count = 0;
 
    // Check if the first character is C
    if (str[0] == 'C') {
        count++;
 
        // Traverse the rest of string
        for (int i = 1; i < N; i++) {
 
            // If character is A or B,
            // increment count by 1
            if (str[i] == 'A' || str[i] == 'B')
                count++;
            else
                break;
        }
    }
    else {
 
        // If the first character
        // is not C, print -1
        cout << "No";
        return;
    }
 
    // If all characters matches
    if (count == N)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    string str = "CAABBAAB";
    int N = str.size();
    DFA(str, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to find whether the given
  // String is Accepted by the DFA
  static void DFA(String str, int N)
  {
 
    // If n <= 1, then print No
    if (N <= 1)
    {
      System.out.print("No");
      return;
    }
 
    // To count the matched characters
    int count = 0;
 
    // Check if the first character is C
    if (str.charAt(0) == 'C')
    {
      count++;
 
      // Traverse the rest of String
      for (int i = 1; i < N; i++)
      {
 
        // If character is A or B,
        // increment count by 1
        if (str.charAt(i) == 'A' ||
            str.charAt(i) == 'B')
          count++;
        else
          break;
      }
    }
    else
    {
 
      // If the first character
      // is not C, print -1
      System.out.print("No");
      return;
    }
 
    // If all characters matches
    if (count == N)
      System.out.print("Yes");
    else
      System.out.print("No");
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String str = "CAABBAAB";
    int N = str.length();
    DFA(str, N);
  }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find whether the given
# is Accepted by the DFA
def DFA(str, N):
     
    # If n <= 1, then prNo
    if (N <= 1):
        print("No")
        return
 
    # To count the matched characters
    count = 0
 
    # Check if the first character is C
    if (str[0] == 'C'):
        count += 1
 
        # Traverse the rest of string
        for i in range(1, N):
 
            # If character is A or B,
            # increment count by 1
            if (str[i] == 'A' or str[i] == 'B'):
                count += 1
            else:
                break
    else:
        # If the first character
        # is not C, pr-1
        print("No")
        return
 
    # If all characters matches
    if (count == N):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
    str = "CAABBAAB"
    N = len(str)
    DFA(str, N)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find whether the given
  // String is Accepted by the DFA
  static void DFA(string str, int N)
  {
 
    // If n <= 1, then print No
    if (N <= 1)
    {
      Console.Write("No");
      return;
    }
 
    // To count the matched characters
    int count = 0;
 
    // Check if the first character is C
    if (str[0] == 'C') {
      count++;
 
      // Traverse the rest of String
      for (int i = 1; i < N; i++) {
 
        // If character is A or B,
        // increment count by 1
        if (str[i] == 'A'
            || str[i] == 'B')
          count++;
        else
          break;
      }
    }
    else {
 
      // If the first character
      // is not C, print -1
      Console.Write("No");
      return;
    }
 
    // If all characters matches
    if (count == N)
      Console.Write("Yes");
    else
      Console.Write("No");
  }
 
  // Driver Code
  static public void Main()
  {
 
    string str = "CAABBAAB";
    int N = str.Length;
    DFA(str, N);
  }
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
Yes

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

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