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

 

转载于:https://www.cnblogs.com/huangZ-H/p/11334098.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值