#!/usr/bin/env perl use strict; use warnings; my $in = <>; chomp $in; my @vs = (split //,$in); my $countA = 0; my $res = 0; while (my ($index, $c) = each(@vs)) { if ($c eq 'A') { $res += $index - $countA; $countA++; } } print $res . "\n";