A. Angry Students(Codeforces 思维题)

题目链接
A. Angry Students
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
It’s a walking tour day in SIS.Winter, so t groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.

Initially, some students are angry. Let’s describe a group of students by a string of capital letters “A” and “P”:

“A” corresponds to an angry student
“P” corresponds to a patient student
Such string describes the row from the last to the first student.

Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index i in the string describing a group then they will throw a snowball at the student that corresponds to the character with index i+1 (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don’t throw a snowball since there is no one in front of them.

Let’s look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry.

Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group.

Input
The first line contains a single integer t — the number of groups of students (1≤t≤100). The following 2t lines contain descriptions of groups of students.

The description of the group starts with an integer ki (1≤ki≤100) — the number of students in the group, followed by a string si, consisting of ki letters “A” and “P”, which describes the i-th group of students.

Output
For every group output single integer — the last moment a student becomes angry.

Examples
inputCopy
1
4
PPAP
outputCopy
1
inputCopy
3
12
APPAPPPAPPPP
3
AAP
3
PPA
outputCopy
4
1
0
Note
In the first test, after 1 minute the state of students becomes PPAA. After that, no new angry students will appear.

In the second tets, state of students in the first group is:

after 1 minute — AAPAAPPAAPPP
after 2 minutes — AAAAAAPAAAPP
after 3 minutes — AAAAAAAAAAAP
after 4 minutes all 12 students are angry
In the second group after 1 minute, all students are angry.

题意思路:就是要统计A后最长能延续的A序列
代码1:

#include<bits/stdc++.h>
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        string str;
        cin>>str;
        int temp=0,ans=0;
        for(int i=n-1;i>=0;i--)
        {
            if(str[i]=='A')
            {
                ans=max(ans,temp);
            }
            else
            {
                temp++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

代码2:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1001;
char a[maxn];
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        scanf("%s",a);
        int temp=0,ans=0;
        for(int i=n-1;i>=0;i--)
        {
            if(a[i]=='A')
            {
                while(a[i+1]=='P')
                {
                    temp++;
                    i++;
                }
            }
            ans=max(temp,ans);
            temp=0;
        }
        cout<<ans<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值