Angry Students

Description
It’s a walking tour day in SIS.Winter, so tt 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 ii in the string describing a group then they will throw a snowball at the student that corresponds to the character with index i+1i+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 tt — the number of groups of students (1≤t≤1001≤t≤100). The following 2t2t lines contain descriptions of groups of students.

The description of the group starts with an integer kiki (1≤ki≤1001≤ki≤100) — the number of students in the group, followed by a string sisi, consisting of kiki letter``s “A” and “P”, which describes the ii-th group of students.

Output
For every group output single integer — the last moment a student becomes angry.
题目大意
就是一群脾气不好的人在互殴 互相问候。
给定一串有A和P组成的字符串,A代表生气,P代表未生气。A会在同一时刻向右发射雪球,遇到P会使P变为A,发射一次的时间为1,问使最后转换完成花费的时间。
思路
就是要求出A右侧最长的连续P的个数。我的做法是在检测到A之后开始计数P的个数,并且每一次计数后更新最大连续P个数。

#include <stdio.h>
int main()
{
    int t;
    int x;
    scanf("%d",&t);
    while(t--)
    {
        int i;
        char c;
        int flag=0;//标识符flag
        int maxP=0;//最长连续P
        int numP=0;//连续P的个数
        scanf("%d",&x);
        for(i=0;i<=x;i++)
        {
            scanf("%c",&c);
            if(c=='A')//遇到A后将flag变为1,并归零连续P的数目
            {
                flag=1;
                numP=0;
                continue;
            }
            if(flag)//遇到A后开始计数P,并且每一次计数后与当前最大连续P个数比较,不断更新
            {
                numP++;
                if(numP>maxP)
                    maxP=numP;
            }
        }
        printf("%d\n",maxP);//输出最大连续P的个数
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值