给定一个二进制字符串S,任务是为DFA Machine写一个程序,该程序接受w∈(a,b) *上包含“ aba”作为子字符串的所有所有字符串的集合。
例子 :
Input-1 : ababa
Output : Accepted
Explanation : "ababa" consists "aba"
Input-2 : abbbb
Output : Not accepted
Explanation : "ababa" does not consist "aba"
方法:以下是针对给定问题设计的DFA计算机。为DFA状态构造一个转换表,并分析每个状态之间的转换。以下是步骤–
所需语言:
L = {aba, baba, abab, aababbb.....}
解释 :
- 首先,将有4个状态。(例如q 0 ,q 1 ,q 2 ,q 3 ) , 和 q 0是初始状态,q 3是最终状态。
- 最初我们将处于q 0状态,现在我们开始读取给定的字符串。
- 当我们读到“ b”时,我们将保持相同的状态
- 如果我们读“ a”,那么它将转换为状态q 1 。
3.假设现在我们处于q 1状态。
- 当我们读“ a”时,我们将保持相同的状态。
- 如果我们读“ b”,我们将过渡到状态q 2 。
4.假设现在我们处于q 2状态。
- 如果我们读“ a”,它将转换为状态q 3 。
- 如果我们读取“ b”,它将转换为状态q 0。
5.假设我们处于最终状态(q 3 )
- 当我们读“ a”或“ b”时,我们保持相同的状态。
6.该DFA接受的所有字符串都将以“ aba”作为其子字符串。转换表:
Current state | Final state | |
---|---|---|
a | b | |
q0 | q1 | q0 |
q1 | q1 | q2 |
q2 | q3 | q0 |
q3 | q3 | q3 |
以下是上述方法的实施–
C++
// C++ program for the above approach
#include
#include
using namespace std;
// Function to check whether the given
// string is accepted by DFA or not
void checkValidDFA(string s)
{
// Stores initial state of DFA
int initial_state = 0;
// Stores previous state of DFA
int previous_state = initial_state;
// Stores final state of DFA
int final_state;
// Iterate through the string
for (int i = 0; i < s.length(); i++) {
// Checking for all combinations
if ((previous_state == 0 && s[i] == 'a') ||
(previous_state == 1 && s[i] == 'a')) {
final_state = 1;
}
if ((previous_state == 0 && s[i] == 'b') ||
(previous_state == 2 && s[i] == 'b')) {
final_state = 0;
}
if (previous_state == 1 && s[i] == 'b') {
final_state = 2;
}
if ((previous_state == 2 && s[i] == 'a') ||
(previous_state == 3)) {
final_state = 3;
}
// Update the previous_state
previous_state = final_state;
}
// If final state is reached
if (final_state == 3) {
cout << "Accepted" << endl;
}
// Otherwise
else {
cout << "Not Accepted" << endl;
}
}
// Driver Code
int main()
{
// Given string
string s = "ababa";
// Function Call
checkValidDFA(s);
}
C
// C++ program for the above approach
#include
#include
// Function to check whether the given
// string is accepted by DFA or not
void checkValidDFA(char s[] )
{
// Stores initial state of DFA
int initial_state = 0;
// Stores previous state of DFA
int previous_state = initial_state;
// Stores final state of DFA
int final_state;
// Iterate through the string
for(int i = 0; i < strlen(s); i++)
{
// Checking for all combinations
if((previous_state == 0 && s[i] == 'a') ||
(previous_state == 1 && s[i] == 'a'))
{
final_state = 1;
}
if((previous_state == 0 && s[i] == 'b') ||
(previous_state == 2 && s[i] == 'b'))
{
final_state = 0;
}
if(previous_state == 1 && s[i] == 'b')
{
final_state = 2;
}
if((previous_state == 2 && s[i] == 'a') ||
(previous_state == 3))
{
final_state = 3;
}
// Update the previous_state
previous_state = final_state;
}
// If final state is reached
if(final_state == 3)
{
printf("Accepted");
}
// Otherwise
else
{
printf("Not Accepted");
}
}
// Driver Code
int main()
{
// Given string
char s[] = "ababa";
// Function Call
checkValidDFA(s);
}
输出 :
Accepted
时间复杂度: O(N)
辅助空间: O(1)
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。