当前位置: 代码迷 >> 综合 >> Java - PAT - 1003. 我要通过!(20)
  详细解决方案

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

热度:63   发布时间:2023-10-09 21:11:50.0

题目地址: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

  相关解决方案