Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation

Introduction

This theme

AtCoder Beginner Contest 107 B - Grid Compression Difficulty: 434

This theme, string manipulation Ruby

ruby.rb


h, w = gets.split.map(&:to_i)
a = []
h.times do |i|
  s = gets.chomp
  a.push(s) if s.index('#')
end
(w - 1).downto(0) do |i|
  if a.all?{|x| x[i] == '.'}
    a.each do |x|
      x[i] = ''
    end
  end
end
puts a

count.rb


  a.push(s) if s.count('.') != w
  a.push(s) if s.index('#')
  a.push(s) if s.contain?('#')

The count method, ʻindex method, contain` method, and other regular expressions are also OK.

index.rb


      x[i] = ''

For * Ruby *, you can directly replace the characters specified in the index. ** Addition ** I reviewed the code from the comments I received. Python

python.py


h, w = map(int, input().split())
a = []
for i in range(h):
    s = input()
    if s.count('.') != w:
        a.append(s)
b = []
for i in range(w):
    f = True
    for x in a:
        if x[i] != '.':
            f = False
    if f:
        b.append(i)
for x in a:
    for i in range(len(x)):
        f = True
        for j in b:
            if i == j:
                f = False
        if f:
            print(x[i], end='')
    print()

find.py


    if s.count('.') != w:
    if s.find('#') == -1:

You can also use the find method.

perl.pl


chomp (my ($h, $w) = split / /, <STDIN>);
my @a;
for my $i (1..$h) {
  chomp (my $s = <STDIN>);
  if (scalar(grep {$_ eq '.'} (split '', $s)) != $w) {
    push @a, $s;
  }
}
for my $i (1..$w) {
  my $f = 1;
  for my $x (@a) {
    $f = 0 if substr($x, $w - $i, 1) ne '.';
  }
  if ($f) {
    for my $x (@a) {
      substr($x, $w - $i, 1) = '';
    }
  }
}
for my $x (@a) {
  print "$x\n";
}

grep.pl


  if (scalar(grep {$_ eq '.'} (split '', $s)) != $w) {
  if (index($s, '#') == -1) {

ʻIndex` is also OK. Java

java.java


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = Integer.parseInt(sc.next());
        int w = Integer.parseInt(sc.next());
        List<String> a = new ArrayList<>();
        for (int i = 0; i < h; i++) {
            String s = sc.next();
            if (s.contains("#")) {
                a.add(s);
            }
        }
        sc.close();
        List<Integer> b = new ArrayList<>();
        for (int i = 0; i < w; i++) {
            boolean f = false;
            for (String x : a) {
                if (x.charAt(i) == '#') {
                    f = true;
                    break;
                }
            }
            if (f) {
                b.add(i);
            }
        }
        for (String x : a) {
            for (int i = 0; i < w; i++) {
                if (b.contains(i))
                    System.out.print(x.charAt(i));
            }
            System.out.println();
        }
    }
}

contains.java


            if (s.contains("#")) {

There seems to be no easy way to count the number of ., so I'm using the contains method. ** Addition ** From the comment section, I got the idea of split with .. Also, I received a comment that charAt is better than substring, so I modified the code.

Ruby Python Perl Java
Code length 226 Byte 457 Byte 410 Byte 1042 Byte
Execution time 9 ms 35 ms 5 ms 219 ms
memory 1788 KB 4468 KB 512 KB 25032 KB

Summary

Recommended Posts

Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby AtCoder ABC110 C String Manipulation
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
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
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
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 networkx AtCoder ABC168 D Adjacency list
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
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
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solve AtCoder ABC 186 with Python
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
AtCoder ABC168 A case expression solved in Ruby and Python
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
Solved AtCoder ABC 114 C-755 with Python3
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Solve AtCoder ABC168 with python (A ~ D)
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and 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 ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
[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] Solve A problem of ABC101 ~ 169 with Python
MessagePack-Try to link Java and Python with RPC
AtCoder ABC 174 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 ABC187 Python
AtCoder ABC188 Python
Solving the Lorenz 96 model with Julia and Python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
AtCoder ABC 175 Python