Codeforces Round #612 (Div. 2) A. Angry Students
Angry Students
原题链接 https://codeforces.com/contest/1287/problem/A
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
input 1 4 PPAP
output 1
input 3 12 APPAPPPAPPPP 3 AAP 3 PPA
output 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.
解题思路:找长度最长的AP序列,然后判断该序列中P的数量。需要注意的是如果不字符串不包括A,那么应该返回0。
AC代码如下
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
while(n-->0) {
int t=cin.nextInt();
String s=cin.next();
char[] ch=s.toCharArray();
int index=s.indexOf('A');
int count=0;
int ans=0;
if(index==-1) {
System.out.println(0);
continue;
}
for(int i=index+1;i<ch.length;i++) {
if(ch[i]=='P') {
count++;
}else {
ans=Math.max(count, ans);
count=0;
}
}
ans=Math.max(count, ans);
System.out.println(ans);
}
}
}
- 谈谈C# 4.0新特性“缺省参数”的实现
- 如何实现对上下文(Context)数据的统一管理 [提供源代码下载]
- 不再和人工智能对弈?柯洁:我要食言了
- Spring实战——Profile
- Spring实战——XML和JavaConfig的混合配置
- ambari安装指南
- Spring实战——通过Java代码装配bean
- WCF技术剖析之三十一: WCF事务编程[中篇]
- Spring实战——无需一行xml配置实现自动化注入
- 基于改进人工蜂群算法的K均值聚类算法(附MATLAB版源代码)
- RabbitMQ入门-Routing直连模式
- WCF技术剖析之三十二:一步步创建一个完整的分布式事务应用
- .NET的资源并不限于.resx文件,你可以采用任意存储形式[上篇]
- RabbitMQ入门-消息订阅模式
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- python第三十课--异常(异常对象传递过程)
- python第三十课--异常(with as操作)
- linux系统运维企业常见面试题集合(二)
- Linux系统Shell编程—企业生产案例(一)
- python第三十一课--递归(1.简单递归函数的定义和使用)
- python第三十一课--递归(2.遍历某个路径下面的所有内容)
- python第三十一课--递归(3.递归的弊端)
- python第三十二课——栈
- linux系统运维企业常见面试题集合(三)
- python第三十二课——队列
- python第三十四课——1.匿名函数的定义和使用
- python第三十四课——2.匿名函数配合容器函数的使用
- 专家专栏|使用agent2自定义插件采集通过MQTT协议发送的数据
- Linux系统双网卡绑定配置教程
- python第三十五课——生成器