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

题目地址:1003. 我要通过!(20)


1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。


思路:

条件一最最基本,暂且不管。条件二,xPATx 就是正确的形式;在此基础上,如果 aPbTc 是正确的,那么 aPbATca 也是正确的。如果二者放在一起考虑,就是中间每增加一个A,后面就增加一个a,似乎数学关系出来了。我们知道aPbTc中a b c 段都只能包含“A",其长度分别为len(a)、len(b)、len(c),则其关系满足len(a)*len(b) = len(c)!这完美的契合了条件二与条件三,xPATx 就是当len(b) = 1,(a=x,c=c,b=A)的情况,在此基础上演化到条件三B中每增加一个A,c中相应增加一段”a“以上的乘法关系式成立。

import java.util.Scanner;  
public class Main{  
	public static void main(String[] args){  
		Scanner sc = new Scanner(System.in);  
		int n = sc.nextInt();
		sc.nextLine();
		for(int i=0 ;i<n ;i++){
			String s = sc.nextLine();
			String news = s;
			if(news.contains("P")&&news.contains("A")&&news.contains("T")){
				news = news.replace("A", "");
				news = news.replace("P", "");
				news = news.replace("T", "");
				news = news.replace("\\s+", "");
				if(news.isEmpty()){
					int p = s.indexOf("P");
					int t = s.indexOf("T");
					int len = s.length();
					int b = t-p-1;
					int c = len -t -1;
					if(p*b==c){
						System.out.println("YES");
					}else{
						System.out.println("NO");
					}
				}else{
					System.out.println("NO");
				}
			}else{
				System.out.println("NO");
			}
		}
	}
}





思路转自http://www.ithao123.cn/content-4199350.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值