【PAT】(乙级)1003. 我要通过!(20)
来源:互联网 发布:红蜘蛛软件 路由器 编辑:程序博客网 时间:2024/06/05 20:39
1003. 我要通过!(20)
- 时间限制 400 ms
- 内存限制 65536 kB
- 代码长度限制 8000 B
- 判题程序 Standard
- 作者 CHEN, Yue
一、题目
“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。 得到“答案正确”的条件是:
- 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
- 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
- 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。
现在就请你为PAT写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。
输入格式:每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。
输出格式:每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。
输入样例:
8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever输出样例:
YES
YES
YES
YES
NO
NO
NO
NO
二、Tips
Tips:仔细看三个条件。理解透了再动手敲代码。尤其是要理解第三个条件,第三个条件一直递推下去,P和T之间无论有几个A都有可能是正确的,需要递归验证。
三、代码
import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 1003. 我要通过!(20) * https://www.patest.cn/contests/pat-b-practise/1003 * Created by Relish on 2016/8/4. */public class Main { static boolean isPAT(String s) { if (s.matches("A*(PAT)A*") && s.substring(0, s.indexOf("PAT")).length() == (s.substring(s.indexOf("PAT") + "PAT".length()).length())) { return true; } else if (s.matches("A*(PA+T)A*")) { try { String a = s.substring(0, s.indexOf("PA")); Matcher m = Pattern.compile("PA*T").matcher(s); String b = ""; if (m.find()) { b = m.group(); } b = b.replace("P", "").replace("T", "").substring(1); String c = s.substring(a.length() + b.length() + 3).substring(a.length()); String newS = a + "P" + b + "T" + c; return isPAT(newS);//递归验证 } catch (IndexOutOfBoundsException e) { return false; } } else { return false; } } public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); for (int i = 0; i < n; i++) { System.out.println(isPAT(cin.next()) ? "YES" : "NO"); } }}
0 0
- PAT-乙级-1003. 我要通过!(20)
- 1003. 我要通过!(20) PAT乙级
- 1003. 我要通过!(20)-PAT乙级
- PAT乙级 1003. 我要通过!(20)
- PAT乙级1003. 我要通过!(20)
- PAT乙级 1003. 我要通过!(20)
- [PAT乙级]1003. 我要通过!(20)
- 1003. 我要通过!(20)--PAT乙级
- PAT乙级1003. 我要通过!(20)
- PAT乙级 1003. 我要通过!(20)
- PAT(乙级) 1003.我要通过
- 【PAT】(乙级)1003. 我要通过!(20)
- PAT 乙级 1003.我要通过!(20)
- PAT乙级 1003. 我要通过!
- 浙大PAT乙级 1003. 我要通过!
- PAT乙级-1003. 我要通过!
- [PAT-乙级]1003.我要通过!
- PAT乙级题1003.我要通过!
- android studio sharedpreference基本操作
- android 加载图片oom若干方案小结
- 对大括号{}的作用的理解
- jmeter参数化设置
- C#中键值对类型Hashtable与Dictionary比较和相关用法
- 【PAT】(乙级)1003. 我要通过!(20)
- iOS 开发问题小结two
- 110. Balanced Binary Tree
- activity的启动模式
- .
- hdu1875 畅通工程再续--prim
- iOS面试系列2
- SmartFoxServer 2X 简介
- RSA加密算法浅谈