📜  构造一个接受语言 L = {w | 的 DFA w ∈ {a,b}* 和 Na(w) mod 3 = Nb (w) mod 3}

📅  最后修改于: 2021-09-27 14:47:38             🧑  作者: Mango

问题:构造一个确定性有限自动机 (DFA) 来接受语言 L = {w |赢; {a,b}* 和 Na(w) mod 3 = Nb (w) mod 3}。

语言 L={w | Na(w) = Nb(w)mod 3} 这意味着所有字符串都包含 a 的计数模数等于 b 的计数模数乘以 3。

例子:

方法:

  1. 为 {w | 构造 FA瓦\epsilon {a, b}} 和 Na = 0 mod 3 表示 a 是 3 的倍数。
  2. 为 {w | 构造 FA瓦\epsilon {a, b}} 和 Nb = 0 mod 3 表示 b 是 3 的倍数。
  3. 连接两个 FA 并使用 DFA 中的连接过程制作单个 DFA。
  4. 将状态作为接受 a 和 b 的相等模数计数的最终状态。

      DFA 状态转换图:

      状态 00、11 和 22 导致接受字符串。而状态 01、02、10、12、20 和 21 导致拒绝字符串。

      让我们看看演示的代码:

      C/C++
      // C/C++ Program to construct a DFA which accept the language
      // L = { w | w is in {a, b}* and Na(w) mod 3 = Nb(w) mod 3}
      #include 
      #include 
        
      // dfa tells the number associated
      // string end in which state.
      int dfa = 0;
        
      // This function is for
      // the starting state (00)of DFA
      void start(char c)
      {
          if (c == 'a') {
              dfa = 10;
          }
          else if (c == 'b') {
              dfa = 1;
          }
        
          // -1 is used to check for any invalid symbol
          else {
              dfa = -1;
          }
      }
        
      // This function is for the first state (01) of DFA
      void state01(char c)
      {
          if (c == 'a') {
              dfa = 11;
          }
          else if (c == 'b') {
              dfa = 2;
          }
          else {
              dfa = -1;
          }
      }
        
      // This function is for the second state (02) of DFA
      void state02(char c)
      {
          if (c == 'b') {
              dfa = 0;
          }
          else if (c == 'a') {
              dfa = 12;
          }
          else {
              dfa = -1;
          }
      }
        
      // This function is for the third state (10)of DFA
      void state10(char c)
      {
          if (c == 'b') {
              dfa = 11;
          }
          else if (c == 'a') {
              dfa = 20;
          }
          else {
              dfa = -1;
          }
      }
        
      // This function is for the forth state (11)of DFA
      void state11(char c)
      {
          if (c == 'b') {
              dfa = 12;
          }
          else if (c == 'a') {
              dfa = 21;
          }
          else {
              dfa = -1;
          }
      }
        
      void state12(char c)
      {
          if (c == 'b') {
              dfa = 10;
          }
          else if (c == 'a') {
              dfa = 22;
          }
          else {
              dfa = -1;
          }
      }
        
      void state20(char c)
      {
          if (c == 'b') {
              dfa = 21;
          }
          else if (c == 'a') {
              dfa = 0;
          }
          else {
              dfa = -1;
          }
      }
        
      void state21(char c)
      {
          if (c == 'b') {
              dfa = 22;
          }
          else if (c == 'a') {
              dfa = 1;
          }
          else {
              dfa = -1;
          }
      }
        
      void state22(char c)
      {
          if (c == 'b') {
              dfa = 20;
          }
          else if (c == 'a') {
              dfa = 2;
          }
          else {
              dfa = -1;
          }
      }
        
      int isAccepted(char str[])
      {
          // store length of string
          int i, len = strlen(str);
        
          for (i = 0; i < len; i++) {
              if (dfa == 0)
                  start(str[i]);
        
              else if (dfa == 1)
                  state01(str[i]);
        
              else if (dfa == 2)
                  state02(str[i]);
        
              else if (dfa == 10)
                  state10(str[i]);
        
              else if (dfa == 11)
                  state11(str[i]);
        
              else if (dfa == 12)
                  state12(str[i]);
        
              else if (dfa == 20)
                  state20(str[i]);
        
              else if (dfa == 21)
                  state21(str[i]);
        
              else if (dfa == 22)
                  state22(str[i]);
        
              else
                  return 0;
          }
          if (dfa == 0 || dfa == 11 || dfa == 22)
              return 1;
          else
              return 0;
      }
        
      // driver code
      int main()
      {
          char str[] = "aaaabbbb";
          if (isAccepted(str))
              printf("ACCEPTED");
          else
              printf("NOT ACCEPTED");
          return 0;
      }
        
      // This code is contributed by SHUBHAMSINGH10.


      Java
      // Java  Program to construct a DFA which accept the language
      // L = { w | w is in {a, b}* and Na(w) mod 3 = Nb(w) mod 3}
      class GFG{
      // dfa tells the number associated
      // string end in which state.
      static int dfa = 0;
         
      // This function is for
      // the starting state (00)of DFA
      static void start(char c)
      {
          if (c == 'a') {
              dfa = 10;
          }
          else if (c == 'b') {
              dfa = 1;
          }
         
          // -1 is used to check for any invalid symbol
          else {
              dfa = -1;
          }
      }
         
      // This function is for the first state (01) of DFA
      static void state01(char c)
      {
          if (c == 'a') {
              dfa = 11;
          }
          else if (c == 'b') {
              dfa = 2;
          }
          else {
              dfa = -1;
          }
      }
         
      // This function is for the second state (02) of DFA
      static void state02(char c)
      {
          if (c == 'b') {
              dfa = 0;
          }
          else if (c == 'a') {
              dfa = 12;
          }
          else {
              dfa = -1;
          }
      }
         
      // This function is for the third state (10)of DFA
      static void state10(char c)
      {
          if (c == 'b') {
              dfa = 11;
          }
          else if (c == 'a') {
              dfa = 20;
          }
          else {
              dfa = -1;
          }
      }
         
      // This function is for the forth state (11)of DFA
      static void state11(char c)
      {
          if (c == 'b') {
              dfa = 12;
          }
          else if (c == 'a') {
              dfa = 21;
          }
          else {
              dfa = -1;
          }
      }
         
      static void state12(char c)
      {
          if (c == 'b') {
              dfa = 10;
          }
          else if (c == 'a') {
              dfa = 22;
          }
          else {
              dfa = -1;
          }
      }
         
      static void state20(char c)
      {
          if (c == 'b') {
              dfa = 21;
          }
          else if (c == 'a') {
              dfa = 0;
          }
          else {
              dfa = -1;
          }
      }
         
      static void state21(char c)
      {
          if (c == 'b') {
              dfa = 22;
          }
          else if (c == 'a') {
              dfa = 1;
          }
          else {
              dfa = -1;
          }
      }
         
      static void state22(char c)
      {
          if (c == 'b') {
              dfa = 20;
          }
          else if (c == 'a') {
              dfa = 2;
          }
          else {
              dfa = -1;
          }
      }
         
      static boolean isAccepted(String st)
      {
          // store length of string
          int i, len = st.length();
          char[] str = st.toCharArray();
          for (i = 0; i < len; i++) {
              if (dfa == 0)
                  start(str[i]);
         
              else if (dfa == 1)
                  state01(str[i]);
         
              else if (dfa == 2)
                  state02(str[i]);
         
              else if (dfa == 10)
                  state10(str[i]);
         
              else if (dfa == 11)
                  state11(str[i]);
         
              else if (dfa == 12)
                  state12(str[i]);
         
              else if (dfa == 20)
                  state20(str[i]);
         
              else if (dfa == 21)
                  state21(str[i]);
         
              else if (dfa == 22)
                  state22(str[i]);
         
              else
                  return false;
          }
          if (dfa == 0 || dfa == 11 || dfa == 22)
              return true;
          else
              return false;
      }
         
      // driver code
      public static void main(String []args)
          {
          String str = "aaaabbbb";
          if (isAccepted(str))
              System.out.println("ACCEPTED");
          else
              System.out.println("NOT ACCEPTED");
          }
      }
        
      // This code contributed by Rajput-Ji


      Python 3
      """Python Program to construct a DFA which accept the language 
      L =  {w | w belongs to {a, b}*}
      and Na(w)mod3 = Nb(w)mod 3"""
          
      # This function is for 
      # the starting state (00)of DFA 
      def start( c): 
         
          if (c == 'a'):  
              dfa = 10 
             
          elif (c == 'b') : 
              dfa = 1 
             
          
           #-1 is used to check for any invalid symbol 
          else:  
              dfa = -1
          return dfa
             
         
          
      # This function is for the first state (01) of DFA 
      def state01( c): 
         
          if (c == 'a'):  
              dfa = 11 
             
          elif (c == 'b') : 
              dfa = 2 
             
          else:  
              dfa = -1
          return dfa
         
          
      # This function is for the second state (02) of DFA 
      def state02( c): 
         
          if (c == 'b') : 
              dfa = 0 
             
          elif(c =='a'):
            
              dfa = 12
            
          else:  
              dfa = -1
          return dfa
             
         
          
      # This function is for the third state (10)of DFA 
      def state10( c): 
         
          if (c == 'b') : 
              dfa = 11 
             
          elif(c =='a'):
            
              dfa = 20
            
          else:  
              dfa = -1
          return dfa
             
         
        
      # This function is for the forth state (11)of DFA 
      def state11( c): 
         
          if (c == 'b') : 
              dfa = 12 
             
          elif(c =='a'):
            
              dfa = 21
            
          else:  
              dfa = -1
          return dfa
             
         
        
      def state12( c): 
         
          if (c == 'b') : 
              dfa = 10 
             
          elif(c =='a'):
            
              dfa = 22
            
          else:  
              dfa = -1
          return dfa
             
         
        
      def state20( c): 
         
          if (c == 'b') : 
              dfa = 21 
             
          elif(c =='a'):
            
              dfa = 0
            
          else:  
              dfa = -1
          return dfa
             
        
        
      def state21( c): 
         
          if (c == 'b') : 
              dfa = 22 
             
          elif(c =='a'):
            
              dfa = 1
            
          else:  
              dfa = -1
          return dfa
        
        
      def state22( c): 
         
          if (c == 'b') : 
              dfa = 20 
             
          elif(c =='a'):
            
              dfa = 2
            
          else:  
              dfa = -1
          return dfa
             
        
        
      def isAccepted(str): 
         
          # store length of string 
          l = len(str)
          # dfa tells the number associated  
          # with the present dfa = state  
          dfa = 0
            
          for i in range(l):  
              if (dfa == 0) :
                  start(str[i]) 
          
              elif (dfa == 1): 
                  state01(str[i]) 
          
              elif (dfa == 2) :
                  state02(str[i]) 
          
              elif (dfa == 10) :
                  state10(str[i]) 
          
              elif (dfa == 11) :
                  state11(str[i]) 
                    
              elif (dfa == 12) :
                  state12(str[i]) 
                
              elif (dfa == 20) :
                  state20(str[i]) 
          
              elif (dfa == 21) :
                  state21(str[i]) 
          
              elif (dfa == 22) :
                  state22(str[i]) 
                    
              else:
                  return 0 
             
          if (dfa == 0 or dfa == 11 or dfa == 22) :
              return 1 
          else:
              return 0 
         
          
      # Driver code   
      if __name__ == "__main__" : 
         
          string = "aaaabbbb" 
          if (isAccepted(string)) :
              print("ACCEPTED") 
          else:
              print("NOT ACCEPTED") 
         
        
      # This code is contributed by SHUBHAMSINGH10.


      C#
      // C# Program to construct a DFA which accept the language 
      // L = { w | w is in {a, b}* and Na(w) mod 3 = Nb(w) mod 3} 
      using System;
        
      class GFG
      { 
        
      // DFA tells the number associated 
      // string end in which state. 
      static int dfa = 0; 
        
      // This function is for 
      // the starting state (00)of DFA 
      static void start(char c) 
      { 
          if (c == 'a') { 
              dfa = 10; 
          } 
          else if (c == 'b') { 
              dfa = 1; 
          } 
        
          // -1 is used to check for any invalid symbol 
          else { 
              dfa = -1; 
          } 
      } 
        
      // This function is for the first state (01) of DFA 
      static void state01(char c) 
      { 
          if (c == 'a') { 
              dfa = 11; 
          } 
          else if (c == 'b') { 
              dfa = 2; 
          } 
          else { 
              dfa = -1; 
          } 
      } 
        
      // This function is for the second state (02) of DFA 
      static void state02(char c) 
      { 
          if (c == 'b') { 
              dfa = 0; 
          } 
          else if (c == 'a') { 
              dfa = 12; 
          } 
          else { 
              dfa = -1; 
          } 
      } 
        
      // This function is for the third state (10)of DFA 
      static void state10(char c) 
      { 
          if (c == 'b') { 
              dfa = 11; 
          } 
          else if (c == 'a') { 
              dfa = 20; 
          } 
          else { 
              dfa = -1; 
          } 
      } 
        
      // This function is for the forth state (11)of DFA 
      static void state11(char c) 
      { 
          if (c == 'b') { 
              dfa = 12; 
          } 
          else if (c == 'a') { 
              dfa = 21; 
          } 
          else { 
              dfa = -1; 
          } 
      } 
        
      static void state12(char c) 
      { 
          if (c == 'b') { 
              dfa = 10; 
          } 
          else if (c == 'a') { 
              dfa = 22; 
          } 
          else { 
              dfa = -1; 
          } 
      } 
        
      static void state20(char c) 
      { 
          if (c == 'b') { 
              dfa = 21; 
          } 
          else if (c == 'a') { 
              dfa = 0; 
          } 
          else { 
              dfa = -1; 
          } 
      } 
        
      static void state21(char c) 
      { 
          if (c == 'b') { 
              dfa = 22; 
          } 
          else if (c == 'a') { 
              dfa = 1; 
          } 
          else { 
              dfa = -1; 
          } 
      } 
        
      static void state22(char c) 
      { 
          if (c == 'b') { 
              dfa = 20; 
          } 
          else if (c == 'a') { 
              dfa = 2; 
          } 
          else { 
              dfa = -1; 
          } 
      } 
        
      static Boolean isAccepted(String st) 
      { 
          // store length of string 
          int i, len = st.Length; 
          char[] str = st.ToCharArray(); 
          for (i = 0; i < len; i++) { 
              if (dfa == 0) 
                  start(str[i]); 
        
              else if (dfa == 1) 
                  state01(str[i]); 
        
              else if (dfa == 2) 
                  state02(str[i]); 
        
              else if (dfa == 10) 
                  state10(str[i]); 
        
              else if (dfa == 11) 
                  state11(str[i]); 
        
              else if (dfa == 12) 
                  state12(str[i]); 
        
              else if (dfa == 20) 
                  state20(str[i]); 
        
              else if (dfa == 21) 
                  state21(str[i]); 
        
              else if (dfa == 22) 
                  state22(str[i]); 
        
              else
                  return false; 
          } 
          if (dfa == 0 || dfa == 11 || dfa == 22) 
              return true; 
          else
              return false; 
      } 
        
      // Driver code 
      public static void Main(String []args) 
      { 
          String str = "aaaabbbb"; 
          if (isAccepted(str)) 
              Console.WriteLine("ACCEPTED"); 
          else
              Console.WriteLine("NOT ACCEPTED"); 
      } 
      } 
        
      // This code is contributed by 29AjayKumar


      输出:

      ACCEPTED