Problem:


テニス

ジョウ君とヤエさんは昼休みにテニスをします。ただし、昼休みは時間が限られているので、短い時間で終わるように、得点について以下の３つのルールで行います。

 相手が３点以下のときに先に５点とれば勝ち。

 ４対４の同点になったときは、その直後に連続して２点とった方が勝ち。

 ４対４の後に双方が１点ずつとったときは引き分け。

以下の図は、ジョウ君とヤエさんの試合で起こり得るすべての状況を表しています。丸の中の左の数がジョウ君の得点、右がヤエさんの得点です。Ａと書いた矢印はジョウ君が１点とったことを、Ｂと書いた矢印はヤエさんが１点とったことを表します。

ジョウ君とヤエさんの得点が与えられたとき、試合開始からその得点状況になるまでに、上の図で通り得るすべての経路を列挙するプログラムを作成してください。

入力

入力は１つのデータセットからなる。入力データは以下の形式で与えられる。

j y

j (0 ≤ j ≤ 6) がジョウ君の得点、y (0 ≤ y ≤ 6) がヤエさんの得点である。ただし、j と y がともに 0 であることはない。また、j が 6 のときは y は 4、y が 6 のときは j は 4 である。

出力

上の図で、試合開始（0-0と書かれた丸）から与えられた得点が書かれた丸までのすべての経路を出力する。経路は図の矢印に添えられた英字(A,B)の列で表し、辞書式順序（英和辞書で単語が並んでいる順番）になるように並べる。１つの経路を１行に出力する。経路の前後には空白を出力しない。

入出力例

入力例1

2 2

出力例1

AABB
ABAB
ABBA
BAAB
BABA
BBAA

入力例2

5 1

出力例2

AAAABA
AAABAA
AABAAA
ABAAAA
BAAAAA



Sample input:
2 2


Sample output:
AABB
ABAB
ABBA
BAAB
BABA
BBAA

Submission status: Wrong Answer

Solution code:
def solve(a,b):
  if a+b==0: return [""]
  res=[]
  if 0<a and b<5: res+=[s+"A" for s in solve(a-1,b)]
  if 0<b and a<5: res+=[s+"B" for s in solve(a,b-1)]
  return res

while 1:
  try: a,b=map(int,input().split())
  except: break
  for s in sorted(solve(a,b)):
    print(s)

Submission status: Wrong Answer

Solution code:
def dfs(game):
    a,b = game.count("A"),game.count("B")
    if (max(a,b) <= 4 or a == 5 and b < 4 or a < 4 and b == 5 or 
        a == b == 5 or a == 6 and b == 4 or a == 4 and b == 6):
            possible.add(game)
            if game+"A" not in possible: dfs(game+"A")
            if game+"B" not in possible: dfs(game+"B")
            
i,j = map(int,raw_input().split())
possible = set([])
dfs("")
ans = []
for game in possible:
    if (game.count("A"),game.count("B")) == (i,j): ans.append(game)
print  "\n".join(sorted(ans))

Submission status: Wrong Answer

Solution code:
# Aizu Problem 0285: Tennis

import sys, math, os, itertools

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


a, b = [int(_) for _ in input().split()]
L = ['A'] * a + ['B'] * b
perms = set()
for perm in itertools.permutations(L):
    perms.add(''.join(perm))

for perm in sorted(list(perms)):
    print(perm)

Submission status: Wrong Answer

Solution code:
# Aizu Problem 0285: Tennis

import sys, math, os, itertools

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


a, b = [int(_) for _ in input().split()]
L = ['A'] * a + ['B'] * b
perms = set()
for perm in itertools.permutations(L):
    if (a == 6 or b == 6) and (perm[:].count('A') != 4 or perm[:].count('B') != 4):
        continue
    perms.add(''.join(perm))

for perm in sorted(list(perms)):
    print(perm)

Submission status: Wrong Answer

Solution code:
# Aizu Problem 0285: Tennis

import sys, math, os, itertools

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


a, b = [int(_) for _ in input().split()]

L = ['A'] * a + ['B'] * b
perms = set()
for perm in itertools.permutations(L):
    if (a == 6 or b == 6) and (perm[:8].count('A') != 4 or perm[:8].count('B') != 4):
        continue
    perms.add(''.join(perm))

for perm in sorted(list(perms)):
    print(perm)

Submission status: Wrong Answer

Solution code:
# Aizu Problem 0285: Tennis

import sys, math, os, itertools

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")

def get_games(game):
    a, b = game.count('A'), game.count('B')
    if (a == 5 and b < 4) or (b == 5 and a < 4) or a == b == 5 or \
       (a == 6 and b == 4) or (b == 6 and a == 4):
        games.add(game)
    elif max(a, b) <= 4 or a == b == 4 or (a == 5 and b == 4) or (b == 5 and a == 4):
        if game + 'A' not in games:
            get_games(game + 'A')
        if game + 'B' not in games:
            get_games(game + 'B')

a, b = [int(_) for _ in input().split()]
games = set()
get_games("")

ans = []
for game in games:
    if game.count('A') == a and game.count('B') == b:
        ans.append(game)

for game in sorted(ans):
    print(game)

Submission status: Accepted

Solution code:
def solve(a,b):
  if a+b==0: return [""]
  if a>=5 and b<=4: return [s+"A" for s in solve(a-1,b)]
  if b>=5 and a<=4: return [s+"B" for s in solve(a,b-1)]
  res=[]
  if a>0: res+=[s+"A" for s in solve(a-1,b)]
  if b>0: res+=[s+"B" for s in solve(a,b-1)]
  return res

