AtCoder Beginner Contest 175 A Problem "Rainy Season" Explanation (C ++, Python3, Java)

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

Problem summary

Output the maximum number of consecutive characters for " R ".

Constraint

|S| = 3 -Each character in $ S $ is 'S' or 'R'

Commentary

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.

Example of answer for each language

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.

Example solution 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))
Example solution in C ++

{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;
  }
}
Java answer example

{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

AtCoder Beginner Contest 175 A Problem "Rainy Season" Explanation (C ++, Python3, Java)
AtCoder Beginner Contest 166 A Explanation of Problem "A? C" (Python3, C ++, Java)
AtCoder Beginner Contest 167 A Problem "Registration" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 169 A Explanation of Problem "Multiplication 1" (Python3, C ++, Java)
AtCoder Beginner Contest 176 A Explanation of problem "Takoyaki" (Python3, C ++, Java)
AtCoder Beginner Contest 174 A Problem "Air Conditioner" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 176 C Problem "Step" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 177 A Problem "Don't be late" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 165 A Problem "We Love Golf" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 174 B Problem "Distance" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 177 B Problem "Substring" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 169 B Problem "Multiplication 2" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 175 B Problem "Making Triangle" Explanation (C ++, Python3, Java)
AtCoder Beginner Contest 176 B Problem "Multiple of 9" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 174 C Problem (Python)
AtCoder Beginner Contest 173 B Problem "Judge Status Summary" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 177 C Problem "Sum of product of pairs" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 167 B Problem "Easy Linear Programming" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 175 Task A --Rainy Season Vivid Answer (Python)
AtCoder Beginner Contest # 002 C Problem
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
AtCoder Regular Contest # 002 C Problem
Atcoder Beginner Contest 152 Kiroku (python)
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
ABC127 A, B, C Explanation (python)
ABC166 in Python A ~ C problem
ABC126 A, B, C Explanation (python)
Python beginner Atcoder memo @ KEYENCE 2020, ABC problem
[AtCoder] Solve ABC1 ~ 100 A problem with Python
AtCoder Beginner Contest: D Question Answers Python
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
AtCoder Beginner Contest 177
AtCoder Beginner Contest 179
AtCoder Beginner Contest 172
AtCoder Beginner Contest 180
AtCoder Beginner Contest 173
Atcoder Beginner Contest 153
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC183 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
AtCoder Beginner Contest 152 Review
AtCoder Beginner Contest 181 Note
AtCoder Beginner Contest 187 Note
AtCoder Beginner Contest 160 Review
AtCoder Beginner Contest 178 Review
AtCoder Beginner Contest 180 Note
AtCoder Beginner Contest 166 Review
AtCoder Beginner Contest 167 Review
AtCoder Beginner Contest 182 Note
AtCoder Beginner Contest 164 Review
AtCoder Beginner Contest 169 Review
AtCoder Beginner Contest 181 Review