Problem:


Problem C:  ラミー

  

    あなたの友達は最近 UT-Rummy というカードゲームを思いついた．
  

  

    このゲームで使うカードには赤・緑・青のいずれかの色と1から9までのいずれかの番号が
    つけられている．
    このゲームのプレイヤーはそれぞれ9枚の手札を持ち，
    自分のターンに手札から1枚選んで捨てて，
    代わりに山札から1枚引いてくるということを繰り返す．
    このように順番にターンを進めていき，
    最初に手持ちのカードに3枚ずつ3つの「セット」を作ったプレイヤーが勝ちとなる．
    セットとは，同じ色の3枚のカードからなる組で，すべて同じ数を持っているか
    連番をなしているもののことを言う．
    連番に関しては，番号の巡回は認められない．
    例えば，7, 8, 9は連番であるが 9, 1, 2は連番ではない．
  

  

    あなたの友達はこのゲームをコンピュータゲームとして売り出すという計画を立てて，
    その一環としてあなたに勝利条件の判定部分を作成して欲しいと頼んできた．
    あなたの仕事は，手札が勝利条件を満たしているかどうかを判定する
    プログラムを書くことである．
  

  
Input

  

    入力の1行目にはデータセット数を表す数 T
    (0 < T ≤ 50) が与えられる．
    この行に引き続き T 個のデータセットが与えられる．
  

  

    各データセットは2行からなり，
    1行目には i 番目
    (i = 1, 2, ..., 9)
    のカードの数字 ni
    がそれぞれスペースで区切られて与えられ，
    2行目には i 番目のカードの色を表す文字
    ci
    がそれぞれスペースで区切られて与えられる．
    カードの数字
    ni
    は 1 ≤ ni ≤ 9
    を満たす整数であり，
    カードの色 ci
    はそれぞれ &ldquo;R&rdquo;, &ldquo;G&rdquo;,
    &ldquo;B&rdquo;
    のいずれかの文字である．
  

  

    1つのデータセット内に色と数字がともに同じカードが5枚以上出現することはない．
  

  
Output

  

    各データセットにつき，勝利条件を満たしていれば
    &ldquo;1&rdquo;，
    そうでなければ &ldquo;0&rdquo; と出力せよ．
  

  
Sample Input

  

5
1 2 3 3 4 5 7 7 7
R R R R R R G G G
1 2 2 3 4 4 4 4 5
R R R R R R R R R
1 2 3 4 4 4 5 5 5
R R B R R R R R R
1 1 1 3 4 5 6 6 6
R R B G G G R R R
2 2 2 3 3 3 1 1 1
R G B R G B R G B

  
Output for the Sample Input

  

1
0
0
0
1



Sample input:
5
1 2 3 3 4 5 7 7 7
R R R R R R G G G
1 2 2 3 4 4 4 4 5
R R R R R R R R R
1 2 3 4 4 4 5 5 5
R R B R R R R R R
1 1 1 3 4 5 6 6 6
R R B G G G R R R
2 2 2 3 3 3 1 1 1
R G B R G B R G B


Sample output:
1
0
0
0
1

Submission status: Accepted

Solution code:
def rm_123(A):
	for a in set(A):
		if set([a,a+1,a+2]) <= set(A):
			A1 = A[:]
			for i in range(3): A1.remove(a+i)
			return judge(A1)

def rm_111(A):
	for a in set(A):
		if A.count(a) >= 3:
			A1 = A[:]
			for i in range(3): A1.remove(a)
			return judge(A1)

def judge(A):
	return len(A) == 0 or rm_123(A) or rm_111(A)

for _ in range(input()):
	n = map(int,raw_input().split())
	R = []; G = []; B = []
	s = raw_input().split()
	for i in range(len(n)):
		if   s[i] == "R": R.append(n[i])
		elif s[i] == "G": G.append(n[i])
		elif s[i] == "B": B.append(n[i])
	print 1 if judge(R) and judge(G) and judge(B) else 0

Submission status: Accepted

Solution code:
def Check(A) :
    Set = 0
    if len(A) < 3 :
        return False
    else :
        a = 0
        while True :
            if a >= len(A)-2 :
                break
            if A[a] == A[a+1] and A[a] == A[a+2] :
                Set += 1
                youso = A[a]
                A.remove(youso)
                A.remove(youso)
                A.remove(youso)
                a = 0
            elif A[a]+1 in A and A[a]+2 in A :
                Set += 1
                youso = A[a]
                A.remove(youso)
                A.remove(youso+1)
                A.remove(youso+2)
                a = 0
            else :
                a += 1
        return Set

