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: Wrong Answer

Solution code:
def sort(a):
    temp = 0
    for i in range(a-1):
        if a[i] > a[i+1]:
            temp = a[i]
            a[i] = a[i+1]
            a[i+1] = temp
    return a


T = int(input())
count = 0
a = []
b = [[] for j in range(T)]
answer = [[False for i in range(3)] for j in range(T)]
for i in range(2*T):
    a.append(list(input().split()))
for i in range(T):
    for j in range(9):
        a[i*2][j] = int(a[i*2][j])
for i in range(T):
    for j in range(9):
        if a[2*i+1][j] == "R":
            a[2*i][j] = a[2*i][j] + 10
        if a[2*i+1][j] == "G":
            a[2*i][j] = a[2*i][j] + 20
        if a[2*i+1][j] == "B":
            a[2*i][j] = a[2*i][j] + 30
        b[i].append(a[2*i][j])
for j in range(T):
    for k in range(9):
        for i in range(8):
            temp = 0
            if b[j][i] > b[j][i+1]:
                temp = b[j][i]
                b[j][i] = b[j][i+1]
                b[j][i+1] = temp
for i in range(T):
    for j in range(3):
        if ((b[i][3*j] == b[i][3*j+1]) and (b[i][3*j] == b[i][3*j+2])) or ((b[i][3*j]+1 == b[i][3*j+1]) and (b[i][3*j]+2 == b[i][3*j+2])):
                answer[i][j] = True
for i in range(T):
    for j in range(2):
        if ((b[i][j] == b[i][j+2]) and (b[i][j] == b[i][j+4])) or ((b[i][j]+1 == b[i][j+2]) and (b[i][j]+2 == b[i][j+4])):
                answer[i][j] = True
for i in range(T):
    if answer[i][0] and answer[i][1] and answer[i][2]:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
T = int(input())
for i in range(T):
    num = list(map(int, input().split()))
    coler = input().split()
    groups = [[], [], []]
    for i, c in enumerate(coler):
        if c == "R":
            groups[0].append(num[i])
        elif c == "G":
            groups[1].append(num[i])
        else:
            groups[2].append(num[i])
    for group in groups:
        for _ in range(3):
            group.sort()
            for i in group:
                if group.count(i) >= 3:
                    for _ in range(3):
                        group.remove(i)
                if i + 1 in group and i + 2 in group:
                    for j in range(3):
                        group.remove(i + j)
    if len(groups[0]) == len(groups[1]) == len(groups[2]) == 0:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
for _ in range(int(input())):
    n, c = map(int, input().split()), input().split()
    c = list(zip(c, n))
    c.sort(key = lambda x:x[1])
    c.sort(key = lambda x:x[0])
    print(c)
    while c != []:
        if c[-1] == c[-2] == c[-3]:
            c = c[:-3]
        elif (c[-1][0], c[-1][1] -1) in c and (c[-1][0], c[-1][1] -2) in c :
            c.remove((c[-1][0], c[-1][1] -1))
            c.remove((c[-1][0], c[-1][1] -2))
            c.pop()
        else:print(0);break
    else:print(1)

Submission status: Wrong Answer

Solution code:
n = int(input())
def check(nl):
    for i in nl:
        if ((i+1 in nl) and (i+2 in nl)):
            for k in range(i,i+3):
                nl.remove(k)
        elif(nl.count(i)%3 == 0):
            for k in range(nl.count(i)):
                nl.remove(i)
                
