📌  相关文章
📜  使用DFA检查二进制字符串是否为3的倍数

📅  最后修改于: 2021-05-25 07:10:54             🧑  作者: Mango

给定字符串二进制字符,请检查它是否为3的倍数。
例子 :

Input :  1 0 1 0
Output : NO
Explanation : (1 0 1 0) is 10 and hence
not a multiple of 3

Input :  1 1 0 0
Output : YES
Explanation : (1 1 0 0) is 12 and hence 
a multiple of 3

方法:一种简单的方法是将二进制数转换为其十进制表示形式,然后检查其是否为3的倍数。现在,当涉及DFA(确定性有限自动机)时,没有内存的概念,即在提供字符串时无法存储该字符串,因此上述方法将不适用。简而言之,DFA会将字符串作为输入并对其进行处理。如果达到最终状态,则接受它,否则拒绝它。正如你不能存储字符串,所以输入被拍摄字符的字符。

给定问题的DFA为:

因为,当一个数字除以3时,只有3种可能性。余数可以是0、1或2。这里,状态0表示数字除以3时的余数为0。状态1表示数字除以3时的余数为1,类似地,状态2表示数字除以3的余数为2。因此,如果字符串到达状态0,则该字符串会被接受,否则将被拒绝。

下面是上述方法的实现:

C++
// C++ Program to illustrate
// DFA for multiple of 3
#include 
using namespace std;
 
// checks if binary characters
// are multiple of 3
bool isMultiple3(char c[], int size)
{
    // initial state is 0th
    char state = '0';
 
    for (int i = 0; i < size; i++) {
 
        // storing binary digit
        char digit = c[i];
 
        switch (state) {
 
        // when state is 0
        case '0':
            if (digit == '1')
                state = '1';
            break;
 
        // when state is 1
        case '1':
            if (digit == '0')
                state = '2';
            else
                state = '0';
            break;
 
        // when state is 2
        case '2':
            if (digit == '0')
                state = '1';
            break;
        }
    }
 
    // if final state is 0th state
    if (state == '0')
        return true;
    return false;
}
 
// Driver's Code
int main()
{
    // size of binary array
    int size = 5;
 
    // array of binary numbers
    // Here it is 21 in decimal
    char c[] = { '1', '0', '1', '0', '1' };
 
    // if binary numbers are a multiple of 3
    if (isMultiple3(c, size))
        cout << "YES\n";
    else
        cout << "NO\n";
 
    return 0;
}


Java
// Java Program to illustrate
// DFA for multiple of 3
import java.io.*;
 
class GFG
{
    // checks if binary characters
    // are multiple of 3
    static boolean isMultiple3(char c[], int size)
    {
        // initial state is 0th
        char state = '0';
     
        for (int i = 0; i < size; i++) {
     
            // storing binary digit
            char digit = c[i];
     
            switch (state) {
     
            // when state is 0
            case '0':
                if (digit == '1')
                    state = '1';
                break;
     
            // when state is 1
            case '1':
                if (digit == '0')
                    state = '2';
                else
                    state = '0';
                break;
     
            // when state is 2
            case '2':
                if (digit == '0')
                    state = '1';
                break;
            }
        }
     
        // if final state is 0th state
        if (state == '0')
            return true;
        return false;
    }
     
 
 
 
    // Driver Code
    public static void main (String[] args)
    {
        // size of binary array
        int size = 5;
     
        // array of binary numbers
        // Here it is 21 in decimal
        char c[] = { '1', '0', '1', '0', '1' };
     
        // if binary numbers are a multiple of 3
        if (isMultiple3(c, size))
            System.out.println ("YES");
        else
            System.out.println ("NO");
             
    }
}
// This code is contributed by vt_m


Python3
# Python3 program to illustrate
# DFA for multiple of 3
 
# Checks if binary characters
# are multiple of 3
def isMultiple3(c, size):
     
    # Initial state is 0th
    state = '0'
     
    for i in range(size):
         
        # Storing binary digit
        digit = c[i]
         
        # When state is 0
        if state == '0':
            if (digit == '1'):
                state = '1'
         
        # When state is 1        
        elif state == '1':
            if (digit == '0'):
                state = '2'
            else:
                state = '0'
         
        # When state is 2        
        elif state == '2':
            if (digit == '0'):
                state = '1'
   
    # If final state is 0th state
    if (state == '0'):
        return True
         
    return False
 
# Driver code
if __name__=="__main__":
     
    # Size of binary array
    size = 5;
  
    # Array of binary numbers
    # Here it is 21 in decimal
    c = [ '1', '0', '1', '0', '1']
  
    # If binary numbers are a multiple of 3
    if (isMultiple3(c, size)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by rutvik_56


C#
// C# Program to illustrate
// DFA for multiple of 3
using System;
 
class GFG {
     
    // checks if binary characters
    // are multiple of 3
    static bool isMultiple3(char []c, int size)
    {
        // initial state is 0th
        char state = '0';
     
        for (int i = 0; i < size; i++)
        {
     
            // storing binary digit
            char digit = c[i];
     
            switch (state)
            {
     
                // when state is 0
                case '0':
                    if (digit == '1')
                        state = '1';
                    break;
         
                // when state is 1
                case '1':
                    if (digit == '0')
                        state = '2';
                    else
                        state = '0';
                    break;
         
                // when state is 2
                case '2':
                    if (digit == '0')
                        state = '1';
                    break;
            }
        }
     
        // if final state is 0th state
        if (state == '0')
            return true;
             
        return false;
    }
 
    // Driver Code
    public static void Main ()
    {
         
        // size of binary array
        int size = 5;
     
        // array of binary numbers
        // Here it is 21 in decimal
        char []c = { '1', '0', '1', '0', '1' };
     
        // if binary numbers are a multiple of 3
        if (isMultiple3(c, size))
            Console.WriteLine ("YES");
        else
            Console.WriteLine ("NO");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

YES