1003 我要通过!

题目

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

参考博客

分析

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

代码

  1. /**
  2. * Score 20
  3. * Run Time 71ms
  4. * @author wowpH
  5. * @version 2.1
  6. */
  7. package problemsets.cn.pintia.wowph.t1003.v2;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. public class Main {
  12. /**
  13. * Save n strings.
  14. */
  15. private static String[] str;
  16. /**
  17. * The number of strings in a test case.
  18. */
  19. private static int n;
  20. /**
  21. * Custom input class.
  22. *
  23. * @author wowpH
  24. */
  25. private static class Scanner {
  26. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  27. /**
  28. * @return The next token.
  29. */
  30. public String next() {
  31. String x = null;
  32. try {
  33. x = br.readLine();
  34. } catch (IOException e) {
  35. System.out.println("Error reading input stream.");
  36. }
  37. return x;
  38. }
  39. /**
  40. * @return The <tt>int</tt> scanned from the input.
  41. */
  42. public int nextInt() {
  43. int x = 0;
  44. try {
  45. x = Integer.parseInt(next());
  46. } catch (NumberFormatException e) {
  47. System.out.println("Error converting String to Integer.");
  48. }
  49. return x;
  50. }
  51. }
  52. /**
  53. * Enter the number of strings and an array of strings.
  54. */
  55. private static void input() {
  56. Scanner sc = new Scanner();
  57. n = sc.nextInt();
  58. str = new String[n]; // 保存字符串
  59. for (int i = 0; i < n; i++) {
  60. str[i] = sc.next();
  61. }
  62. }
  63. /**
  64. * Determines whether the string with <tt>index</tt> in the string array
  65. * <tt>str</tt> is correct.
  66. *
  67. * @param index 字符串数组的下标,指向当前需要判断的字符串
  68. * @return <tt>true</tt> 如果字符串正确
  69. */
  70. private static boolean judge(int index) {
  71. int numP, numT, numOther; // 'P'的个数,'T'的个数,除了PAT以外的字符的个数
  72. numP = numT = numOther = 0;
  73. char[] s = str[index].toCharArray(); // 当前字符串
  74. int len = s.length; // 长度
  75. int indexP = 0, indexT = 0; // 'P'的下标,'T'的下标
  76. // 统计字符的个数,不用统计'A'的个数
  77. for (int i = 0; i < len; i++) {
  78. if ('P' == s[i]) {
  79. numP++;
  80. indexP = i;
  81. } else if ('T' == s[i]) {
  82. numT++;
  83. indexT = i;
  84. } else if ('A' != s[i]) {
  85. numOther++;
  86. }
  87. }
  88. if (1 != numP || 1 != numT || 0 != numOther || indexT - indexP <= 1) {
  89. return false;
  90. }
  91. // 核心代码
  92. if (indexP * (indexT - indexP - 1) != (len - indexT - 1)) {
  93. return false;
  94. }
  95. return true;
  96. }
  97. public static void main(String[] args) {
  98. input(); // 输入
  99. for (int i = 0; i < n; i++) {
  100. if (judge(i)) {
  101. System.out.println("YES"); // 正确
  102. } else {
  103. System.out.println("NO"); // 错误
  104. }
  105. }
  106. }
  107. }

补充

  • 字母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. 【原】Python基础-函数

    #不定长参数,这里prams是一个元组集合def print_params(*prams): for e in prams: print(e) print(prams) #输出('xxx', (1, ...

  2. selenium反爬机制

    使用selenium模拟浏览器进行数据抓取无疑是当下最通用的数据采集方案,它通吃各种数据加载方式,能够绕过客户JS加密,绕过爬虫检测,绕过签名机制.它的应用,使得许多网站的反采集策略形同虚设.由于se ...

  3. 2018-2019-2 《网络对抗技术》Exp9 Web安全基础 20165114

    Exp9 Web安全基础 目录 一.实验内容 二.基础问题回答 (1)SQL注入攻击原理,如何防御 (2)XSS攻击的原理,如何防御 (3)CSRF攻击原理,如何防御 三.实践过程记录 3.1 注入缺 ...

  4. 小福bbs——项目需求分析

    # 一.简单了解 这个作业属于哪个课程 班级链接 这个作业要求在哪里 作业要求的链接 团队名称 小福bbs 这个作业的目标 第一个版本,根据项目预期情况形成 作业的正文 小福bbs--项目需求分析 其 ...

  5. 从结构到性能,一文概述XGBoost、Light GBM和CatBoost的同与不同

    尽管近年来神经网络复兴并大为流行,但是 boosting 算法在训练样本量有限.所需训练时间较短.缺乏调参知识等场景依然有其不可或缺的优势.本文从算法结构差异.每个算法的分类变量时的处理.算法在数据集 ...

  6. 请解释一下 JavaScript 的同源策略

    概念: 同源策略是客户端脚本(尤其是Netscape Navigator2.0,其目的是防止某个文档或脚本从多个不同源装载. 这里的同源策略指的是:协议,域名,端口相同,同源策略是一种安全协议. 指一 ...

  7. <javaScript>通过getElementsByTagName获取标签的class值

    console.log(p[1].id); console.log(p.item(1).id); console.log(p[2].getAttribute("class")); ...

  8. SurfaceView动态背景效果实现

    package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.*; imp ...

  9. 阶段5 3.微服务项目【学成在线】_day18 用户授权_15-细粒度授权-我的课程细粒度授权-实现

    先定义接口 实现接口 service 需要通过conpanyId去查询课程的列表 定义dao 要查课程的图片 名称 等相关信息.所以使用Mybatis来实现 定义Mapper 看这个dao里面方法在哪 ...

  10. Pyhthon3之使用__slots__

    正常情况下,我们定义了一个class,创建了一个class实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: >>> class Student( ...