import java.io.FileReader; import java.math.BigInteger; import java.util.Scanner; public class EndOfWorld { private void work() throws Exception { Scanner sc = new Scanner(new FileReader("endofworld.in")); while (sc.hasNext()) { char[] s = sc.next().toCharArray(); BigInteger res = BigInteger.ONE.shiftLeft(s.length).subtract( BigInteger.ONE); char lastPeg = 'B'; for (int i = s.length - 1; i >= 0; i--) { if (s[i] == lastPeg) { // if the i_th disk is moved where it should be, we made 2^i moves res = res.subtract(BigInteger.ONE.shiftLeft(i)); } else { // otherwise we have to move all the smaller pegs to the 'spare' before we move the i_th disk lastPeg = getLastPeg(s[i], lastPeg); } } System.out.println(res); } } private char getLastPeg(char a, char b) { // NOT a AND NOT b if (a == 'A') return b == 'B' ? 'C' : 'B'; if (a == 'B') return b == 'A' ? 'C' : 'A'; return b == 'A' ? 'B' : 'A'; } public static void main(String[] args) throws Exception { new EndOfWorld().work(); } }