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);
}
}
}
- 关于Java你不知道的10件事
- 03-移动端开发教程-CSS3新特性(下)
- 大数据面试题整理
- 02-移动端开发教程-CSS3新特性(中)
- 强悍!Java 9 中的9个新特性
- 神经网络反向传播梯度计算数学原理
- Spring Boot 1.5.10 发布:修复重要安全漏洞!!!
- 从零开始写项目第三篇【在线聊天和个人收藏夹】
- TiDB 源码阅读系列文章(六)Select 语句概览
- 从零开始写项目第四篇【搭建Linux环境】
- salesforce零基础学习(八十六)Ajax Toolkit (VF页面中使用及javascript action使用)
- salesforce零基础学习(八十五)streaming api 简单使用(接近实时获取你需要跟踪的数据的更新消息状态)
- 深入nDPI
- 44个Java代码性能优化总结
- 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 数组属性和方法
- 给你一个优秀的Django工程模板
- 纯css实现选项卡功能
- linux每日一练文件专题之常用文件命令(附昨日答案)
- 搞懂JavaScript全局变量与局部变量,看这篇文章就够了
- Day3-linux用户管理
- rbind的坑?
- 全文检索工具Lucene入门教程
- 数据库技术:数据库连接池,Commons DbUtils,批处理,元数据
- elasticsearch-快速入门
- 数据库技术:JDBC,预处理对象,事务控制
- elasticsearch-DSL高级查询语法
- 数据库技术:XML
- 利用logstash将mysql多表数据增量同步到es
- 纯CSS实现自定义单选框和复选框
- 基于Linux安装Mysql5.7