Problem:


Problem A: Whist

Whist is a game played by four players with a standard deck of playing cards. The players seat
around a table, namely, in north, east, south, and west. This game is played in a team-play
basis: the players seating opposite to each other become a team. In other words, they make two
teams we could call the north-south team and the east-west team.

Remember that the standard deck consists of 52 cards each of which has a rank and a suit. The
rank indicates the strength of the card and is one of the following: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack,
queen, king, and ace (from the lowest to the highest). The suit refers to the type of symbols
printed on the card, namely, spades, hearts, diamonds, and clubs. The deck contains exactly
one card for every possible pair of a rank and a suit, thus 52 cards.

One of the four players (called a dealer) shuffles the deck and deals out all the cards face down,
one by one, clockwise from the player left to him or her. Each player should have thirteen cards.
Then the last dealt card, which belongs to the dealer, is turned face up. The suit of this card is
called trumps and has a special meaning as mentioned below.

A deal of this game consists of thirteen tricks. The objective for each team is winning more
tricks than another team. The player left to the dealer leads the first trick by playing one of
the cards in his or her hand. Then the other players make their plays in the clockwise order.
They have to play a card of the suit led if they have one; they can play any card otherwise. The
trick is won by the player with the highest card of the suit led if no one plays a trump, or with
the highest trump otherwise. The winner of this trick leads the next trick, and the remaining
part of the deal is played similarly. After the thirteen tricks have been played, the team winning
more tricks gains a score, one point per trick in excess of six.

Your task is to write a program that determines the winning team and their score for given
plays of a deal.

Input

The input is a sequence of datasets. Each dataset corresponds to a single deal and has the
following format:

Trump

CardN,1 CardN,2 ... CardN,13

CardE,1 CardE,2 ... CardE,13

CardS,1 CardS,2 ... CardS,13

CardW,1 CardW,2 ... CardW,13

Trump indicates the trump suit. CardN,i, CardE,i, CardS,i, and CardW,i denote the card played in the i-th trick by the north, east, south, and west players respectively. Each card is represented
by two characters; the first and second character indicates the rank and the suit respectively.

The rank is represented by one of the following characters: ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘T’ (10), ‘J’ (jack), ‘Q’ (queen), ‘K’ (king), and ‘A’ (ace). The suit is represented by one of the following characters: ‘S’ (spades), ‘H’ (hearts), ‘D’ (diamonds), and ‘C’ (clubs).

You should assume the cards have been dealt out by the west player. Thus the first trick is led
by the north player. Also, the input does not contain any illegal plays.

The input is terminated by a line with “#”. This is not part of any dataset and thus should not
be processed.

Output

For each dataset, print the winner and their score of the deal in a line, with a single space
between them as a separator. The winner should be either “NS” (the north-south team) or “EW”
(the east-west team). No extra character or whitespace should appear in the output.

Sample Input

H
4C 8H QS 5D JD KS 8S AH 6H 7H 3S 7S 6D
TC JC JS KD AC QC QD 2H QH 3H 3C 7C 4D
6C 9C AS TD 5H 6S 5S KH TH AD 9S 8D 2D
8C 5C 2S 7D KC 4S TS JH 4H 9H 2C 9D 3D
D
8D 9D 9S QS 4H 5H JD JS 9H 6S TH 6H QH
QD 9C 5S 7S 7H AC 2D KD 6C 3D 8C TC 7C
5D QC 3S 4S 3H 3C 6D KS JC AS 5C 8H TS
4D 4C 8S 2S 2H KC TD JH 2C AH 7D AD KH
#

Output for the Sample Input

EW 1
EW 2

The winners of the tricks in the first dataset are as follows (in the order of tricks): east, north,
south, east, south, north, west, north, east, west, east, east, north.



Sample input:
H
4C 8H QS 5D JD KS 8S AH 6H 7H 3S 7S 6D
TC JC JS KD AC QC QD 2H QH 3H 3C 7C 4D
6C 9C AS TD 5H 6S 5S KH TH AD 9S 8D 2D
8C 5C 2S 7D KC 4S TS JH 4H 9H 2C 9D 3D
D
8D 9D 9S QS 4H 5H JD JS 9H 6S TH 6H QH
QD 9C 5S 7S 7H AC 2D KD 6C 3D 8C TC 7C
5D QC 3S 4S 3H 3C 6D KS JC AS 5C 8H TS
4D 4C 8S 2S 2H KC TD JH 2C AH 7D AD KH
#


Sample output:
EW 1
EW 2

Submission status: Wrong Answer

