Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation

Introduction

This theme

AtCoder diverta 2019 Programming Contest C - AB Substrings Difficulty: 911

This theme, string manipulation Ruby Each given string is counted by BXA`` BX XA. Of these, BXA becomes BXA even if BXAs are combined, so process this first to reduce the number of BXAs. Similarly, even if BXA and BX are combined, it is BX, and even if XA and BXA are combined, it is XA, so use that to count.

ruby.rb


n = gets.to_i
w = x = y = z = 0
n.times do |i|
  s = gets.chomp
  w += s.scan("AB").size
  if s[0] == "B"
    if s[-1] == "A"
      x += 1
    else
      y += 1
    end
  elsif s[-1] == "A"
    z += 1
  end
end
if x > 1
  w += x - 1
  x = 1
end
if [x, y, z].max == y
  w += x
  w += z
elsif [x, y, z].max == z
  w += x
  w += y
end
puts w

scan.rb


  w += s.scan("AB").size

For * Ruby *, s.scan ("AB"). Size counts the number of ʻAB`.

string.rb


    if s[-1] == "A"

It's amazing that you can access the last character with [-1]. Python

python.py


import re

n = int(input())
w = x = y = z = 0
for i in range(n):
    s = input()
    t = re.sub(r'AB', "X", s)
    w += len(s) - len(t)
    if s[0] == "B":
        if s[-1] == "A":
            x += 1
        else:
            y += 1
    elif s[-1] == "A":
        z += 1
if x > 1:
    w += x - 1
    x = 1
if max(x, y, z) == y:
    w += x
    w += z
elif max(x, y, z) == z:
    w += x
    w += y
print(w)

len.py


    t = re.sub(r'AB', "X", s)
    w += len(s) - len(t)

I'm not familiar with * Python * functions, so I'm counting the number of ʻAB` from the number of characters before and after replacement. Perl

perl.pl


use List::Util qw/max/;

chomp (my $n = <STDIN>);
my ($w, $x, $y, $z);
for my $i (1..$n) {
  chomp (my $s = <STDIN>);
  $w++ while $s =~ /AB/g;
  if (substr($s, 0, 1) eq "B") {
    if (substr($s, -1, 1) eq "A") {
      $x++;
    } else {
      $y++;
    }
  } elsif (substr($s, -1, 1) eq "A") {
    $z++;
  }
}
if ($x > 1) {
  $w += $x - 1;
  $x = 1;
}
if (max($x, $y, $z) == $y) {
  $w += $x;
  $w += $z;
} elsif (max($x, $y, $z) == $z) {
  $w += $x;
  $w += $y;
}
print "$w\n";

reg.pl


  $w++ while $s =~ /AB/g;

For * Perl *, there is a idiom. After all, Perl is great for character processing. Java

java.java


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int w = 0, x = 0, y = 0, z = 0;
        for (int i = 0; i < n; i++) {
            String s = sc.next();
            String t = s.replaceAll("AB", "X");
            w += s.length() - t.length();
            if (s.substring(0, 1).equals("B")) {
                if (s.substring(s.length() - 1, s.length()).equals("A")) {
                    x++;
                } else {
                    y++;
                }
            } else if (s.substring(s.length() - 1, s.length()).equals("A")) {
                z++;
            }
        }
        sc.close();
        if (x > 1) {
            w += x - 1;
            x = 1;
        }
        if (Math.max(x, Math.max(y, z)) == y) {
            w += x;
            w += z;
        } else if (Math.max(x, Math.max(y, z)) == z) {
            w += x;
            w += y;
        }
        System.out.println(w);
    }
}

replaceAll.java


            String t = s.replaceAll("AB", "X");
            w += s.length() - t.length();

In the case of * Java *, the number of ʻAB` is counted from the number of characters before and after replacement as in Python.

Ruby Python Perl Java
Code length 364 Byte 428 Byte 507 Byte 1071 Byte
Execution time 22 ms 62 ms 9 ms 250 ms
memory 1788 KB 3188 KB 640 KB 38660 KB

Summary

Referenced site

Recommended Posts

Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby AtCoder ABC110 C String Manipulation
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 167 B Problem "Easy Linear Programming" Explanation (Python3, C ++, Java)
Benchmark for C, Java and Python with prime factorization
AtCoder Beginner Contest 176 C Problem "Step" Explanation (Python3, C ++, Java)
Programming with Python and Tkinter
Atcoder Acing Programming Contest Python
AtCoder Beginner Contest 166 A Explanation of Problem "A? C" (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 167 A Problem "Registration" Explanation (Python3, C ++, Java)
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
AtCoder Beginner Contest 169 B Problem "Multiplication 2" Explanation (Python3, C ++, Java)
[Swift / Ruby / Python / Java] Object-oriented programming
Scraping with Node, Ruby and Python
Create AtCoder Contest appointments on Google Calendar with Python and GAS
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 175 B Problem "Making Triangle" Explanation (C ++, Python3, Java)
Solved AtCoder ABC 114 C-755 with Python3
AtCoder Beginner Contest 175 A Problem "Rainy Season" Explanation (C ++, Python3, Java)
List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)
AtCoder Beginner Contest 176 B Problem "Multiple of 9" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 174 C Problem (Python)
AtCoder Beginner Contest 174 A Problem "Air Conditioner" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 177 A Problem "Don't be late" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 173 B Problem "Judge Status Summary" Explanation (Python3, C ++, Java)
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
AtCoder Beginner Contest 177 C Problem "Sum of product of pairs" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 165 A Problem "We Love Golf" Explanation (Python3, C ++, Java)
Eating and comparing programming languages: Python and Ruby
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby