--- title: "最大价值" created: 2025-11-28 tags: - 算法 --- # 最大价值 ## 题目 [最大价值](https://www.acwing.com/problem/content/description/4606/) ![[image-1763f128.png]] ## 思路分析 对于P不用管 它价值为0 一定不是答案 只要找到A 去用指针j往该位置的右边挪 数出现几个p 记录在res里面 用max去维护res j指的如果是P就做一遍res=max(res,j-i) 反正不是最大也不会被保存 如果指的是A 就直接break掉本次 如果是APPP的情况 右边全是P j就需要一个终止 即循环条件 using namespace std; #define endl '\n' const int N=110; string str; int T,n; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>T; while(T--){ cin>>n>>str; int res=0; for(int i=0;i