📌  相关文章
📜  构建接受语言 L = {aN | 的 DFA 的程序N≥1}

📅  最后修改于: 2021-09-06 07:31:47             🧑  作者: Mango

先决条件:有限自动机

给定一个大小为N的字符串S ,任务是设计一个确定性有限自动机 (DFA) 来接受语言L = {a N | N≥1} 。正则语言L是 {a, aa, aaa, aaaaaaa…, }。如果给定的字符串遵循给定的语言L ,则打印“Accepted” 。否则,打印“不接受”

例子:

方法:下面分步骤说明自动机导致接受字符串的想法:

  • 自动机将接受仅包含字符‘a’ 的所有字符串。如果用户尝试输入除“a”以外的任何字符,机器将拒绝它。
  • 让状态q0初始状态,表示所有长度为0 的字符串的集合,状态q1最终状态,表示从1N的所有字符串的集合。
  • 状态q1包含 a 的自循环,这表明它可以根据需要重复。
  • 对于代码逻辑是很基本的,因为它只有一个for循环,其对一个给定的字符串中的数量,如果计数是一样的N,则它会被接受。否则,该字符串将被拒绝。

DFA 状态转换图

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check whether the string
// S satisfy the given DFA or not
void isAcceptedDFA(string s, int N)
{
    // Stores the count of chracters
    int count = 0;
 
    // Iterate over the range [0, N]
    for (int i = 0; i < N; i++) {
 
        // Count and check every
        // element for 'a'
        if (s[i] == 'a')
            count++;
    }
 
    // If string matches with DFA
    if (count == N && count != 0) {
        cout << "Accepted";
    }
 
    // If not matches
    else {
        cout << "Not Accepted";
    }
}
 
// Driver Code
int main()
{
    string S = "aaaaa";
 
    // Function Call
    isAcceptedDFA(S, S.size());
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
// Function to check whether the String
// S satisfy the given DFA or not
static void isAcceptedDFA(String s, int N)
{
    // Stores the count of chracters
    int count = 0;
 
    // Iterate over the range [0, N]
    for (int i = 0; i < N; i++)
    {
 
        // Count and check every
        // element for 'a'
        if (s.charAt(i) == 'a')
            count++;
    }
 
    // If String matches with DFA
    if (count == N && count != 0)
    {
        System.out.print("Accepted");
    }
 
    // If not matches
    else
    {
        System.out.print("Not Accepted");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "aaaaa";
 
    // Function Call
    isAcceptedDFA(S, S.length());
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to check whether the string
# S satisfy the given DFA or not
def isAcceptedDFA(s, N):
   
    # Stores the count of chracters
    count = 0
 
    # Iterate over the range [0, N]
    for i in range(N):
 
        # Count and check every
        # element for 'a'
        if (s[i] == 'a'):
            count += 1
 
    # If string matches with DFA
    if (count == N and count != 0):
        print ("Accepted")
 
    # If not matches
    else :
        print ("Not Accepted")
 
# Driver Code
if __name__ == '__main__':
    S = "aaaaa"
 
    # Function Call
    isAcceptedDFA(S, len(S))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to check whether the String
// S satisfy the given DFA or not
static void isAcceptedDFA(String s, int N)
{
   
    // Stores the count of chracters
    int count = 0;
 
    // Iterate over the range [0, N]
    for (int i = 0; i < N; i++)
    {
 
        // Count and check every
        // element for 'a'
        if (s[i] == 'a')
            count++;
    }
 
    // If String matches with DFA
    if (count == N && count != 0)
    {
        Console.Write("Accepted");
    }
 
    // If not matches
    else
    {
        Console.Write("Not Accepted");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    String S = "aaaaa";
 
    // Function Call
    isAcceptedDFA(S, S.Length);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Accepted

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

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