for i in range(n):
    num_list = list(map(int, input().split()))
    c_list = list(map(str, input().split()))
    r = c_list.count('R')
    g = c_list.count('G')
    b = c_list.count('B')
    if (r%3 == 0 and g % 3 == 0 and b % 3 == 0):
        rlist = []
        glist = []
        blist = []
        for k in range(9):
            if(c_list[k] == 'R'):
                rlist.append(num_list[k])
            elif(c_list[k] == 'G'):
                glist.append(num_list[k])
            elif(c_list[k] == 'B'):
                blist.append(num_list[k])
        rlist.sort()
        blist.sort()
        glist.sort()
        check(rlist)
        check(glist)
        check(blist)
        if(rlist == [] and glist == [] and blist == []):
            print(1)
        else:
            print(0)
        
        
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
def check(nl):
    fl = list(nl)
    for w in range(len(nl)):
        if (fl[w] in nl):
            i = fl[w]
            if ((i+1 in nl) and (i+2 in nl)):
                for k in range(i,i+3):
                    nl.remove(k)
            elif(nl.count(i)%3 == 0):
                for k in range(nl.count(i)):
                    nl.remove(i)
        
                
for i in range(n):
    num_list = list(map(int, input().split()))
    c_list = list(map(str, input().split()))
    r = c_list.count('R')
    g = c_list.count('G')
    b = c_list.count('B')
    if (r%3 == 0 and g % 3 == 0 and b % 3 == 0):
        rlist = []
        glist = []
        blist = []
        for k in range(9):
            if(c_list[k] == 'R'):
                rlist.append(num_list[k])
            elif(c_list[k] == 'G'):
                glist.append(num_list[k])
            elif(c_list[k] == 'B'):
                blist.append(num_list[k])
        check(rlist)
        check(glist)
        check(blist)
        if(rlist == [] and glist == [] and blist == []):
            print(1)
        else:
            print(0)
        
        
    else:
        print(0)

Submission status: Time Limit Exceeded

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:
                while hand[0] in nums and hand[1] in nums and hand[2] in nums:
                    tmp = nums.copy()
                    if hand[0] in tmp:
                        if hand[1] in tmp:
                            if hand[2] in tmp:
                                match_count += 1
                                nums = tmp
                            else:
                                break
                        else:
                            break
                    else:
                        break

                    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: Accepted

Solution code:
def judge(nums):
  if nums == []:
    return True
  ret = False
  m = nums[0]
  if nums.count(m) == 3:
    ret = ret or judge(nums[3:])
  if m in nums and m + 1 in nums and m + 2 in nums:
    new_nums = nums[:]
    new_nums.remove(m)
    new_nums.remove(m + 1)
    new_nums.remove(m + 2)
    ret = ret or judge(new_nums)
  return ret

n = int(input())
for _ in range(n):
  nums = list(map(int, input().split()))
  colors = input().split()
  dic = {"R":[], "G":[], "B":[]}
  for num, color in zip(nums, colors):
    dic[color].append(num)
  ans = judge(sorted(dic["R"])) and judge(sorted(dic["G"])) and judge(sorted(dic["B"]))
  print(int(ans))

Submission status: Accepted

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 c in it.permutations(cards):#range(9)):
        #c = [cards[p[j]] for j in range(9)]
        if win(c):
            w = 1
            break
    print w