n = int(input())
for i in range(n) :
    card = list([0, "X"] for i in range(9))
    R = []
    G = []
    B = []
    tmp = list(map(int,input().split()))
    for j in range(9) :
        card[j][0] = tmp[j]
    tmp = list(input().split())
    for j in range(9) :
        card[j][1] = tmp[j]
        if tmp[j] == "R" :
            R.append(card[j][0])
        elif tmp[j] == "G" :
            G.append(card[j][0])
        else :
            B.append(card[j][0])
    
    R.sort()
    G.sort()
    B.sort()
    if Check(R) + Check(G) + Check(B) == 3 :
        print(1) 
    else :
        print(0)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from itertools import permutations

def main():
    T = int(input())
    for _ in range(T):
        number = list(map(int,input().split()))
        color = input().split()
        ans = 0
        for ptr in permutations(range(9)):
            flag = True
            for i in range(3):
                c = color[ptr[3*i]]
                card = []
                for j in range(3):
                    if c != color[ptr[3*i + j]]:
                        flag = False
                        break
                    card.append(number[ptr[3*i + j]])
                if not flag:
                    break
                card.sort()
                if card[0] == card[2] or (card[2] - card[1] == 1 and card[1] - card[0] == 1):
                    continue
                else:
                    flag = False
                    break
            if flag:
                ans = 1
                break
        print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
R1 = [("R1","R2","R3"),("R2","R3","R4"),("R3","R4","R5"),("R4","R5","R6"),("R5","R6","R7"),("R6","R7","R8"),("R7","R8","R9")]
R2 = ["R1","R2","R3","R4","R5","R6","R7","R8","R9"]

G1 = [("G1","G2","G3"),("G2","G3","G4"),("G3","G4","G5"),("G4","G5","G6"),("G5","G6","G7"),("G6","G7","G8"),("G7","G8","G9")]
G2 = ["G1","G2","G3","G4","G5","G6","G7","G8","G9"]

B1 = [("B1","B2","B3"),("B2","B3","B4"),("B3","B4","B5"),("B4","B5","B6"),("B5","B6","B7"),("B6","B7","B8"),("B7","B8","B9")]
B2 = ["B1","B2","B3","B4","B5","B6","B7","B8","B9"]

def Rummy(C:list,N:list):

	#RGBの数が3の倍数で無い場合、負け
	if C.count("R") % 3 != 0 or C.count("G") % 3 != 0 or C.count("B") % 3 != 0:
		print("0")
		return

	ans = []
	for x,y in zip(C,N):
		tmp = x + str(y)
		ans.append(tmp)

	REN = R1+G1+B1
	DOU = R2+G2+B2

	#同番検索
	for z in DOU:
		if ans.count(z) >= 3:
			ans.remove(z)
			ans.remove(z)
			ans.remove(z)

	#連番検索
	for j in REN:
		while True:
			if all(x in ans for x in j):
				for k in j:
					ans.remove(k)
			else:
				break

	if len(ans) == 0:
		print("1")
	else:
		print("0")

if __name__ == '__main__':

	n = int(input())

	for _ in range(n):
		N = list(map(int,input().split()))
		C = input().split()

		Rummy(C,N)

Submission status: Runtime Error

Solution code:
import itertools as it

T = input()

def goodset(a, b, c):
    return (a == b and b == c) or (a + 1 == b and b + 1 == c)

def win(c):
    return goodset(c[0], c[1], c[2]) and goodset(c[3], c[4], c[5]) and goodset(c[6], c[7], c[8])

for i in range(T):
    cards = map(int, raw_input().split())
    color = raw_input().split()
    for j in range(9):
        if color[j] == "G":
            cards[j] += 100
        elif color[j] == "B":
            cards[j] += 200
    w = 0
    for p in it.permutations(range(9)):
        c = [cards[p[j]] for j in range(9)]
        if win(c):
            w = 1
            break
    print w

Submission status: Runtime Error

Solution code:
import os

