先决条件:有限自动机
给定大小为N的字符串S ,任务是设计确定性有限自动机(DFA)以接受语言L = {a N | N≥1} 。常规语言L为{a,aa,aaa,aaaaaaa…,}。如果给定的字符串遵循给定的语言L ,则打印“ Accepted” 。否则,打印“不接受” 。
例子:
Input: S = “aaabbb”
Output: Not Accepted
Explanation: String must only contain a.
Input: S = “aa”
Output: Accepted
方法:自动机导致接受字符串的想法在以下步骤中阐述:
- 自动机将接受仅包含字符‘a’的所有字符串。如果用户试图输入除“ a”以外的任何字符,则机器将拒绝它。
- 令状态q0是表示长度为0的所有字符串的集合的初始状态,状态q1是表示从1到N的所有字符串的集合的最终状态。
- 状态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
输出:
Accepted
时间复杂度: O(N)
辅助空间: O(1)