博客
关于我
【PAT乙级】1003 我要通过!
阅读量:190 次
发布时间:2019-02-28

本文共 1941 字,大约阅读时间需要 6 分钟。

为了解决这个问题,我们需要编写一个自动裁判程序,判断给定的字符串是否满足特定的条件,从而输出“答案正确”或“答案错误”。

方法思路

  • 输入处理:读取输入的字符串数量和每个字符串。
  • 字符检查:确保字符串只包含P、A、T三种字符。
  • 统计字符:统计字符串中P、A、T各自的数量,并检查是否存在至少一个P和一个T。
  • 结构检查:根据题目条件,检查字符串的结构是否符合要求。具体来说,P前面的A的数量乘以P和T之间的A的数量必须等于T后面的A的数量,并且P和T之间必须至少有一个A。
  • 解决代码

    #include 
    #include
    #include
    using namespace std;int main() { int t; cin >> t; for (int i = 0; i < t; ++i) { string s; cin >> s; bool valid = true; // 检查是否只包含P、A、T for (char c : s) { if (c != 'P' && c != 'A' && c != 'T') { valid = false; break; } } if (!valid) { cout << "NO" << endl; continue; } // 统计P、A、T的数量 int P = 0, A = 0, T = 0; int aqnum = 0, aznum = 0, ahnum = 0; // 遍历字符串 for (int j = 0; j < s.size(); ++j) { if (s[j] == 'P') { ++P; } else if (s[j] == 'A') { ++A; } else if (s[j] == 'T') { ++T; } } if (P == 0 || T == 0) { cout << "NO" << endl; continue; } // 统计各个部分的A的数量 int left = 0; int mid = 0; int right = 0; for (int j = 0; j < s.size(); ++j) { if (s[j] == 'A') { if (j < P) { left++; } else if (j > P && j < T) { mid++; } else if (j > T) { right++; } } } // 检查条件 if (aznum > 0 && aqnum * aznum == ahnum) { cout << "YES" << endl; } else { cout << "NO" << endl; } }}

    代码解释

  • 输入处理:读取字符串数量t,然后逐个读取每个字符串s
  • 字符检查:遍历字符串,检查是否只包含P、A、T三种字符。如果有其他字符,标记为无效并输出“NO”。
  • 统计字符:统计字符串中P、A、T的数量。如果P或T的数量为0,直接输出“NO”。
  • 结构检查:统计P前面的A的数量(left),P和T之间的A的数量(mid),以及T后面的A的数量(right)。然后检查是否满足条件,即left * mid == rightmid > 0。满足条件则输出“YES”,否则输出“NO”。
  • 转载地址:http://nqoi.baihongyu.com/

    你可能感兴趣的文章
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>