Solution code:
ref = "23456789TJQKA"
def judge(hand,leader):
    led = hand[leader][1]
    mx = tx = winner = -1
    for i in xrange(4):
        num,suit = ref.index(hand[i][0]),hand[i][1]
        if suit == trump:
            if num > tx:
                tx = num
                winner = i
        elif tx == -1:
            if suit == led and num > mx:
                mx = num
                winner = i
    return 0 if winner in [0,2] else 1
    
while 1:
    trump = raw_input()
    if trump == "#": break
    hands = [raw_input().split() for i in xrange(4)]
    hands = zip(hands[0],hands[1],hands[2],hands[3])
    leader = ns = ew = 0
    for hand in hands:
        leader = judge(hand, leader)
        if leader in [0,2]: ns += 1
        else: ew += 1
    if ns > ew: print "NS",ns-6
    else: print "EW",ew-6

Submission status: Accepted

Solution code:
ref = "23456789TJQKA"
def judge(hand,leader):
    led = hand[leader][1]
    mx = tx = winner = -1
    for i in xrange(4):
        num,suit = ref.index(hand[i][0]),hand[i][1]
        if suit == trump:
            if num > tx:
                tx = num
                winner = i
        elif tx == -1:
            if suit == led and num > mx:
                mx = num
                winner = i
    return winner
    
while 1:
    trump = raw_input()
    if trump == "#": break
    hands = [raw_input().split() for i in xrange(4)]
    hands = zip(hands[0],hands[1],hands[2],hands[3])
    leader = ns = ew = 0
    for hand in hands:
        leader = judge(hand, leader)
        if leader in [0,2]: ns += 1
        else: ew += 1
    if ns > ew: print "NS",ns-6
    else: print "EW",ew-6

Submission status: Accepted

Solution code:
ref = "23456789TJQKA"
def judge(hand,led):
    l = hand[led][1]
    mx = tx = w = -1
    for i in xrange(4):
        num,s = ref.index(hand[i][0]),hand[i][1]
        if s == t and num > tx: tx,w = num,i
        elif tx == -1 and s == l and num > mx: mx,w = num,i
    return w
    
while 1:
    t = raw_input()
    if t == "#": break
    hands = [raw_input().split() for i in xrange(4)]
    hands = zip(hands[0],hands[1],hands[2],hands[3])
    led = 0
    win = [0]*2
    for hand in hands:
        led = judge(hand, led)
        win[led%2] += 1
    if win[0] > win[1]: print "NS",win[0]-6
    else: print "EW",win[1]-6

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []
    mk = {}
    mk['T'] = 10
    mk['J'] = 11
    mk['Q'] = 12
    mk['K'] = 13
    mk['A'] = 14
    for i in range(1,10):
        mk[str(i)] = i

    while True:
        t = S()
        if t == '#':
            break
        a = [list(map(lambda x: (mk[x[0]], x[1]), LS())) for _ in range(4)]
        d = 0
        ns = 0
        ew = 0
        for i in range(13):
            m = -1
            mi = -1
            it = a[d][i][1]
            for j in range(4):
                k = a[j][i][0]
                if a[j][i][1] == t:
                    k += 100
                if a[j][i][1] == it:
                    k += 50
                if m < k:
                    m = k
                    mi = j
            d = mi
            if mi % 2 == 0:
                ns += 1
            else:
                ew += 1
        if ns > 6:
            rr.append('NS {}'.format(ns - 6))
        else:
            rr.append('EW {}'.format(ew - 6))

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = input().rstrip()
    if n == "#":
        return False
    lis = [LS() for i in range(4)]
    num = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]
    ans = [0] * 4
    master = 0
    for i in range(13):
        north, east, south, west = lis[0][i], lis[1][i], lis[2][i], lis[3][i]
        suit = [north[1], east[1], south[1], west[1]]
        tmp = [num.index(lis[master][i][0]), master]
        if n in suit:
            tmp = [-1, -1]
            for k in range(4):
                if suit[k] == n:
                    if tmp[0] < num.index(lis[k][i][0]):
                        tmp = [num.index(lis[k][i][0]), k]
        else:
            for k in range(4):
                if suit[tmp[1]] == suit[k] and tmp[0] < num.index(lis[k][i][0]):
                    tmp = [num.index(lis[k][i][0]), k]
        ans[tmp[1]] += 1
        master = tmp[1]
    a = ans[0] + ans[2] - 6
    b = ans[1] + ans[3] - 6
    if a > b:
        print("NS {}".format(a))
    else:
        print("EW {}".format(b))
    return True


#main
if __name__ == '__main__':
    while solve():
        pass