1003. 我要通过!(20)--Java

使用了String结构
使用String的正则表达式用来匹配字符串是否符合情况

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;
    }
}

猜你喜欢

转载自www.cnblogs.com/huangZ-H/p/11334098.html