字符串:Angry Students

题目
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 letters “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.
Examples

input
1
4
PPAP
output
1

题意
有一队小孩,分为两类A小孩和P小孩,每分钟A小孩会向下一小孩丢雪球,如果下一个小孩是P,那被雪球砸到之后就会变为A(最后一个小孩不会再仍雪球因为他后面已经没有人了),问什么时候不在有小孩丢雪球,或者说是什么时间队列中A小孩的数量最多。
思路
这是一道简单的字符串的题目,很容易想到字符串中两个A之间越远需要的时间越长,比如APPAPPPAPPPPA中,最后两个A之间差的最多,所以它俩的距离就是所需时间的大小。

#include<iostream>
#include<cstdio>

using namespace std;
int t,n,s,ans,count;
char k[110];
int max(int a,int b){
 return a>b?a:b;
}
int main(){
	 int i;
	 cin>>t;
	 while(t--){
 		ans=-1,s=0,count=0;
  		cin>>n;
 	 	cin>>k;
  		for(i=0;i<n;i++) //这个循环就是为了求出两个A之间最大距离 
  			if(k[i]=='A')ans=max(ans,s),s=0,count=1;
  			else if(count)s++;
  		ans=max(ans,s);
  		cout <<ans<<endl;
  	}
  	return 0}
发布了4 篇原创文章 · 获赞 0 · 访问量 35

猜你喜欢

转载自blog.csdn.net/qq_40791907/article/details/104341426
今日推荐