|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + public static void main(String[] args) throws IOException { |
| 6 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + int T = Integer.parseInt(br.readLine()); |
| 8 | + |
| 9 | + while (T-- > 0) { |
| 10 | + String s = br.readLine(); |
| 11 | + if (isMatch(s)) System.out.println("YES"); |
| 12 | + else System.out.println("NO"); |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + static boolean isMatch(String s) { |
| 17 | + int state = 0; |
| 18 | + |
| 19 | + for (char c : s.toCharArray()) { |
| 20 | + int cur = c - '0'; |
| 21 | + |
| 22 | + if (state == 0) { |
| 23 | + state = (cur == 0) ? 1 : 2; |
| 24 | + } else if (state == 1) { |
| 25 | + if (cur == 1) state = 0; |
| 26 | + else return false; |
| 27 | + } else if (state == 2) { |
| 28 | + if (cur == 0) state = 3; |
| 29 | + else return false; |
| 30 | + } else if (state == 3) { |
| 31 | + if (cur == 0) state = 4; |
| 32 | + else return false; |
| 33 | + } else if (state == 4) { |
| 34 | + if (cur == 0) state = 4; |
| 35 | + else state = 5; |
| 36 | + } else if (state == 5) { |
| 37 | + if (cur == 0) state = 6; |
| 38 | + else state = 7; |
| 39 | + } else if (state == 6) { |
| 40 | + if (cur == 0) return false; |
| 41 | + else state = 0; |
| 42 | + } else if (state == 7) { |
| 43 | + if (cur == 0) state = 8; |
| 44 | + else state = 7; |
| 45 | + } else if (state == 8) { |
| 46 | + if (cur == 0) state = 4; |
| 47 | + else state = 0; |
| 48 | + } |
| 49 | + |
| 50 | + if (state == -1) return false; |
| 51 | + } |
| 52 | + |
| 53 | + return state == 0 || state == 5 || state == 7; |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
0 commit comments