Problem:


Problem C: K Poker

Description

時は200X年、K大学で活動する謎のサークルKはその大学の文化祭の最中にK東4Fで彼らが作成した新たなゲーム、Kポーカーを発表した。

このゲームは一見すると普通のポーカーであるが、カードに基本点という要素をつけてポーカーをより奥の深いゲームにしたものである。

カードの基本点はそれぞれのカードに書かれている絵柄によって決まり、手札の点数は手札にある5枚の基本点の合計に手札の役の倍率を掛けたものになる。

この基本点を導入した事により、例えばフルハウスなどの強い役を作ったとしても基本点が0であればブタと同様に扱われてしまい、ワンペアに負けることさえある。

それ以外のルールは通常のポーカーと同じである。

このKポーカーをPCに移植しようと思ったあなたは手札の点数を計算するプログラムを書く事にした。

なおこのゲームは18歳未満の人はプレイ出来ない。

Input

入力は複数のテストケースからなる。

各テストケースの最初の行にはチェックする手札の数Nが書かれている。

次の4行には13個の整数が並びカードの基本点が1-13の順に並んで書かれている。4行のスーツは上から順にスペード、クローバー、ハート、ダイヤの順に並んでいる。

次の行には9個の整数が並び、順にワンペア、ツーペア、スリーカード、ストレート、フラッシュ、フルハウス、フォーカード、ストレートフラッシュ、ロイヤルストレートフラッシュの倍率を表している。役の倍率は必ず昇順となっている。ブタ(役無し、ノーペア)の倍率は常に0である。

次のN行には手札を表す5個の長さ2の異なる文字列が並んでいる。1文字目はカードの数値を表し,A,2,3,4,5,6,7,8,9,T,J,Q,Kのいずれかである。2文字目はカードのスーツを表し、S,C,H,Dのいずれかである。各アルファベットはAはエース、Tは10、Jはジャック、Qはクイーン、Kはキング、Sはスペード、Cはクローバー、Hはハート、Dはダイヤを表す。

入力の数値は全て[0,10000]の範囲に収まっている。

入力はEOFで終わる。

Output

各手札の点数を1行ずつ出力せよ。

連続する2つのテストケースの間には空行を1つ入れること。

役についてはwikipediaのポーカーのページを参照のこと。
なお、ストレートはエースとキングをまたぐ場合があるが、手札が10,ジャック、クイーン、キング、エースの場合にのみストレートとみなされる。

Sample Input

3
0 1 0 1 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 1 0 0 2
0 1 1 0 1 1 1 0 0 0 0 5 5
3 1 1 1 0 1 0 0 1 0 3 0 2
1 1 2 4 5 10 20 50 100
7H 6H 2H 5H 3H
9S 9C 9H 8H 8D
KS KH QH JD TS
10
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9
AS 3D 2C 5D 6H
6S 6C 7D 8H 9C
TS TD QC JD JC
KD KH KC AD 2C
3D 4H 6D 5H 7C
2S KS QS AS JS
TS TD JD JC TH
8H 8D TC 8C 8S
4S 5S 3S 7S 6S
KD QD JD TD AD

Output for Sample Input

25
0
14

0
5
10
15
20
25
30
35
40
45



Sample input:
3
0 1 0 1 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 1 0 0 2
0 1 1 0 1 1 1 0 0 0 0 5 5
3 1 1 1 0 1 0 0 1 0 3 0 2
1 1 2 4 5 10 20 50 100
7H 6H 2H 5H 3H
9S 9C 9H 8H 8D
KS KH QH JD TS
10
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9
AS 3D 2C 5D 6H
6S 6C 7D 8H 9C
TS TD QC JD JC
KD KH KC AD 2C
3D 4H 6D 5H 7C
2S KS QS AS JS
TS TD JD JC TH
8H 8D TC 8C 8S
4S 5S 3S 7S 6S
KD QD JD TD AD


Sample output:
25
0
14

0
5
10
15
20
25
30
35
40
45

Submission status: Wrong Answer

