2

我现在正在我的大学学习 DFA 和 NFA 自动机,以及如何使用 Java 代码实现其中的一些。

我在这个练习中遇到了一些麻烦:我们有 4 个不同的实验室轮次(T1、T2、T3 和 T4),我们需要编写代码来识别特定字符串(由学生的大学徽章编号和他的名称,例如123321Johnson) 对应于 T2 或 T3。

我们知道:

  • T1 轮到在“A”和“K”之间有奇数徽章编号和姓氏的人
  • T2 轮到“A”和“K”之间的偶数徽章编号和姓氏
  • T3是“L”和“Z”之间的奇数徽章编号和姓氏的轮流
  • T4 轮到“L”和“Z”之间的偶数徽章编号和姓氏

我们还知道字符串必须由至少一个数字和至少一个字母组成。

例如,自动机必须接受"1232324Gac"or"1232323Lum"但不接受"121234Lum"or "121233Gac"

这是我写的代码:

import java.util.Scanner;

public class Es3 {

    static Scanner sc = new Scanner(System.in);
    String s = sc.next();
    public static boolean scan(String s)
    {
        int state = 0;                      
        int i = 0;                              

        while (state >= 0 && i < s.length()) {
            final char ch = s.charAt(i++);
            switch (state) {
            case 0:
                if (ch >= 0 && ch <= 9)
                    state = 1;
                else
                    state = -1;
                break;

            case 1:
                if (ch >=0 && ch <=9)
                    state = 1;
                else if (ch >='a' && ch <='k')
                    if ((s.charAt(i--))%2==0)
                        state = 2;
                    else
                        state = -1;
                else if (ch >='l' && ch <='z')
                    if ((s.charAt(i--))%2==1)
                        state = 3;
                    else
                        state = -1;
                else
                    state = -1;
                break;

            case 2:
                if (ch >='a' && ch <='z')
                    state = 2;
                else
                    state = -1;
                break;

            case 3:
                if (ch >='a' && ch <='z')
                    state = 3;
                else 
                    state = -1;
                break;
            }
        }
        return (state == 2 || state == 3);      
    }

    public static void main(String[] args)
    {
        System.out.println(scan(args[0]) ? "OK" : "NO");
    }
}

显然,代码不起作用,但这对于显示练习的一般目的很重要。

有人可以帮助我吗?

4

1 回答 1

0

您的算法不起作用的原因是因为您试图将char值与int值进行比较,这不会给出预期的结果。此外,当您检查一个char值是否在某个字母范围内时,您没有考虑大写字母。

import java.util.Scanner;

public class Es3 
{
    static Scanner sc = new Scanner(System.in);
    String s = sc.next();
    public static boolean scan(String s)
    {
        int state = 0;
        int i = 0;

        while (state >= 0 && i < s.length()) {
            final char ch = s.charAt(i++);
            switch (state) {
            case 0:
                // Compare the char to the char values of the numbers
                if (ch >= '0' && ch <= '9')
                    state = 1;          
                else
                    state = -1;
                break;

            case 1:  
                // Same here, compare the char to the char values of the numbers
                if (ch >= '0' && ch <= '9')
                    state = 1;
                // Check if the char is capital, as well as lowercase
                else if ((ch >= 'a' && ch <= 'k') || (ch >= 'A' && ch <= 'K'))
                    // Convert the char to an int before performing the calculations
                    if ((Character.getNumericValue(s.charAt(i-1)))%2 == 0)
                        state = 2;
                    else
                        state = -1;
                // Check if the char is capital as well
                else if ((ch >= 'l' && ch <= 'z') || (ch >= 'L' && ch <= 'Z'))
                    // Convert from char to int before calculating
                    if ((Character.getNumericValue(s.charAt(i-1)))%2 == 1)
                        state = 3;
                    else
                        state = -1;
                else
                    state = -1;
                break;

            case 2:
                // Check if the char is capital as well
                if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
                    state = 2;
                else
                    state = -1;
                break;

            case 3:
                // Check if the char is capital as well
                if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
                    state = 3;
                else 
                    state = -1;
                break;
            }
        }
        System.out.println("State "+state);
        return (state == 2 || state == 3);      
    }

    public static void main(String[] args)
    {
        System.out.println(scan(args[0]) ? "OK" : "NO");
    }
}

我认为上面的代码应该做你想做的事情。

于 2018-10-06T21:13:26.500 回答