This is Rute.
AtCoder Beginner Contest 175 A I will explain the problem "Rainy Season".
Problem URL: https://atcoder.jp/contests/abc175/tasks/abc175_a
Output the maximum number of consecutive characters for " R "
.
・'S'
or 'R'
Define ls = [].
Receives $ S $ as a string.
Set now = 0. This will be used later.
Perform the following iterative processing.
・ If S [i](the i character of S)
is "R"
Add 1 to now.
・ If not
Insert the value of now into ls and set the value of now to 0.
Finally, insert the value of now into ls after iterating.
Of the numbers in ls, the largest number is the answer, so you can output this.
Or, since the constraint is quite small, it seems that you can calculate in advance how many characters 'R'
exists for all character strings and output each time with conditional branching.
Python3 is a method using a loop, and C ++ and Java are solved by a method of conditional branching on all character strings. It is also possible to AC by using a loop in C ++ and Java, and by conditional branching in all character strings in Python3.
{ABC175A.py}
S = input()
ls = []
now = 0
for i in range(3):
if S[i] == "R":
now += 1
else:
ls.append(now)
now = 0
ls.append(now)
print(max(ls))
{ABC175A.cpp}
#include<bits/stdc++.h>
using namespace std;
int main(){
string S;
cin >> S;
if (S == "RRR"){
cout << 3 << endl;
}else if (S == "RRS"){
cout << 2 << endl;
}else if (S == "SRR"){
cout << 2 << endl;
}else if (S == "SRS"){
cout << 1 << endl;
}else if (S == "RSR"){
cout << 1 << endl;
}else if (S == "RSS"){
cout << 1 << endl;
}else if (S == "SSR"){
cout << 1 << endl;
}else{
cout << 0 << endl;
}
}
{ABC175A.java}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String s = scan.next();
if (s.equals("RRR")){
System.out.println(3);
}else if (s.equals("RRS")){
System.out.println(2);
}else if (s.equals("SRR")){
System.out.println(2);
}else if (s.equals("RSR")){
System.out.println(1);
}else if (s.equals("RSS")){
System.out.println(1);
}else if (s.equals("SRS")){
System.out.println(1);
}else if (s.equals("SSR")){
System.out.println(1);
}else{
System.out.println(0);
}
}
}
Comparing whether the strings are the same using '=='
may result in a conditional branch that is different from the output expected by the Java reference. For more information, please see here.
Recommended Posts