使用了String結構
使用String的正則表達式用來匹配字符串是否符合狀況java
package _1003_我要經過; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; public class Main2 { public static void main(String[] args) throws Exception{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> list = new ArrayList<String>(); int n = Integer.parseInt(bf.readLine()); for(int i=0 ; i<n; i++) { list.add(bf.readLine()); } for(String s: list) { judge(s); } } public static void judge(String s) { String pattern1 = "A*PA+TA*"; String pathern2 = "PA+T"; if(s.matches(pattern1)) { if(s.matches(pathern2)) { printYES(); } else { String[] temp = s.split("P|T"); int aLength = temp[0].length(); int bLength = temp[1].length(); int cLength = temp[2].length(); if(stringLengthMatche(aLength, bLength, cLength)) { printYES(); } else { printNO(); } } } else { printNO(); } } public static void printYES() { System.out.println("YES"); } public static void printNO() { System.out.println("NO"); } public static boolean stringLengthMatche(int aLen, int bLen, int cLen) { if(((cLen- aLen) / aLen ) == bLen - 1) { return true; } return false; } }