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 |
Referenced site
Recommended Posts