-
题目


-
思路
- 对读入的每个字符串分别处理,记录其中p、t、其他字符的个数,以及p、t的位置,比较左侧a的个数 * 中间a的个数是否等于右侧a的个数
-
代码
#include <iostream> #include <string> using namespace std; int main(){ int n; cin>>n; string s; for(int i=0;i<n;i++){ cin>>s; int p_num=0,other=0,t_num=0; //记录不同字符个数 int p_loc=-1,t_loc=-1; for(int j=0;j<s.length();j++){ if(s[j]=='P'){ p_num++; p_loc=j; }else if(s[j]=='T'){ t_num++; t_loc=j; }else if(s[j]!='A'){ other++; } } int l=p_loc; int m=t_loc-p_loc-1; int r=s.length()-1-t_loc; if(other!=0||p_num!=1||t_num!=1||t_loc-p_loc<2){ //答案错误 cout<<"NO"<<endl; continue; } if(l*m==r){ cout<<"YES"<<endl; }else{ cout<<"NO"<<endl; } } return 0; }
PAT笔记:1003 我要通过! (20分)
最新推荐文章于 2025-07-08 13:14:23 发布
本文详细解析了一道关于字符串处理的竞赛题目,介绍了如何通过遍历字符串并统计特定字符的数量和位置来判断字符串是否符合特定规则的方法。文章通过具体代码示例展示了整个解题过程,适合对字符串操作和算法感兴趣的读者。
573

被折叠的 条评论
为什么被折叠?