Submission status: Accepted

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:
                while hand[0] in nums and hand[1] in nums and hand[2] in nums:
                    tmp = nums.copy()
                    if hand[0] in tmp:
                        tmp.pop(tmp.index(hand[0]))
                        if hand[1] in tmp:
                            tmp.pop(tmp.index(hand[1]))
                            if hand[2] in tmp:
                                tmp.pop(tmp.index(hand[2]))
                                match_count += 1
                                nums = tmp
                            else:
                                break
                        else:
                            break
                    else:
                        break

                    if match_count == 3:
                        results.append(1)
                        continue

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

    for result in results:
        print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

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:
                while hand[0] in nums and hand[1] in nums and hand[2] in nums:
                    tmp = nums.copy()
                    if hand[0] in tmp:
                        tmp.pop(tmp.index(hand[0]))
                        if hand[1] in tmp:
                            tmp.pop(tmp.index(hand[1]))
                            if hand[2] in tmp:
                                tmp.pop(tmp.index(hand[2]))
                                match_count += 1
                                nums = tmp
                            else:
                                break
                        else:
                            break
                    else:
                        break
 
                    if match_count == 3:
                        results.append(1)
                        continue
 
        if dataset < num_of_datasets and match_count != 3:
            results.append(0)
 
    for result in results:
        print(result)
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

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:
                while hand[0] in nums and hand[1] in nums and hand[2] in nums:
                    tmp = nums.copy()
                    if hand[0] in tmp:
                        tmp.pop(tmp.index(hand[0]))
                        if hand[1] in tmp:
                            tmp.pop(tmp.index(hand[1]))
                            if hand[2] in tmp:
                                tmp.pop(tmp.index(hand[2]))
                                match_count += 1
                                nums = tmp
                            else:
                                break
                        else:
                            break
                    else:
                        break

                    if match_count == 3:
                        results.append(1)
                        break

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

    for result in results:
        print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

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]:
            if len(datasets[dataset][rgb_key]) > 1:
                nums = sorted(datasets[dataset][rgb_key])
            else:
                continue

            for hand in WINNING_HANDS:
                while hand[0] in nums and hand[1] in nums and hand[2] in nums:
                    tmp = nums.copy()
                    if hand[0] in tmp:
                        tmp.pop(tmp.index(hand[0]))
                        if hand[1] in tmp:
                            tmp.pop(tmp.index(hand[1]))
                            if hand[2] in tmp:
                                tmp.pop(tmp.index(hand[2]))
                                match_count += 1
                                nums = tmp
                            else:
                                break
                        else:
                            break
                    else:
                        break

                    if match_count == 3:
                        results.append(1)
                        break

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

    for result in results:
        print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

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]:
            if len(datasets[dataset][rgb_key]) > 1:
                nums = sorted(datasets[dataset][rgb_key])
            else:
                continue

            for hand in WINNING_HANDS:
                while hand[0] in nums and hand[1] in nums and hand[2] in nums:
                    tmp = nums.copy()
                    if hand[0] in tmp:
                        tmp.pop(tmp.index(hand[0]))
                        if hand[1] in tmp:
                            tmp.pop(tmp.index(hand[1]))
                            if hand[2] in tmp:
                                tmp.pop(tmp.index(hand[2]))
                                match_count += 1
                                nums = tmp
                            else:
                                break
                        else:
                            break
                    else:
                        break

                if match_count == 3:
                    results.append(1)
                    break

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

    for result in results:
        print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def check(card_set):
    for i in card_set:
        if len(i)%3!=0:
            return 0
        i.sort()
        for j in range(int(len(i)/3)):
            if i.count(i[0])>=3:
                a=i[0]
                i.remove(a)
                i.remove(a)
                i.remove(a)
            else:
                a=i[0]
                try:
                    i.remove(a)
                    i.remove(a+1)
                    i.remove(a+2)
                except ValueError:
                    return 0       
    return 1




n=int(input())

for count in range(n):
    number=[int(i) for i in input().split(" ")]
    color=input().split(" ")
    R=[]
    G=[]
    B=[]
    for i in range(9):
        if color[i]=="R":
            R.append(number[i])
        elif color[i]=="G":
            G.append(number[i])
        else:
            B.append(number[i])
    print(check([R,G,B]))

Submission status: Accepted

Solution code:
# your code goes here
#rummy

