pat 乙级1104c++
时间: 2025-03-25 21:17:45 浏览: 25
### 关于 PAT 乙级 1104 题的 C++ 解法
以下是针对 PAT 乙级 1104 题的一个可能的解决方案。该问题通常涉及字符串处理和简单的逻辑判断。
#### 代码实现
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s); // 获取输入的整个行
int countA = 0, countP = 0, countT = 0;
// 统计 A、P 和 T 的数量
for(char c : s){
if(c == 'A') countA++;
else if(c == 'P') countP++;
else if(c == 'T') countT++;
}
// 判断是否满足条件
if(countP == 1 && countT == 1 && countA >=2 ){
int posP = s.find('P');
int posT = s.find('T');
if(posP < posT){ // P 必须在 T 前面
string subStr = s.substr(posP + 1, posT - posP - 1);
bool isAllA = true;
for(char c : subStr){
if(c != 'A'){
isAllA = false;
break;
}
}
if(isAllA){
int m = posP; // AP 中 A 的个数
int n = subStr.length(); // PT 中 A 的个数
// 检查剩余部分是否全是 A 并且长度等于 mn
string remaining = s.substr(0,m) + s.substr(posT+1);
if(remaining.length()==m*n){
bool isValid=true;
for(int i=0;i<remaining.length();i++){
if(remaining[i]!='A'){
isValid=false;
break;
}
}
if(isValid){
cout << "true";
}else{
cout << "false";
}
}else{
cout << "false";
}
}else{
cout << "false";
}
}else{
cout << "false";
}
}else{
cout << "false";
}
return 0;
}
```
#### 说明
上述程序实现了对给定字符串的分析,验证其是否符合特定模式 `AP^nTAmn`,其中:
- 字符串中只有一个 ‘P’ 和一个 ‘T’;
- ‘P’ 出现在 ‘T’ 之前;
- ‘P’ 后‘T’前的部分全部由 ‘A’ 构成;
- 整体结构需严格遵循指定的比例关系[^6]。
阅读全文
相关推荐

