Solution code:
ref = "SCHD"
first = 0
while 1:
	try:
		n = input()
		if first: print
		first = 1
	except:
		break
	P = [map(int,raw_input().split()) for i in range(4)]
	H = map(int,raw_input().split())
	for _ in range(n):
		hand = raw_input().replace("A","1").replace("T","10").replace("J","11").replace("Q","12").replace("K","13").split()
		num  = [int(i[:-1]) for i in hand]
		suit = [ref.index(i[-1]) for i in hand]
		ans  = sum(P[suit[i]][num[i]-1] for i in range(5))
		if len(set(suit)) == 1:
			if sorted(num) == [1,10,11,12,13]:
				ans *= H[8]
			elif sorted([i - min(num) for i in num]) == [0,1,2,3,4]:
				ans *= H[7]
			else:
				ans *= H[4]
		elif sorted([i - min(num) for i in num]) == [0,1,2,3,4]:
			ans *= H[3]
		elif max(num.count(i) for i in set(num)) == 4:
			ans *= H[6]
		elif max(num.count(i) for i in set(num)) == 3:
			if len(set(num)) == 2:
				ans *= H[5]
			else:
				ans *= H[2]
		elif max(num.count(i) for i in set(num)) == 2:
			if len(set(num)) == 3:
				ans *= H[1]
			else:
				ans *= H[0]
		else:
			ans = 0
		print ans

Submission status: Accepted

Solution code:
from collections import Counter
def parse_card(card):
  suit = card[-1]
  num = card[:-1]
  if num == "A":num = 1
  elif num == "K":num = 13
  elif num == "Q":num = 12
  elif num == "J":num = 11
  elif num == "T":num = 10
  else:num = int(num)
  return (num, suit)
  
def parse_hand(hand):
  return sorted((map(parse_card, hand)))

def judge(hand):
  nums = [num for num, _ in hand]
  flash_flag = len({suit for _, suit in hand}) == 1
  head = nums[0]
  straight_flag = [head, head + 1, head + 2, head + 3, head + 4] == nums or \
                  [1, 10, 11, 12, 13] == nums
  dist = sorted(Counter(nums).values())
  
  if nums == [1, 10, 11, 12, 13] and flash_flag:return 9
  if straight_flag and flash_flag:return 8
  if dist == [1, 4]:return 7
  if dist == [2, 3]:return 6
  if flash_flag:return 5
  if straight_flag:return 4
  if dist == [1, 1, 3]:return 3
  if dist == [1, 2, 2]:return 2
  if dist == [1, 1, 1, 2]:return 1
  else:return 0

def score(hand, dic, point):
  ret = 0
  for num, suit in hand:
    ret += dic[suit][num - 1]
  ret *= point[judge(hand)]
  return ret

def main():
  first_flag = True
  while True:
    try:
      n = int(input())
    except EOFError:
      break
    
    if not first_flag:print()
    else:first_flag = False
    
    dic = {}
    dic["S"] = list(map(int, input().split()))
    dic["C"] = list(map(int, input().split()))
    dic["H"] = list(map(int, input().split()))
    dic["D"] = list(map(int, input().split()))
    point = [0] + list(map(int, input().split()))
    for _ in range(n):
      hand = parse_hand(input().split())
      print(score(hand, dic, point))

main()

Submission status: Accepted