T=int(input())
for i in range(T):
    n=[int(i) for i in input().split()]
    c=[i for i in input().split()]
    R=[[],[],[]]
    for i in range(9):
        if c[i]=="R":
            R[0].append(n[i])
        elif c[i]=="G":        
            R[1].append(n[i])
        else:
            R[2].append(n[i])
    y=1
    i=0
 #   print(R)
    while y>0 and i<3:
        l=len(R[i])
        if l%3!=0:
            y=0
        else:
            R[i].sort()
            s=l//3
            j=0
            while y>0 and s>0 and j<len(R[i])-2:
                if R[i][j]==R[i][j+1]:
                    if R[i][j+1]==R[i][j+2]:
                        for k in range(3):
                            R[i].pop(j)
                        s-=1
                    elif R[i][j+1]+1==R[i][j+2]:
                        k=j+3
                        while k<len(R[i]) and R[i][k]==R[i][j+2]:
                            k+=1
                        if k>=len(R[i]) or R[i][k]!=R[i][j+2]+1:
                            y=0
                        else:
                            R[i].pop(k)
                            R[i].pop(j+2)
                            R[i].pop(j)
                        s-=1
                    else:
                        y=0
                elif R[i][j]+1==R[i][j+1]:
                    k=j+2
                    while k<len(R[i]) and R[i][k]==R[i][j+1]:
                        k+=1
                    if k>=len(R[i]) or R[i][k]!=R[i][j+1]+1:
                        y=0
                    else:
                        R[i].pop(k)
                        R[i].pop(j+1)
                        R[i].pop(j)
                        s-=1
                else:
                    y=0
        i+=1
    print(y)

Submission status: Accepted

Solution code:
T=int(input())
for i in range(T):
    n=[int(i) for i in input().split()]
    c=[i for i in input().split()]
    R=[[],[],[]]
    for i in range(9):
        if c[i]=="R":
            R[0].append(n[i])
        elif c[i]=="G":        
            R[1].append(n[i])
        else:
            R[2].append(n[i])
    y=1
    i=0
 #   print(R)
    while y>0 and i<3:
        l=len(R[i])
        if l%3!=0:
            y=0
        else:
            R[i].sort()
            s=l//3
            j=0
            while y>0 and s>0 and j<len(R[i])-2:
                if R[i][j]==R[i][j+1]:
                    if R[i][j+1]==R[i][j+2]:
                        for k in range(3):
                            R[i].pop(j)
                        s-=1
                    elif R[i][j+1]+1==R[i][j+2]:
                        k=j+3
                        while k<len(R[i]) and R[i][k]==R[i][j+2]:
                            k+=1
                        if k>=len(R[i]) or R[i][k]!=R[i][j+2]+1:
                            y=0
                        else:
                            R[i].pop(k)
                            R[i].pop(j+2)
                            R[i].pop(j)
                        s-=1
                    else:
                        y=0
                elif R[i][j]+1==R[i][j+1]:
                    k=j+2
                    while k<len(R[i]) and R[i][k]==R[i][j+1]:
                        k+=1
                    if k>=len(R[i]) or R[i][k]!=R[i][j+1]+1:
                        y=0
                    else:
                        R[i].pop(k)
                        R[i].pop(j+1)
                        R[i].pop(j)
                        s-=1
                else:
                    y=0
        i+=1
    print(y)

Submission status: Accepted

Solution code:
T = int(input())
for i in range(T):
    n = [int(i) for i in input().split()]
    c = [i for i in input().split()]
    value = [[],[],[]]
    for j in range(9):
        if c[j] == "R":
            value[0].append(n[j])
        elif c[j] == "G":
            value[1].append(n[j])
        else:
            value[2].append(n[j])
    ans = 1
    for i in range(3):
        l = len(value[i])
        if l%3 != 0:#RGBの長さが3の倍数でないと、正解になる可能性はない
            ans = 0
        else:
            value[i].sort()
            s = l//3
            j=0
            while ans>0 and s>0 and j<len(value[i])-2:
                if value[i][j] == value[i][j+1]:#隣り合う数字が一致している場合
                    if value[i][j+1] == value[i][j+2]:#3連続で同じ数字が並んでいる場合
                        for k in range(3):
                            value[i].pop(j)#値の削除
                        s-=1
                    elif value[i][j+1]+1==value[i][j+2]:#2つめの数字と3つ目の数字が連番　ex)112,223,334,
                        k=j+3
                        while k<len(value[i]) and value[i][k]==value[i][j+2]:#3つめと4つめの値が同じ
                            k+=1
                        if k>=len(value[i]) or value[i][k]!=value[i][j+2]+1:#4つ目と5つ目の数字が連番でない場合 ex)11225,22335　不正解
                            ans=0
                        else:
                            #値削除
                            value[i].pop(k)
                            value[i].pop(j+2)
                            value[i].pop(j)
                        s-=1
                    else:#3つ目の数字が全然違うやつ
                        ans=0

                elif value[i][j]+1==value[i][j+1]:#隣り合う数字が連番の場合
                    k=j+2
                    while k<len(value[i]) and value[i][k]==value[i][j+1]:
                        k+=1
                    if k>=len(value[i]) or value[i][k]!=value[i][j+1]+1:#3つめの数字が連番でない場合　不正解
                        ans=0
                    else:
                        #値削除
                        value[i].pop(k)
                        value[i].pop(j+1)
                        value[i].pop(j)
                        s-=1
                else:#隣り合う数字が連番でもなく、同値でもない場合　不正解
                    ans=0
        i+=1
    print(ans)

