1003 我要通过!

题目

  判断字符串是否符合给定的规则。更多内容点击标题。

参考博客

分析

  规律:num_a * num_b = num_c。字符串a中字母A的个数乘以字符串b中字母A的个数等于字符串c中字母A的个数。

代码

/**
* Score 20
* Run Time 71ms
* @author wowpH
* @version 2.1
*/
package problemsets.cn.pintia.wowph.t1003.v2; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class Main {
/**
* Save n strings.
*/
private static String[] str;
/**
* The number of strings in a test case.
*/
private static int n; /**
* Custom input class.
*
* @author wowpH
*/
private static class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); /**
* @return The next token.
*/
public String next() {
String x = null;
try {
x = br.readLine();
} catch (IOException e) {
System.out.println("Error reading input stream.");
}
return x;
} /**
* @return The <tt>int</tt> scanned from the input.
*/
public int nextInt() {
int x = 0;
try {
x = Integer.parseInt(next());
} catch (NumberFormatException e) {
System.out.println("Error converting String to Integer.");
}
return x;
}
} /**
* Enter the number of strings and an array of strings.
*/
private static void input() {
Scanner sc = new Scanner();
n = sc.nextInt();
str = new String[n]; // 保存字符串
for (int i = 0; i < n; i++) {
str[i] = sc.next();
}
} /**
* Determines whether the string with <tt>index</tt> in the string array
* <tt>str</tt> is correct.
*
* @param index 字符串数组的下标,指向当前需要判断的字符串
* @return <tt>true</tt> 如果字符串正确
*/
private static boolean judge(int index) {
int numP, numT, numOther; // 'P'的个数,'T'的个数,除了PAT以外的字符的个数
numP = numT = numOther = 0;
char[] s = str[index].toCharArray(); // 当前字符串
int len = s.length; // 长度
int indexP = 0, indexT = 0; // 'P'的下标,'T'的下标
// 统计字符的个数,不用统计'A'的个数
for (int i = 0; i < len; i++) {
if ('P' == s[i]) {
numP++;
indexP = i;
} else if ('T' == s[i]) {
numT++;
indexT = i;
} else if ('A' != s[i]) {
numOther++;
}
}
if (1 != numP || 1 != numT || 0 != numOther || indexT - indexP <= 1) {
return false;
}
// 核心代码
if (indexP * (indexT - indexP - 1) != (len - indexT - 1)) {
return false;
}
return true;
} public static void main(String[] args) {
input(); // 输入
for (int i = 0; i < n; i++) {
if (judge(i)) {
System.out.println("YES"); // 正确
} else {
System.out.println("NO"); // 错误
}
}
} }

补充

  • 字母P和字母T的个数都为1。
  • 不能有其他字母。
  • 字母P和字母T之间至少有1个字母A

备注

  刚来PAT没什么经验。这个居然不是多组输入。而且还是一次性读完n个字符串再一次性输出。我还以为是读取一个字符串数出一个结果。

PAT(B)1003 我要通过!(Java)的更多相关文章

  1. PAT甲级1003. Emergency

    PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...

  2. PAT 乙级 1003

    题目 题目地址:PAT 乙级 1003 题解 规律观察题,本题的关键在于把题读懂,同时还有几个比较容易疏忽的地方需要注意:总之这道题要考虑的东西更多,细节上也要特别注意: 规律:“如果 aPbTc 是 ...

  3. 图论 - PAT甲级 1003 Emergency C++

    PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...

  4. PAT(B) 1021 个位数统计(Java)

    题目链接:1021 个位数统计 (15 point(s)) 代码 /** * Score 15 * Run Time 93ms * @author wowpH * @version 1.0 */ im ...

  5. PAT(B) 1019 数字黑洞(Java)

    题目链接:1019 数字黑洞 (20 point(s)) 分析 输入正整数n后,将n转成int型数组nArr[4] 用Arrays.sort(int[] a)方法将数组nArr非递减排序 很显然,非递 ...

  6. PAT 乙级 1003.我要通过! C++/Java

    1003 我要通过! (20 分) 题目来源 “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则 ...

  7. PAT乙级--1003

    1003. 我要通过!(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue "答案正确"是 ...

  8. PAT乙级1003

    1003 我要通过! (20 point(s)) “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”, ...

  9. [C++]PAT乙级1003. 我要通过!(17/20)

    /* 1003. 我要通过!(20) “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错 ...

  10. PAT 乙级 1003 我要通过!(20) C++版

    1003. 我要通过!(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue “答案正确”是自动判题系统给出的最 ...

随机推荐

  1. 享学课堂java架构vip课程

    1.wps文档地址 https://docs.qq.com/doc/DRVNLUndvTmFSdEhO 2.百度网盘地址 https://pan.baidu.com/s/1uxaTzJZHKrsw_H ...

  2. python3编程基础之一:操作

    基本操作有:读数据.写数据.运算.控制.输入.输出.语句块 1.读取数据: num1 = 50 num2 = num1 //通过num2取得num1的值,这就是逻辑上的读取 测试数据:print(nu ...

  3. elasticsearch java插入索引批量操作

    1.单条所以插入//第一个参数:索引名:第二个参数:索引类型:第三个参数:索引ID(相同的id时修改数据,默认为随机字符串)IndexResponse indexResponse = client.p ...

  4. 关于Vue.use()详解

    问题 相信很多人在用Vue使用别人的组件时,会用到 Vue.use() .例如:Vue.use(VueRouter).Vue.use(MintUI).但是用 axios时,就不需要用 Vue.use( ...

  5. Interacted Action-Driven Visual Tracking Algorithm

    文章来源:Attentional Action-Driven Deep Network for Visual Object Tracking   博士论文(2017年8月份完稿) http://s-s ...

  6. python-pptx add_image_picture

  7. MediaPlayer: MediaPlayer中的prepare方法和prepareAsync方法的区别

    prepare方法是将资源同步缓存到内存中,一般加载本地较小的资源可以用这个,如果是较大的资源或者网络资源建议使用prepareAsync方法,异步加载.但如果想让资源启动,即start()起来,因为 ...

  8. 阶段5 3.微服务项目【学成在线】_day18 用户授权_10-前端集成认证授权-需求分析

    4 前端集成认证授权 4.1 需求分析 截至目前认证授权服务端的功能已基本完成,本章实现前端集成认证授权功能. 前端集成认证授权功能需要作如下工作: 1.前端页面校验用户的身份,如果用户没有登录则跳转 ...

  9. Qt编写自定义控件50-迷你仪表盘

    一.前言 这个控件取名叫迷你仪表盘,是以为该控件可以缩小到很小很小的区域显示,非常适合小面积区域展示仪表数据使用,还可以手动触摸调节进度,是我个人觉得最漂亮小巧的一个控件.初次看到类似的控件是在一个音 ...

  10. python基础之坑爹正则表达式

    python基础之坑爹正则表达式 概述 re模块就是python语言中的正则表达式,拆出来单独写一条blog是因为正则表达式本身就是比较庞大的知识,写具体些让自己以后方便查找. IP: ^(25[0- ...