先决条件:有限自动机
给定一个大小为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
Javascript
输出:
Accepted
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live