Submission status: Accepted

Solution code:
T = int(input())
for i in range(T):
    num = list(map(int, input().split()))
    coler = input().split()
    groups = [[], [], []]
    for i, c in enumerate(coler):
        if c == "R":
            groups[0].append(num[i])
        elif c == "G":
            groups[1].append(num[i])
        else:
            groups[2].append(num[i])
    for group in groups:
        while len(group) != 0 and len(group) % 3 == 0:
            tmp = group[:]
            group.sort()
            i = group[0]
            if group.count(i) >= 3:
                for _ in range(3):
                    group.remove(i)
            if i + 1 in group and i + 2 in group:
                for j in range(3):
                    group.remove(i + j)
            if tmp == group:
                break
    if len(groups[0]) == len(groups[1]) == len(groups[2]) == 0:
        print(1)
    else:
        print(0)

Submission status: Accepted

Solution code:
for _ in range(int(input())):
    n, c = map(int, input().split()), input().split()
    c = list(zip(c, n))
    c.sort(key = lambda x:x[1])
    c.sort(key = lambda x:x[0])
    while c != []:
        if c[-1] == c[-2] == c[-3]:
            c = c[:-3]
        elif (c[-1][0], c[-1][1] -1) in c and (c[-1][0], c[-1][1] -2) in c :
            c.remove((c[-1][0], c[-1][1] -1))
            c.remove((c[-1][0], c[-1][1] -2))
            c.pop()
        else:print(0);break
    else:print(1)

Submission status: Accepted

Solution code:
n = int(input())
def check(nl):
    fl = list(nl)
    for w in range(len(nl)):
        if (fl[w] in nl):
            i = fl[w]
            if ((i+1 in nl) and (i+2 in nl)):
                for k in range(i,i+3):
                    nl.remove(k)
            elif(nl.count(i)%3 == 0):
                for k in range(nl.count(i)):
                    nl.remove(i)
        
                
for i in range(n):
    num_list = list(map(int, input().split()))
    c_list = list(map(str, input().split()))
    r = c_list.count('R')
    g = c_list.count('G')
    b = c_list.count('B')
    if (r%3 == 0 and g % 3 == 0 and b % 3 == 0):
        rlist = []
        glist = []
        blist = []
        for k in range(9):
            if(c_list[k] == 'R'):
                rlist.append(num_list[k])
            elif(c_list[k] == 'G'):
                glist.append(num_list[k])
            elif(c_list[k] == 'B'):
                blist.append(num_list[k])
        rlist.sort()
        glist.sort()
        blist.sort()
        check(rlist)
        check(glist)
        check(blist)
        if(len(rlist) == 0 and len(glist) == 0 and len(blist) == 0):
            print(1)
        else:
            print(0)
        
        
    else:
        print(0)

Submission status: Accepted

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

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

def judge(A):
	return 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 rm_123(A):
	if len(A) == 0: return 1
	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):
	if len(A) == 0: return 1
	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 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