while 1:
  try: a,b=map(int,input().split())
  except: break
  for s in sorted(solve(a,b)):
    print(s)

Submission status: Accepted

Solution code:
def dfs(game):
    a,b = game.count("A"),game.count("B")
    if  (a == 5 and b < 4 or a < 4 and b == 5 or a == b == 5 or 
            a == 6 and b == 4 or a == 4 and b == 6):
        possible.add(game)
    elif (max(a,b) <= 4 or a == b == 4 or a == 5 and b == 4 or a == 4 and b == 5):
        possible.add(game)
        if game+"A" not in possible: dfs(game+"A")
        if game+"B" not in possible: dfs(game+"B")
            
j,y = map(int,raw_input().split())
possible = set([])
dfs("")
ans = []
for game in possible:
    if (game.count("A"),game.count("B")) == (j,y): ans.append(game)
print  "\n".join(sorted(ans))

Submission status: Accepted

Solution code:
class Node(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
        if a <= 3 and b == 5:
            self.left = None
            self.right = None
        elif a == 5 and b <= 3:
            self.left = None
            self.right = None
        elif (a, b) in ((5, 5), (6, 4), (4, 6)):
            self.left = None
            self.right = None

        else:
            self.left = Node(a+1, b)
            self.right = Node(a, b+1)
    def prints(self):
        print "%d %d" % (self.a, self.b)

def parse(n, a, b, s):
    if n.a == a and n.b == b:
        print s
    else:
        if n.left != None:
            s1 = s +  'A'
            parse(n.left, a, b, s1)
            s2 = s + 'B'
            parse(n.right, a, b, s2)

read = map(int, raw_input().split())
n = Node(0, 0)
st = ""
parse(n, read[0], read[1], st)

Submission status: Accepted

Solution code:
# Aizu Problem 0285: Tennis

import sys, math, os, itertools

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")

def get_games(game):
    a, b = game.count('A'), game.count('B')
    if (a == 5 and b < 4) or (b == 5 and a < 4) or a == b == 5 or \
       (a == 6 and b == 4) or (b == 6 and a == 4):
        games.add(game)
    elif max(a, b) <= 4 or a == b == 4 or (a == 5 and b == 4) or (b == 5 and a == 4):
        games.add(game)
        if game + 'A' not in games:
            get_games(game + 'A')
        if game + 'B' not in games:
            get_games(game + 'B')

A, B = [int(_) for _ in input().split()]
games = set()
get_games("")

ans = []
for game in games:
    if game.count('A') == A and game.count('B') == B:
        ans.append(game)

for game in sorted(ans):
    print(game)

Submission status: Accepted

Solution code:
dic = {}
dic[(0, 0)] = [""]
dic[(1, 0)] = ["A"]
dic[(0, 1)] = ["B"]
for s in range(2, 9):
  for i in range(min(5, s + 1)):
    j = s - i
    if j >= 5 or j <= -1:
      continue
    dic[(i, j)] = []
    if i - 1 >= 0:
      dic[(i, j)] += [lst + "A" for lst in dic[(i - 1, j)]]
    if j - 1 >= 0:
      dic[(i, j)] += [lst + "B" for lst in dic[(i, j - 1)]]
for i in range(4):
  dic[(5, i)] = [lst + "A" for lst in dic[(4, i)]]
  dic[(i, 5)] = [lst + "B" for lst in dic[(i, 4)]]

dic[(5, 4)] = [lst + "A" for lst in dic[(4, 4)]]
dic[(4, 5)] = [lst + "B" for lst in dic[(4, 4)]]
dic[(5, 5)] = [lst + "A" for lst in dic[(4, 5)]] + [lst + "B" for lst in dic[(5, 4)]]
dic[(6, 4)] = [lst + "A" for lst in dic[(5, 4)]]
dic[(4, 6)] = [lst + "B" for lst in dic[(4, 5)]]
j, y = map(int, input().split())
print(*sorted(dic[(j, y)]), sep="\n")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Tennis
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0285

"""
import sys


Ans = []

def solve(j, y, score = ''):
    if score.count('A') == j and score.count('B') == y:
        Ans.append(score)
        return
    if score.count('A') == 5 and score.count('B') < 4 or score.count('A') < 4 and score.count('B') == 5:
        return
    if score.count('A') < j:
        solve(j, y, score+'A')
    if score.count('B') < y:
        solve(j, y, score+'B')


def main(args):
    j, y = map(int, input().split())
    solve(j, y)
    print(*sorted(Ans), sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
j, y = map(int, input().split())
memo = {}
ans = []
def dfs(a, b, R):
    key = (a, b)
    if key == (j, y):
        ans.append(''.join(R))
    if key in ((5, 4), (4, 5)) or (a < 5 and b < 5):
        R.append('A')
        dfs(a+1, b, R)
        R[-1] = 'B'
        dfs(a, b+1, R)
        R.pop()
dfs(0, 0, [])
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
def parse(j, y, p, J, Y):
    if j == J and y == Y:
        print(p)
        return
    elif j == 5 and y <= 3 or y == 5 and j <= 3:
        return

    if  j > J or y > Y:
        return

    parse(j+1, y, p + "A", J, Y)
    parse(j, y+1, p + "B", J, Y)

J,Y = map(int, input().split())
parse(0, 0, "", J, Y)