问题–接受不以子字符串“ THE”结尾的字符串。检查给定的字符串是否以“ the”结尾。字符串末尾避免使用的“ the”的不同形式为:
"THE", "ThE", "THe", "tHE", "thE", "The", "tHe" and "the"
所有以上述任何形式的“ the”结尾的字符串均不接受。
不以“ THE”结尾的字符串的确定性自动机(DFA)–
此dfa中的初始状态为Qo
程序中使用的方法–
在此程序中,请考虑4个状态分别为0、1、2和3。现在让我们接受一个名为DFA的变量,该变量最初将为0。无论何时发生任何转换,它都将使用与DFA相关的数字来更新DFA的值。新状态。
示例:如果发生从状态0到状态1的过渡,则DFA的值将更新为1。如果发生从状态2到状态3的过渡,则dfa的值将更新为3。整个字符串上的算法,如果最后达到状态0、1或2,则我们的字符串将被接受,否则将不被接受。
Input : XYzabCthe
Output : NOT ACCEPTED
Input : Themaliktth
Output : ACCEPTED
C++
// CPP program to implement DFS that accepts
// all string that do not end with "THE"
#include
#include
// dfa tells the number associated
// with the present state
int dfa = 0;
// This function is for
// the starting state (zeroth) of DFA
void start(char c)
{
// On receiving 'T' or 't' goto first state (1)
if (c == 't' || c == 'T')
dfa = 1;
}
// This function is for the first state of DFA
void state1(char c)
{
// On receiving 'T' or 't' goto first state (1)
if (c == 't' || c == 'T')
dfa = 1;
// On receiving 'H' or 'h' goto second state (2)
else if (c == 'h' || c == 'H')
dfa = 2;
// else goto starting state (0)
else
dfa = 0;
}
// This function is for the second state of DFA
void state2(char c)
{
// On receiving 'E' or 'e' goto third state (3)
// else goto starting state (0)
if (c == 'e' || c == 'E')
dfa = 3;
else
dfa = 0;
}
// This function is for the third state of DFA
void state3(char c)
{
// On receiving 'T' or 't' goto first state (1)
// else goto starting state (0)
if (c == 't' || c == 'T')
dfa = 1;
else
dfa = 0;
}
bool isAccepted(char str[])
{
// store length of string
int len = strlen(str);
for (int i=0; i < len; i++) {
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else
state3(str[i]);
}
return (dfa != 3);
}
// driver code
int main()
{
char str[] = "forTHEgeeks";
if (isAccepted(str) == true)
printf("ACCEPTED\n");
else
printf("NOT ACCEPTED\n");
return 0;
}
Java
// Java program to implement DFS that accepts
// all string that do not end with "THE"
import java.util.*;
class GFG
{
// dfa tells the number associated
// with the present state
static int dfa = 0;
// This function is for
// the starting state (zeroth) of DFA
static void start(char c)
{
// On receiving 'T' or 't' goto first state (1)
if (c == 't' || c == 'T')
dfa = 1;
}
// This function is for the first state of DFA
static void state1(char c)
{
// On receiving 'T' or 't' goto first state (1)
if (c == 't' || c == 'T')
dfa = 1;
// On receiving 'H' or 'h' goto second state (2)
else if (c == 'h' || c == 'H')
dfa = 2;
// else goto starting state (0)
else
dfa = 0;
}
// This function is for the second state of DFA
static void state2(char c)
{
// On receiving 'E' or 'e' goto third state (3)
// else goto starting state (0)
if (c == 'e' || c == 'E')
dfa = 3;
else
dfa = 0;
}
// This function is for the third state of DFA
static void state3(char c)
{
// On receiving 'T' or 't' goto first state (1)
// else goto starting state (0)
if (c == 't' || c == 'T')
dfa = 1;
else
dfa = 0;
}
static boolean isAccepted(char str[])
{
// store length of string
int len = str.length;
for (int i=0; i < len; i++)
{
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else
state3(str[i]);
}
return (dfa != 3);
}
// Driver code
public static void main(String[] args)
{
char str[] = "forTHEgeeks".toCharArray();
if (isAccepted(str) == true)
System.out.println("ACCEPTED\n");
else
System.out.println("NOT ACCEPTED\n");
}
}
/* This code is contributed by PrinciRaj1992 */
Python3
# Python3 program to implement DFS that accepts
# all stringing that do not end with "THE"
# This function is for the starting
# state (zeroth) of DFA
def start(c):
# On receiving 'T' or 't' goto
# first state (1)
if (c == 't' or c == 'T'):
dfa=1
# This function is for the first state of DFA
def state1(c):
# On receiving 'T' or 't' goto first state (1)
if (c == 't' or c == 'T'):
dfa = 1
# On receiving 'H' or 'h' goto second state (2)
elif (c == 'h' or c == 'H'):
dfa = 2
# else goto starting state (0)
else:
dfa = 0
# This function is for the second state of DFA
def state2(c):
# On receiving 'E' or 'e' goto third
# state (3) else goto starting state (0)
if (c == 'e' or c == 'E'):
dfa = 3
else:
dfa = 0
# This function is for the third state of DFA
def state3(c):
# On receiving 'T' or 't' goto first
# state (1) else goto starting state (0)
if (c == 't' or c == 'T'):
dfa = 1
else:
dfa = 0
def isAccepted(string):
# store length of stringing
length = len(string)
for i in range(length):
if (dfa == 0):
start(string[i])
elif (dfa == 1):
state1(string[i])
elif (dfa == 2):
state2(string[i])
else:
state3(string[i])
return (dfa != 3)
# Driver Code
if __name__ == "__main__" :
string="forTHEgeeks"
# dfa tells the number associated
# with the present state
dfa = 0
if isAccepted(string):
print("ACCEPTED")
else:
print("NOT ACCEPTED")
# This code is contributed by SHUBHAMSINGH10
C#
// C# program to implement DFS that accepts
// all string that do not end with "THE"
using System;
class GFG
{
// dfa tells the number associated
// with the present state
static int dfa = 0;
// This function is for
// the starting state (zeroth) of DFA
static void start(char c)
{
// On receiving 'T' or 't' goto first state (1)
if (c == 't' || c == 'T')
dfa = 1;
}
// This function is for the first state of DFA
static void state1(char c)
{
// On receiving 'T' or 't' goto first state (1)
if (c == 't' || c == 'T')
dfa = 1;
// On receiving 'H' or 'h' goto second state (2)
else if (c == 'h' || c == 'H')
dfa = 2;
// else goto starting state (0)
else
dfa = 0;
}
// This function is for the second state of DFA
static void state2(char c)
{
// On receiving 'E' or 'e' goto third state (3)
// else goto starting state (0)
if (c == 'e' || c == 'E')
dfa = 3;
else
dfa = 0;
}
// This function is for the third state of DFA
static void state3(char c)
{
// On receiving 'T' or 't' goto first state (1)
// else goto starting state (0)
if (c == 't' || c == 'T')
dfa = 1;
else
dfa = 0;
}
static bool isAccepted(char []str)
{
// store length of string
int len = str.Length;
for (int i=0; i < len; i++)
{
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else
state3(str[i]);
}
return (dfa != 3);
}
// Driver code
static public void Main ()
{
char []str = "forTHEgeeks".ToCharArray();
if (isAccepted(str) == true)
Console.WriteLine("ACCEPTED\n");
else
Console.WriteLine("NOT ACCEPTED\n");
}
}
/* This code is contributed by ajit. */
PHP
输出 :
ACCEPTED
该程序的时间复杂度为O(n)