Solution code:
card_table = {'A': 0, '2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12}
suit_table = {'S': 0, 'C': 1, 'H': 2, 'D': 3}


def get_numlist(cards):
    numlist = []
    for card in cards:
        numlist.append(card[0])
    return numlist


def is_royal(cards):
    numlist = get_numlist(cards)
    if set(numlist) == set(['T', 'J', 'Q', 'K', 'A']):
        return True
    else:
        return False


def is_fourcard(cards):
    numlist = get_numlist(cards)
    if numlist.count(numlist[0]) == 4 or numlist.count(numlist[1]) == 4:
        return True
    else:
        return False


def is_fullhouse(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 2:
        for num in numlist:
            if numlist.count(num) == 3:
                return True
    return False


def is_flash(cards):
    suitlist = []
    for card in cards:
        suitlist.append(card[1])
    if len(set(suitlist)) == 1:
        return True
    else:
        return False


def is_straight(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 5:
        index_list = []
        for num in numlist:
            index_list.append(card_table[num])
        index_list.sort()
        if index_list == range(index_list[0], index_list[4]+1) or index_list == [0, 9, 10, 11, 12]:
            return True
    return False


def is_threecard(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 3:
            return True
    return False


def is_twopair(cards):
    count = 0
    numlist = get_numlist(cards)
    for num in set(numlist):
        if numlist.count(num) == 2:
            count += 1
        if count == 2:
            return True
    return False


def is_onepair(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 2:
            return True
    return False


def get_hand(cards, rate):
    if is_flash(cards) and is_straight(cards):
        if is_royal(cards):
            return rate[8]
        else:
            return rate[7]
    if is_fourcard(cards):
        return rate[6]
    if is_fullhouse(cards):
        return rate[5]
    if is_flash(cards):
        return rate[4]
    if is_straight(cards):
        return rate[3]
    if is_threecard(cards):
        return rate[2]
    if is_twopair(cards):
        return rate[1]
    if is_onepair(cards):
        return rate[0]
    return 0


def sum_bps(cards, bps):
    bp = 0
    for card in cards:
        num = card_table[card[0]]
        suit = suit_table[card[1]]
        bp += bps[suit][num]
    return bp

first_flag = False
while(1):
    try:
        input_num = int(raw_input())
        if first_flag:
            print
        bps = []
        for count in range(4):
            i = raw_input()
            bps.append(map(int, i.split()))
        i = raw_input()
        rate = map(int, i.split())
        for count in range(input_num):
            i = raw_input()
            cards = i.split()
            hand_rate = get_hand(cards, rate)
            bp = sum_bps(cards, bps)
            print hand_rate * bp
        first_flag = True
    except:
        break

Submission status: Accepted

Solution code:
ref = "SCHD"
first = 0
while 1:
	try:
		n = input()
		if first: print
		first = 1
	except:
		break
	P = [map(int,raw_input().split()) for i in range(4)]
	H = map(int,raw_input().split())
	for _ in range(n):
		hand = raw_input().replace("A","1").replace("T","10").replace("J","11").replace("Q","12").replace("K","13").split()
		num  = [int(i[:-1]) for i in hand]
		suit = [ref.index(i[-1]) for i in hand]
		ans  = sum(P[suit[i]][num[i]-1] for i in range(5))
		if len(set(suit)) == 1:
			if sorted(num) == [1,10,11,12,13]:
				ans *= H[8]
			elif sorted([i - min(num) for i in num]) == [0,1,2,3,4]:
				ans *= H[7]
			else:
				ans *= H[4]
		elif sorted([i - min(num) for i in num]) == [0,1,2,3,4] or sorted(num) == [1,10,11,12,13]:
			ans *= H[3]
		elif max(num.count(i) for i in set(num)) == 4:
			ans *= H[6]
		elif max(num.count(i) for i in set(num)) == 3:
			if len(set(num)) == 2:
				ans *= H[5]
			else:
				ans *= H[2]
		elif max(num.count(i) for i in set(num)) == 2:
			if len(set(num)) == 3:
				ans *= H[1]
			else:
				ans *= H[0]
		else:
			ans = 0
		print ans

Submission status: Accepted

Solution code:
ref = "SCHD"
first = 0
while 1:
	try:
		n = input()
		if first: print
		first = 1
	except:
		break
	P = [map(int,raw_input().split()) for i in range(4)]
	H = map(int,raw_input().split())
	for _ in range(n):
		hand = raw_input().replace("A","1").replace("T","10").replace("J","11").replace("Q","12").replace("K","13").split()
		num  = [int(i[:-1]) for i in hand]
		suit = [ref.index(i[-1]) for i in hand]
		ans  = sum(P[suit[i]][num[i]-1] for i in range(5))
		snum = sorted([i - min(num) for i in num])
		same = max(num.count(i) for i in set(num))
		if len(set(suit)) == 1:
			if snum == [0,9,10,11,12]:
				ans *= H[8]
			elif snum == [0,1,2,3,4]:
				ans *= H[7]
			else:
				ans *= H[4]
		elif snum == [0,1,2,3,4] or snum == [0,9,10,11,12]:
			ans *= H[3]
		elif same == 4:
			ans *= H[6]
		elif same == 3:
			if len(set(num)) == 2:
				ans *= H[5]
			else:
				ans *= H[2]
		elif same == 2:
			if len(set(num)) == 3:
				ans *= H[1]
			else:
				ans *= H[0]
		else:
			ans = 0
		print ans

Submission status: WA: Presentation Error

Solution code:
card_table = {'A': 0, '2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12}
suit_table = {'S': 0, 'C': 1, 'H': 2, 'D': 3}


def get_numlist(cards):
    numlist = []
    for card in cards:
        numlist.append(card[0])
    return numlist


def is_royal(cards):
    numlist = get_numlist(cards)
    if set(numlist) == set(['T', 'J', 'Q', 'K', 'A']):
        return True
    else:
        return False


def is_fourcard(cards):
    numlist = get_numlist(cards)
    if numlist.count(numlist[0]) == 4 or numlist.count(numlist[1]) == 4:
        return True
    else:
        return False


def is_fullhouse(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 2:
        for num in numlist:
            if numlist.count(num) == 3:
                return True
    return False


def is_flash(cards):
    suitlist = []
    for card in cards:
        suitlist.append(card[1])
    if len(set(suitlist)) == 1:
        return True
    else:
        return False


def is_straight(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 5:
        index_list = []
        for num in numlist:
            index_list.append(card_table[num])
        index_list.sort()
        if index_list == range(index_list[0], index_list[4]+1) or index_list == [0, 9, 10, 11, 12]:
            return True
    return False


def is_threecard(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 3:
            return True
    return False


def is_twopair(cards):
    count = 0
    numlist = get_numlist(cards)
    for num in set(numlist):
        if numlist.count(num) == 2:
            count += 1
        if count == 2:
            return True
    return False


def is_onepair(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 2:
            return True
    return False


def get_hand(cards, rate):
    if is_flash(cards) and is_straight(cards):
        if is_royal(cards):
            return rate[8]
        else:
            return rate[7]
    if is_fourcard(cards):
        return rate[6]
    if is_fullhouse(cards):
        return rate[5]
    if is_flash(cards):
        return rate[4]
    if is_straight(cards):
        return rate[3]
    if is_threecard(cards):
        return rate[2]
    if is_twopair(cards):
        return rate[1]
    if is_onepair(cards):
        return rate[0]
    return 0


def sum_bps(cards, bps):
    bp = 0
    for card in cards:
        num = card_table[card[0]]
        suit = suit_table[card[1]]
        bp += bps[suit][num]
    return bp

while(1):
    try:
        input_num = int(raw_input())
        bps = []
        for count in range(4):
            i = raw_input()
            bps.append(map(int, i.split()))
        i = raw_input()
        rate = map(int, i.split())
        for count in range(input_num):
            i = raw_input()
            cards = i.split()
            hand_rate = get_hand(cards, rate)
            bp = sum_bps(cards, bps)
            print hand_rate * bp
        print
    except:
        break

Submission status: WA: Presentation Error

Solution code:
card_table = {'A': 0, '2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12}
suit_table = {'S': 0, 'C': 1, 'H': 2, 'D': 3}


def get_numlist(cards):
    numlist = []
    for card in cards:
        numlist.append(card[0])
    return numlist


def is_royal(cards):
    numlist = get_numlist(cards)
    if set(numlist) == set(['T', 'J', 'Q', 'K', 'A']):
        return True
    else:
        return False


def is_fourcard(cards):
    numlist = get_numlist(cards)
    if numlist.count(numlist[0]) == 4 or numlist.count(numlist[1]) == 4:
        return True
    else:
        return False


def is_fullhouse(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 2:
        for num in numlist:
            if numlist.count(num) == 3:
                return True
    return False


def is_flash(cards):
    suitlist = []
    for card in cards:
        suitlist.append(card[1])
    if len(set(suitlist)) == 1:
        return True
    else:
        return False


def is_straight(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 5:
        index_list = []
        for num in numlist:
            index_list.append(card_table[num])
        index_list.sort()
        if index_list == range(index_list[0], index_list[4]+1) or index_list == [0, 9, 10, 11, 12]:
            return True
    return False


def is_threecard(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 3:
            return True
    return False


def is_twopair(cards):
    count = 0
    numlist = get_numlist(cards)
    for num in set(numlist):
        if numlist.count(num) == 2:
            count += 1
        if count == 2:
            return True
    return False


def is_onepair(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 2:
            return True
    return False


def get_hand(cards, rate):
    if is_flash(cards) and is_straight(cards):
        if is_royal(cards):
            return rate[8]
        else:
            return rate[7]
    if is_fourcard(cards):
        return rate[6]
    if is_fullhouse(cards):
        return rate[5]
    if is_flash(cards):
        return rate[4]
    if is_straight(cards):
        return rate[3]
    if is_threecard(cards):
        return rate[2]
    if is_twopair(cards):
        return rate[1]
    if is_onepair(cards):
        return rate[0]
    return 0


def sum_bps(cards, bps):
    bp = 0
    for card in cards:
        num = card_table[card[0]]
        suit = suit_table[card[1]]
        bp += bps[suit][num]
    return bp

first_flag = False
while(1):
    try:
        if not first_flag:
            print
        input_num = int(raw_input())
        bps = []
        for count in range(4):
            i = raw_input()
            bps.append(map(int, i.split()))
        i = raw_input()
        rate = map(int, i.split())
        for count in range(input_num):
            i = raw_input()
            cards = i.split()
            hand_rate = get_hand(cards, rate)
            bp = sum_bps(cards, bps)
            print hand_rate * bp
        first_flag = True
    except:
        break

Submission status: WA: Presentation Error

Solution code:
card_table = {'A': 0, '2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12}
suit_table = {'S': 0, 'C': 1, 'H': 2, 'D': 3}


def get_numlist(cards):
    numlist = []
    for card in cards:
        numlist.append(card[0])
    return numlist


def is_royal(cards):
    numlist = get_numlist(cards)
    if set(numlist) == set(['T', 'J', 'Q', 'K', 'A']):
        return True
    else:
        return False


def is_fourcard(cards):
    numlist = get_numlist(cards)
    if numlist.count(numlist[0]) == 4 or numlist.count(numlist[1]) == 4:
        return True
    else:
        return False


def is_fullhouse(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 2:
        for num in numlist:
            if numlist.count(num) == 3:
                return True
    return False


def is_flash(cards):
    suitlist = []
    for card in cards:
        suitlist.append(card[1])
    if len(set(suitlist)) == 1:
        return True
    else:
        return False


def is_straight(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 5:
        index_list = []
        for num in numlist:
            index_list.append(card_table[num])
        index_list.sort()
        if index_list == range(index_list[0], index_list[4]+1) or index_list == [0, 9, 10, 11, 12]:
            return True
    return False


def is_threecard(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 3:
            return True
    return False


def is_twopair(cards):
    count = 0
    numlist = get_numlist(cards)
    for num in set(numlist):
        if numlist.count(num) == 2:
            count += 1
        if count == 2:
            return True
    return False


def is_onepair(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 2:
            return True
    return False


def get_hand(cards, rate):
    if is_flash(cards) and is_straight(cards):
        if is_royal(cards):
            return rate[8]
        else:
            return rate[7]
    if is_fourcard(cards):
        return rate[6]
    if is_fullhouse(cards):
        return rate[5]
    if is_flash(cards):
        return rate[4]
    if is_straight(cards):
        return rate[3]
    if is_threecard(cards):
        return rate[2]
    if is_twopair(cards):
        return rate[1]
    if is_onepair(cards):
        return rate[0]
    return 0


def sum_bps(cards, bps):
    bp = 0
    for card in cards:
        num = card_table[card[0]]
        suit = suit_table[card[1]]
        bp += bps[suit][num]
    return bp

first_flag = False
while(1):
    try:
        input_num = int(raw_input())
        if not first_flag:
            print
        bps = []
        for count in range(4):
            i = raw_input()
            bps.append(map(int, i.split()))
        i = raw_input()
        rate = map(int, i.split())
        for count in range(input_num):
            i = raw_input()
            cards = i.split()
            hand_rate = get_hand(cards, rate)
            bp = sum_bps(cards, bps)
            print hand_rate * bp
        first_flag = True
    except:
        break