test
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String input = scanner.nextLine(); if (input == null || input.length() == 0) { System.out.println(0); continue; } boolean nextIsChar = true; boolean nextIsCommand = false; boolean onlyNOTCommand = false; boolean flag = true; String[] element = input.split(" "); for (String temp : element) { if (temp.length() == 1 && (Character.isLowerCase(temp.charAt(0)))) { if (nextIsChar) { nextIsChar = false; nextIsCommand = true; onlyNOTCommand = false; continue; } else { System.out.println(0); flag = false; break; } } else if (temp.length() > 1) { if (nextIsCommand) { if (!onlyNOTCommand && temp.equals("AND")) { nextIsChar = true; nextIsCommand = false; continue; } else if (!onlyNOTCommand && temp.equals("OR")) { nextIsChar = true; onlyNOTCommand = true; continue; } else if (temp.equals("NOT")) { nextIsChar = true; nextIsCommand = false; onlyNOTCommand = false; continue; } else { System.out.println(0); flag = false; break; } } else { System.out.println(0); flag = false; break; } } flag = false; System.out.println(0); break; } if (flag) { System.out.println(1); } } }