首页 > 试题广场 >

1003. 我要通过!(20)

[编程题]1003. 我要通过!(20)
  • 热度指数:4020 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。
现在就请你为PAT写一个自动裁判程序,判定哪些字符串是可以获得“答案正确”的。

输入描述:
每个测试输入包含1个测试用例。第1行给出一个自然数n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过100,且不包含空格。


输出描述:
每个字符串的检测结果占一行,如果该字符串可以获得“答案正确”,则输出YES,否则输出NO。
示例1

输入

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA

输出

YES
YES
YES
YES
NO
NO
NO
NO
推荐
本题是数学推导类型

条件1就不说了,太简单了
条件2,xPATx是正确的类型,其中x是由n个A组成(n>=0),
所以AAPATAA都是正确的,并且P和T两边A的个数相等。

关键是条件3
如果aPbTc是正确的,那么aPbATca也是正确的,其中a,b,c都是n个A组成(n>=0)。

由条件2知,如果aPbTc是正确的,那么它必须满足条件2,
所以a的长度和c的长度必定是相等的,且b等于1。

另外可以根据P和T间A的个数,判断通过条件3迭代了几次,,,就是P和T间A的个数减去1。

在迭代的过程正,a保持不变,c每迭代一次,长度加a,因此可以推出才c最后的长度。

大致原理就是由xPATx ----> aPbTc ----> aPbATca

代码如下
import java.io.PrintStream;
import java.text.ParseException;
import java.util.Scanner;

public class Main {
	public static Scanner in = new Scanner(System.in);
	public static PrintStream out = System.out;

	public static boolean test(String line){
		if(line==null)
			return false;
		
		int i=0;
		int len = line.length();
		
		int pCount,aCount,tCount;
		pCount=aCount=tCount=0;
		
		for(i=0;i<len;++i){
			if(line.charAt(i)=='P'){
				++pCount;
				
			}else if(line.charAt(i)=='A'){
				++aCount;
				
			}else if(line.charAt(i)=='T'){
				++tCount;
				
			}else{
				// 不满足条件1: 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符
				return false;
			}
		}
		
		// P和T只能出现一次 ,且A的个数大于一次
		if(!(pCount==tCount && aCount>=1 && pCount==1 ))
			return false;
		
		int indexP = line.indexOf('P');
		int indexT = line.indexOf('T');
		
		int leftA = indexP; // P前面A的个数
		int rightA = len - indexT - 1; // T后面A的个数
		int middleA = indexT - indexP - 1; // P和T直间A的个数
		
		int n = middleA - 1; // 迭代次数
		if(rightA != n*leftA+leftA)
			return false;
		
		return true;
	}
	public static void main(String[] args) throws ParseException {
		int N = in.nextInt();
		in.nextLine(); // 读取空白符
		
		for(int i=0;i<N;++i){
			if(test(in.nextLine()))
				out.println("YES");
			else
				out.println("NO");
		}
		
	}
}

编辑于 2015-08-18 22:30:15 回复(2)

用正则匹配应该最简单了吧!

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Pattern pattern = Pattern.compile("(A*)P(A+)T(A*)");
        boolean flag;
        while (n-- > 0) {
            String s = in.next();
            Matcher matcher = pattern.matcher(s);
            flag = matcher.matches()
                    && matcher.group(1).length() * matcher.group(2).length() == matcher.group(3).length();
            System.out.println(flag ? "YES" : "NO");
        }
    }
}
编辑于 2019-01-20 13:16:19 回复(1)

的确是数学方法。。
刚开始打算构造字符串硬肛。。被续了。。
PAT那边的测试用例比较完整,这边pass的,那边跪了。

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(input.readLine());
        boolean stop = false;
        for (int i = 0; i < num; i++) {
            String in = input.readLine();
            // check if there are characters besides "P A T"
            for (int j = 0; j < in.length(); j++) {
                String sub = in.substring(j, j + 1);
                if (sub.matches("(P|A|T)")) {
                    ;
                }
                else {
                    System.out.println("NO");
                    stop = true;
                    break;
                }
            }
            // skip the rest of the loop
            if (stop) {
                stop = false;
                continue;
            }

            if (in.matches("A*PATA*")) {
                check1(in);
            }
            else if (in.matches("A*PA+TA*")) {
                check1(in);
            }
            else {
                System.out.println("NO");
            }
        }
        input.close();
    }

    private static void check1(String in) {
        int aLenngth = in.indexOf('P');
        int endLength = in.length() - 1 - in.indexOf('T');
        int numOfA = in.indexOf('T') - in.indexOf('P') - 1;

        if (aLenngth * numOfA != endLength)
            System.out.println("NO");
        else
            System.out.println("YES");
    }
}
编辑于 2017-09-07 13:30:55 回复(1)

问题信息

难度:
3条回答 26722浏览

热门推荐

通过挑战的用户