with open("input1.txt") as f:
    trial = int(f.readline())
    nums,words = [],[]
    for s in range(trial):
        nums = [[int(n)] for n in f.readline().split(" ")]
        words = [n for n in f.readline().rstrip("\n").split(" ")]
        for t in range(9):
            nums[t].append(words[t])
        nums = sorted(nums,key=lambda x:(x[1],x[0]),reverse=False)
        n,color,cnt,samecnt,ans = nums[0][0],nums[0][1],1,1,0
        for t in range(1,9):
            #print(cnt,samecnt,1,nums[t][0],nums[t][1])
            if samecnt == 3:
                samecnt = 0
                ans += 1
            if cnt == 3:
                samecnt,cnt,n,color = 1,1,nums[t][0],nums[t][1]
                ans += 1
            else:
                if color != nums[t][1]:
                    n,color,cnt,samecnt = nums[t][0],nums[t][1],1,1
                elif n == nums[t][0]:
                    samecnt += 1
                elif n == nums[t][0] - 1:
                    n,samecnt = nums[t][0],1
                    cnt += 1
                else:
                    print(0)
                    break
        else:
            if ans == 2 and (samecnt == 3 or cnt == 3):
                print(1)
            else:
                print(0)

Submission status: Runtime Error

Solution code:
trial = int(f.readline())
nums,words = [],[]
for s in range(trial):
    nums = [[int(n)] for n in f.readline().split(" ")]
    words = [n for n in f.readline().rstrip("\n").split(" ")]
    for t in range(9):
        nums[t].append(words[t])
    nums = sorted(nums,key=lambda x:(x[1],x[0]),reverse=False)
    n,color,cnt,samecnt,ans = nums[0][0],nums[0][1],1,1,0
    for t in range(1,9):
        #print(cnt,samecnt,ans)
        if samecnt == 3:
            samecnt,cnt,n,color = 1,1,nums[t][0],nums[t][1]
            ans += 1
        elif cnt == 3:
            samecnt,cnt,n,color = 1,1,nums[t][0],nums[t][1]
            ans += 1
        else:
            if color != nums[t][1]:
                n,color,cnt,samecnt = nums[t][0],nums[t][1],1,1
            elif n == nums[t][0]:
                samecnt += 1
            elif n == nums[t][0] - 1:
                n,samecnt = nums[t][0],1
                cnt += 1
            else:
                print(0)
                break
    else:
        #print(cnt,ans)
        if ans == 2 and (samecnt == 3 or cnt == 3):
            print(1)
        else:
            print(0)

Submission status: Runtime Error

Solution code:
trial = int(f.input())
nums,words = [],[]
for s in range(trial):
    nums = [[int(n)] for n in f.input().split(" ")]
    words = [n for n in f.input().rstrip("\n").split(" ")]
    for t in range(9):
        nums[t].append(words[t])
    nums = sorted(nums,key=lambda x:(x[1],x[0]),reverse=False)
    n,color,cnt,samecnt,ans = nums[0][0],nums[0][1],1,1,0
    for t in range(1,9):
        #print(cnt,samecnt,ans)
        if samecnt == 3:
            samecnt,cnt,n,color = 1,1,nums[t][0],nums[t][1]
            ans += 1
        elif cnt == 3:
            samecnt,cnt,n,color = 1,1,nums[t][0],nums[t][1]
            ans += 1
        else:
            if color != nums[t][1]:
                n,color,cnt,samecnt = nums[t][0],nums[t][1],1,1
            elif n == nums[t][0]:
                samecnt += 1
            elif n == nums[t][0] - 1:
                n,samecnt = nums[t][0],1
                cnt += 1
            else:
                print(0)
                break
    else:
        #print(cnt,ans)
        if ans == 2 and (samecnt == 3 or cnt == 3):
            print(1)
        else:
            print(0)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3

import sys


