AtCoder diverta 2019 Programming Contest C - AB Substrings Difficulty: 911
Ce thème, opération de chaîne de caractères Ruby Chaque chaîne donnée est comptée par «BXA» BX «XA». Parmi ceux-ci, «BXA» devient «BXA» même si les «BXA» sont combinés, donc traitez-le d'abord pour réduire le nombre de «BXA». De même, même si «BXA» et «BX» sont combinés, c'est «BX», et même si «XA» et «BXA» sont combinés, c'est «XA», alors utilisez-le pour compter.
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
Pour * Ruby *, s.scan (" AB "). Size compte le nombre de ʻAB`.
string.rb
    if s[-1] == "A"
C'est incroyable que vous puissiez accéder au dernier caractère avec [-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)
Je ne suis pas familier avec les fonctions * Python *, donc je compte le nombre de ʻAB` à partir du nombre de caractères avant et après le remplacement. 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;
Pour * Perl *, il existe un idiome. Après tout, Perl est idéal pour le traitement des caractères. 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();
Dans le cas de * Java *, le nombre de ʻAB` est compté à partir du nombre de caractères avant et après remplacement comme en Python.
| Ruby | Python | Perl | Java | |
|---|---|---|---|---|
| Longueur du code | 364 Byte | 428 Byte | 507 Byte | 1071 Byte | 
| Temps d'exécution | 22 ms | 62 ms | 9 ms | 250 ms | 
| Mémoire | 1788 KB | 3188 KB | 640 KB | 38660 KB | 
Site référencé
Recommended Posts