AtCoder dwango Programming Contest B in Ruby, Perl and Java

Introduction

This theme

The theme of this time is aggregation by reduce Ruby For example, the character string 1251251252525 is converted to 25-> ʻato make it easier to count characters. =>1a1a1aaa The number of consecutive ʻas is (1, 1, 3), so add up the combinations for each.

bit.rb


s = gets.chomp

s.gsub!(/25/, 'a')
p = []
c = 0
0.upto(s.size - 1) do |i|
  if s[i] == 'a'
    c += 1
  elsif c > 0
    p.push(c)
    c = 0
  end
  p.push(c) if i == s.size - 1 && c > 0
end
sum = 0
0.upto(p.size - 1) do |i|
  sum += (1..p[i]).reduce{|a, b| a + b}
end
puts sum

reduce.rb


  sum += (1..p[i]).reduce{|a, b| a + b}
  sum += (1..p[i]).reduce(&:+)
  sum += (1..p[i]).inject(:+)
  sum += (1..p[i]).sum

The reduce part can be either inject or sum (ruby 2.4 or later). ** Addition ** Added reduce (&: +) from the comments.

Perl

perl.pl


use v5.18; # strict say state
use List::Util qw/reduce/;

chomp (my $s = <STDIN>);
$s =~ s/25/a/g;
my $c = 0;
my @p;
for my $i (0..length($s)-1) {
  if (substr($s, $i, 1) eq 'a') {
    $c++;
  } elsif ($c > 0) {
    push @p, $c;
    $c = 0;
  }
  if ($i == length($s)-1 && $c > 0) {
    push @p, $c;
  }
}
my $sum = 0;
for my $i (0..$#p) {
  $sum += reduce {$a + $b} 1..$p[$i];
}
say $sum;

reduce.pl


use List::Util qw/reduce sum/;

  $sum += reduce {$a + $b} 1..$p[$i];
  $sum += sum(1..$p[$i]);

The reduce part can also be sum. Java

java.java


import java.util.*;
import java.util.stream.IntStream;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        sc.close();
        s = s.replace("25", "a");
        List<Integer> p = new ArrayList<>();
        int c = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.substring(i, i + 1).equals("a")) {
                c++;
            } else if (c > 0) {
                p.add(c);
                c = 0;
            }
            if (i == s.length() - 1 && c > 0) {
                p.add(c);
            }
        }
        int sum = 0;
        for (int i = 0; i < p.size(); i++) {
            sum += IntStream.rangeClosed(1, p.get(i)).reduce(0, (a, b) -> a + b);
        }
        System.out.println(sum);
    }
}

reduce.java


            sum += IntStream.rangeClosed(1, p.get(i)).reduce(0, (a, b) -> a + b);
            sum += IntStream.rangeClosed(1, p.get(i)).sum();

The reduce part can also be sum.

Ruby Perl Java
Code length 284 Byte 411 Byte 847 Byte
Execution time 34 ms 28 ms 284 ms
memory 4092 KB 2816 KB 33228 KB

Summary

Referenced site

Recommended Posts

AtCoder dwango Programming Contest B in Ruby, Perl and Java
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Solve AtCoder Beginner Contest 151 in java
Solve AtCoder Beginner Contest 150 in java
Solve AtCoder Beginner Contest 153 in java
Solve AtCoder Beginner Contest 175 in java
Solve AtCoder Beginner Contest 152 in java
Solve AtCoder Beginner Contest 156 in java
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Solving with Ruby, Perl and Java AtCoder ABC 136 D Breadth-first search
AtCoder Beginner Contest 169 A, B, C with ruby
Constraint programming in Java
Solving with Ruby and Java AtCoder ABC129 D 2D array
Java programming (variables and data)
[Java] Basic terms in programming
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Encoding and Decoding example in Java
[Ruby] then keyword and case in
StringBuffer and StringBuilder Class in Java
Write keys and values in Ruby
Understanding equals and hashCode in Java
AtCoder Beginner Contest 167 C Problem (Java)
Hello world in Java and Gradle
Let's think about what declarative programming is in Java and Elm (Part 1)
I wrote EX25 of AtCoder Programming Guide for beginners (APG4b) in java.
Use OpenCV_Contrib (ArUco) in Java! (Part 2-Programming)
Difference between final and Immutable in Java
Make bubble sort and selection sort in Ruby
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
[Java] for Each and sorted in Lambda
Java programming (classes and instances, main methods)
Java programming (static clauses and "class variables")
[Ruby] Classification and usage of loops in Ruby
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Arrylist and linked list difference in java
Program PDF headers and footers in Java
Learn Flyweight patterns and ConcurrentHashMap in Java
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
diverta 2019 Programming Contest A & B & C & D
Discrimination of Enums in Java 7 and above