def main():
    WINNING_HANDS = [
                '111', '222', '333', '444', '555', '666', '777', '888', '999',
                '123', '234', '345', '456', '567', '678', '789'
            ]

    num_of_datasets = int(sys.stdin.readline().strip('\n'))
    datasets = [{'R': [], 'G': [], 'B': []} for _ in range(num_of_datasets)]
    results = list()

    for dataset in range(num_of_datasets):
        n_set = [num for num in sys.stdin.readline().strip('\n').split()]
        c_set = [colour for colour in sys.stdin.readline().strip('\n').split()]
        for idx, colour in enumerate(c_set):
            if colour == 'R':
                datasets[dataset]['R'].append(n_set[idx])
            elif colour == 'G':
                datasets[dataset]['G'].append(n_set[idx])
            elif colour == 'B':
                datasets[dataset]['B'].append(n_set[idx])

        match_count = int()

        for rgb_key in datasets[dataset]:
            nums = sorted(datasets[dataset][rgb_key])

            for hand in WINNING_HANDS:
                if hand[0] and hand[1] and hand[2] in ''.join(nums):
                    match_count += 1
                    for digit in hand:
                        nums.pop(nums.index(digit))
                    if match_count == 3:
                        results.append(1)
                        continue

        if dataset < num_of_datasets-1 and match_count != 3:
            results.append(0)

    for result in results:
        print(result)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3

import sys


def main():
    WINNING_HANDS = [
                '111', '222', '333', '444', '555', '666', '777', '888', '999',
                '123', '234', '345', '456', '567', '678', '789'
            ]

    num_of_datasets = int(sys.stdin.readline().strip('\n'))
    datasets = [{'R': [], 'G': [], 'B': []} for _ in range(num_of_datasets)]
    results = list()

    for dataset in range(num_of_datasets):
        n_set = [num for num in sys.stdin.readline().strip('\n').split()]
        c_set = [colour for colour in sys.stdin.readline().strip('\n').split()]
        for idx, colour in enumerate(c_set):
            if colour == 'R':
                datasets[dataset]['R'].append(n_set[idx])
            elif colour == 'G':
                datasets[dataset]['G'].append(n_set[idx])
            elif colour == 'B':
                datasets[dataset]['B'].append(n_set[idx])

        match_count = int()

        for rgb_key in datasets[dataset]:
            nums = sorted(datasets[dataset][rgb_key])

            for hand in WINNING_HANDS:
                if hand[0] and hand[1] and hand[2] in ''.join(nums):
                    match_count += 1
                    nums.pop(nums.index(hand[0]))
                    nums.pop(nums.index(hand[1]))
                    nums.pop(nums.index(hand[2]))
                    if match_count == 3:
                        results.append(1)
                        continue

        if dataset < num_of_datasets-1 and match_count != 3:
            results.append(0)

    for result in results:
        print(result)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

a = []
M = int(input())
for i in range(M*2):
    if(i%2==0):
        a.append(map(int, input().split()))
    else:
        a.append(map(str, input().split()))

for i in range(M):
    #data = [[2,2,2,3,3,3,1,1,1],["R","G","B","R","G","B","R","G","B"]]
    data = a[i*2:(i+1)*2]
    data_df = pd.DataFrame(data)
    data_df = data_df.sort_values(1,axis=1)
    #print(i)
    #print(data_df,"\n")
    rgb_count = 0
    for j in range(3):
        data3 = data_df.iloc[:,3*j:3*(j+1)].sort_values(0,axis=1)
        r = (data3.iloc[:,0][0]+data3.iloc[:,2][0])/data3.iloc[:,1][0]
        if data3.iloc[:,0][1] == data3.iloc[:,1][1] == data3.iloc[:,2][1]:
            rgb_count+=1
        #print(data3.iloc[:,0][1])
        #print("1 = ",data3.iloc[:,1][1])
        #print("2 = ",data3.iloc[:,2][1])
        #print(data3)
        #print("r = ",r)
        
    
    if(r == 2 and rgb_count == 3):#連番または同じ値　かつ　同じ色
        print("1")
    elif(data_df.iloc[:,0][0] != 8 and data_df.iloc[:,1][0] != 9 and data_df.iloc[:,3][0] != 1):
        print("0")
    else:
        print("0")

Submission status: Runtime Error

Solution code:

a = []
M = int(input())
for i in range(M*2):
    if(i%2==0):
        a.append(map(int, input().split()))
    else:
        a.append(map(str, input().split()))

