Java 利用 DFA 判断输入字符串是否为 4 位无符号整数
以下是使用 Java 实现的根据给定的 DFA 来判断输入字符串是否为 4 位无符号整数的程序: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FourDigitUnsignedIntegerDFA { public static final int FINAL = 5; public static final int ERROR = 6; public static int[][] integerDFA = { {0, 0, 0}, {0, 2, ERROR}, {0, 3, FINAL}, {0, 4, FINAL}, {0, FINAL, FINAL}, {0, ERROR, ERROR}, {0, 0, 0} }; public static boolean isFourDigitUnsignedInteger(String input) { int currentState = 1; for (char c : input.toCharArray()) { int inputSymbol = getInputSymbol(c); currentState = integerDFA[currentState][inputSymbol]; if (currentState == ERROR) { return false; } } return currentState == FINAL; } public static int getInputSymbol(char c) { if (Character.isDigit(c)) { return 0; } else { return 1; } } public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new FileReader("input.txt")); FileWriter writer = new FileWriter("output.txt")) { String input = reader.readLine(); boolean result = isFourDigitUnsignedInteger(input); writer.write(result? "true" : "false"); } catch (IOException e) { e.printStackTrace(); } } } ``` ######[AI写代码神器 | 423点数解答 | 2024-10-07 19:23:58]
- Java 实现复制粘贴功能:打造简易文本复制粘贴程序(GPT | 1497点数解答 | 2024-08-25 09:40:33)405
- 深度剖析:有穷自动机(NFA与DFA)及Thompson构造法的正确解读 (字节豆包 | 180点数解答 | 2024-10-16 16:21:49)120
- 深度剖析:有穷自动机相关说法及Thompson构造法全解读 (讯飞星火 | 358点数解答 | 2024-10-16 16:22:41)108
- 判断超级幸运数:满足“纯4/7数位”或“4/7总个数为4或7”的整数计数(阿里通义 | 965点数解答 | 2026-03-13 12:15:06)20
- C++ 求解:最少操作次数让字符串无子串 “friend” (字节豆包 | 153点数解答 | 2024-08-17 16:29:54)478
- C++ 求解:最少操作次数让字符串不再含 “friend” 子串!(讯飞星火 | 260点数解答 | 2024-08-17 16:30:49)464
- Python 巧解:最少操作次数让字符串告别“friend”子串(字节豆包 | 139点数解答 | 2024-08-18 13:04:14)401
- 3414 数字游戏:Python 求解 8 位 01 字符串中 1 的个数 (字节豆包 | 106点数解答 | 2024-10-27 14:23:27)426
- 巧用字符添加,计算字符串中“our”最大数量及开发注意要点(DeepSeek | 376点数解答 | 2025-10-30 19:20:06)78
- C 语言实现:处理字符串生成最多 “our” 串,附代码及关键开发注意点 (DeepSeek | 257点数解答 | 2025-10-30 19:20:36)107
- 解决数据库“dm.jdbc.driver.dmexception: 数据溢出”问题的全步骤指南(阿里通义 | 598点数解答 | 2024-10-15 15:48:38)305
- 达梦数据库数据溢出问题排查及多维度解决方法(阿里通义 | 403点数解答 | 2024-10-15 15:48:41)1134