for i in range(M):
    data = a[i*2:(i+1)*2]
    data_df = pd.DataFrame(data)
    data_df = data_df.sort_values(1,axis=1)
    #print(i)
    #print(data_df,"\n")
    rgb_count = 0
    value_count = 0
    for j in range(3):
        data3 = data_df.iloc[:,3*j:3*(j+1)].sort_values(0,axis=1)
        r = (data3.iloc[:,0][0]+data3.iloc[:,2][0])/data3.iloc[:,1][0]
        if data3.iloc[:,0][1] == data3.iloc[:,1][1] == data3.iloc[:,2][1]:
            rgb_count+=1
        if r == 2:
            value_count+=1
        #print(data3.iloc[:,0][1])
        #print("1 = ",data3.iloc[:,1][1])
        #print("2 = ",data3.iloc[:,2][1])
        #print(data3)
        #print("r = ",r)
        
    
    if(value_count == 3 and rgb_count == 3):#連番または同じ値　かつ　同じ色
        print("1")
    elif(data_df.iloc[:,0][0] != 8 and data_df.iloc[:,1][0] != 9 and data_df.iloc[:,3][0] != 1):
        print("0")
    else:
        print("0")

Submission status: Runtime Error

Solution code:
a = []
M = int(input())
for i in range(M*2):
    if(i%2==0):
        a.append([int(i) for i in  input().split()])
    else:
        a.append([i for i in  input().split()])
for i in range(M):
    data = a[i*2:(i+1)*2]
    print(data)
    data_df = pd.DataFrame(data)
    data_df = data_df.sort_values((i*2)+1,axis=1)#色ソート
    #print(i)
    #print(data_df,"\n")
    rgb_count = 0
    value_count = 0
    for j in range(3):
        data3 = data_df.iloc[:,3*j:3*(j+1)].sort_values((i*2),axis=1)
        r = (int(data3.iloc[:,0][(i*2)])+int(data3.iloc[:,2][(i*2)]))/int(data3.iloc[:,1][(i*2)])
        if data3.iloc[:,0][(i*2)+1] == data3.iloc[:,1][(i*2)+1] == data3.iloc[:,2][(i*2)+1]:
            rgb_count+=1
        if r == 2:
            value_count+=1
        #print(data3.iloc[:,0][1])
        #print("1 = ",data3.iloc[:,1][1])
        #print("2 = ",data3.iloc[:,2][1])
        #print(data3)
        #print("r = ",r)
        
    
    if(value_count == 3 and rgb_count == 3):#連番または同じ値　かつ　同じ色
        print("1")
    elif(data_df.iloc[:,0][(i*2)] != 8 and data_df.iloc[:,1][(i*2)] != 9 and data_df.iloc[:,3][(i*2)] != 1):
        print("0")
    else:
        print("0")

Submission status: Runtime Error

Solution code:
a = []
M = int(input())
for i in range(M*2):
    if(i%2==0):
        a.append([int(i) for i in  input().split()])
    else:
        a.append([i for i in  input().split()])

for i in range(M):
    data = a[i*2:(i+1)*2]
    data_df = pd.DataFrame(data)
    data_df = data_df.sort_values(1,axis=1)#色ソート
    #print(i)
    #print(data_df,"\n")
    rgb_count = 0
    value_count = 0
    for j in range(3):
        data3 = data_df.iloc[:,3*j:3*(j+1)].sort_values(0,axis=1)
        r = (data3.iloc[:,0][0]+data3.iloc[:,2][0])/data3.iloc[:,1][0]
        if data3.iloc[:,0][1] == data3.iloc[:,1][1] == data3.iloc[:,2][1]:
            rgb_count+=1
        if r == 2:
            value_count+=1
        #print(data3.iloc[:,0][1])
        #print("1 = ",data3.iloc[:,1][1])
        #print("2 = ",data3.iloc[:,2][1])
        #print(data3)
        #print("r = ",r)
        
    
    if(value_count == 3 and rgb_count == 3):#連番または同じ値　かつ　同じ色
        print("1")
    elif(data_df.iloc[:,0][0] != 8 and data_df.iloc[:,1][0] != 9 and data_df.iloc[:,3][0] != 1):
        print("0")
    else:
        print("0")

Submission status: Runtime Error

Solution code:
5
1 2 3 3 4 5 7 7 7
R R R R R R G G G
1 2 2 3 4 4 4 4 5
R R R R R R R R R
1 2 3 4 4 4 5 5 5
R R B R R R R R R
1 1 1 3 4 5 6 6 6
R R B G G G R R R
2 2 2 3 3 3 1 1 1
R G B R G B R G B