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
はそれぞれ “R”, “G”,
“B”
のいずれかの文字である．

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

Output

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

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

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            List<int> resultlist = new List<int>();

            int datasenum = int.Parse(Console.ReadLine());


            int result = 1;

            for (int i = 0; i < datasenum; i++)
            {
                var red = new Red();
                var bule = new Bule();
                var green = new Green();

                string[] cardnum = Console.ReadLine().Split(' ');

                string[] cardcolor = Console.ReadLine().Split(' ');

                //???????????????????????????????????????
                for (int j = 0; j < cardnum.Length; j++)
                {

                    switch (cardcolor[j])
                    {

                        case "R":
                            red.SetNum(int.Parse(cardnum[j]));
                            break;


                        case "G":
                            green.SetNum(int.Parse(cardnum[j]));
                            break;


                        case "B":
                            bule.SetNum(int.Parse(cardnum[j]));
                            break;

                    }

                }

                if (red.CheckWin() && bule.CheckWin() && green.CheckWin()) result = 1;
                else result = 0;

                //???????????????????????????Add??????
                resultlist.Add(result);

            }

            //????????????
            foreach(var output in resultlist)
            {
                Console.WriteLine(output);
            }


        }

    }



    class Red
    {
        #region ??????????????°(?????£???????????°??????????¨???°???????´??????????)
        int one = 0;
        int two = 0;
        int therr = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int seven = 0;
        int eight = 0;
        int nine = 0;
        #endregion

        enum CardNum { one = 1, two, therr, four, five, six, seven, eight, nine}


        //?????°?????°?????¨??¢??£????????????????????°???????????????????????????????????????????????????
        public void SetNum(int i)
        {

            switch (i)
            {
                case (int)CardNum.one:
                    this.one += 1;
                    break;

                case (int)CardNum.two:
                    this.two += 1;
                    break;

                case (int)CardNum.therr:
                    this.therr += 1;
                    break;

                case (int)CardNum.four:
                    this.four += 1;
                    break;

                case (int)CardNum.five:
                    this.five += 1;
                    break;

                case (int)CardNum.six:
                    this.six += 1;
                    break;

                case (int)CardNum.seven:
                    this.seven += 1;
                    break;

                case (int)CardNum.eight:
                    this.eight += 1;
                    break;

                case (int)CardNum.nine:
                    this.nine += 1;
                    break;

            }
        }

        //???????????¶????????????????????????????????????????????????
        public bool CheckWin()
        {
            bool result = true;

            //??????????????°?????????????´????????????????
            List<int> cardlist = new List<int>();


            cardlist.Add(this.one);
            cardlist.Add(this.two);
            cardlist.Add(this.therr);
            cardlist.Add(this.four);
            cardlist.Add(this.five);
            cardlist.Add(this.six);
            cardlist.Add(this.seven);
            cardlist.Add(this.eight);
            cardlist.Add(this.nine);


            //?????£??????????????????????????°???????´????????????°
            int totalnum = 0;

            //?????¢?????°???????´????????????°
            int peanum = 0;

            //????????£???????????¢????????°????????´??????????\???¨?????°
            int serialtemp = 0;


            //??£?????????????????????????????????
            int serialcounter = 0;

            //????????£??????????????????????????°(3??§????????????????????´??????false)
            //????????¢?????°(???????????§?????£?????¨??????????????¨????????????????????´??????false)
            //??¢??????????????¢??????????????????????????????
            for (int i = 0; i < 9; i++)
            {
                totalnum += cardlist[i];

                //????????????????????????????????´??????1?????¢
                if (cardlist[i] == 3)
                {
                    peanum += 1;
                    continue;
                }


                if (cardlist[i] == 4)
                {
                    peanum += 1;
                }

                //??£??????????????????
                if (0 != cardlist[i])
                {
                    serialcounter++;

                    //???????????¢????????°????????´????????????
                    if (serialtemp == 0) serialtemp = cardlist[i];
                    else if (serialtemp > cardlist[i]) serialtemp = 1;

                    if (serialcounter == 3)
                    {
                        //??£???????????¢???1or2????????§?????£?????????????????°???????????????2??????2?????¢???????????????
                        peanum += serialtemp;

                        //??£?????????????????°???????????°????????´???????¬??????£?????????????????????????????????
                        if(serialtemp < cardlist[i])
                        {
                            serialcounter = 1;
                            continue;
                        }

                        serialcounter = 0;
                        serialtemp = 0;
                    }
                }
                else
                {
                    serialcounter = 0;
                }
            
            }


            if (totalnum % 3 != 0) result = false;

            if (totalnum / 3 > peanum) result = false;

            //????????????1??????????????´??????true
            if (totalnum == 0) result = true;                 
        
            return result;
        }


        //??????????????°????????????????????¨??????????????????

    }


    class Green
    {
        #region ??????????????°(?????£???????????°??????????¨???°???????´??????????)
        int one = 0;
        int two = 0;
        int therr = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int seven = 0;
        int eight = 0;
        int nine = 0;
        #endregion

        enum CardNum { one = 1, two, therr, four, five, six, seven, eight, nine }


        public void SetNum(int i)
        {

            switch (i)
            {
                case (int)CardNum.one:
                    this.one += 1;
                    break;

                case (int)CardNum.two:
                    this.two += 1;
                    break;

                case (int)CardNum.therr:
                    this.therr += 1;
                    break;

                case (int)CardNum.four:
                    this.four += 1;
                    break;

                case (int)CardNum.five:
                    this.five += 1;
                    break;

                case (int)CardNum.six:
                    this.six += 1;
                    break;

                case (int)CardNum.seven:
                    this.seven += 1;
                    break;

                case (int)CardNum.eight:
                    this.eight += 1;
                    break;

                case (int)CardNum.nine:
                    this.nine += 1;
                    break;

            }
        }

        public bool CheckWin()
        {
            bool result = true;

            //??????????????°?????????????´????????????????
            List<int> cardlist = new List<int>();


            cardlist.Add(this.one);
            cardlist.Add(this.two);
            cardlist.Add(this.therr);
            cardlist.Add(this.four);
            cardlist.Add(this.five);
            cardlist.Add(this.six);
            cardlist.Add(this.seven);
            cardlist.Add(this.eight);
            cardlist.Add(this.nine);


            //?????£??????????????????????????°???????´????????????°
            int totalnum = 0;

            //?????¢?????°???????´????????????°
            int peanum = 0;

            //????????£???????????¢????????°????????´??????????\???¨?????°
            int serialtemp = 0;


            //??£?????????????????????????????????
            int serialcounter = 0;

            //????????£??????????????????????????°(3??§????????????????????´??????false)
            //????????¢?????°(???????????§?????£?????¨??????????????¨????????????????????´??????false)
            //??¢??????????????¢??????????????????????????????
            for (int i = 0; i < 9; i++)
            {
                totalnum += cardlist[i];

                //????????????????????????????????´??????1?????¢
                if (cardlist[i] == 3)
                {
                    peanum += 1;
                    continue;
                }


                if (cardlist[i] == 4)
                {
                    peanum += 1;
                }

                //??£??????????????????
                if (0 != cardlist[i])
                {
                    serialcounter++;

                    //???????????¢????????°????????´????????????
                    if (serialtemp == 0) serialtemp = cardlist[i];
                    else if (serialtemp > cardlist[i]) serialtemp = 1;

                    if (serialcounter == 3)
                    {
                        //??£???????????¢???1or2????????§?????£?????????????????°???????????????2??????2?????¢???????????????
                        peanum += serialtemp;

                        //??£?????????????????°???????????°????????´???????¬??????£?????????????????????????????????
                        if (serialtemp < cardlist[i])
                        {
                            serialcounter = 1;
                            continue;
                        }

                        serialcounter = 0;
                        serialtemp = 0;
                    }
                }
                else
                {
                    serialcounter = 0;
                }

            }


            if (totalnum % 3 != 0) result = false;

            if (totalnum / 3 > peanum) result = false;

            //????????????1??????????????´??????true
            if (totalnum == 0) result = true;

            return result;
        }

    }


    class Bule
    {
        #region ??????????????°(?????£???????????°??????????¨???°???????´??????????)
        int one = 0;
        int two = 0;
        int therr = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int seven = 0;
        int eight = 0;
        int nine = 0;
        #endregion

        enum CardNum { one = 1, two, therr, four, five, six, seven, eight, nine }


        public void SetNum(int i)
        {

            switch (i)
            {
                case (int)CardNum.one:
                    this.one += 1;
                    break;

                case (int)CardNum.two:
                    this.two += 1;
                    break;

                case (int)CardNum.therr:
                    this.therr += 1;
                    break;

                case (int)CardNum.four:
                    this.four += 1;
                    break;

                case (int)CardNum.five:
                    this.five += 1;
                    break;

                case (int)CardNum.six:
                    this.six += 1;
                    break;

                case (int)CardNum.seven:
                    this.seven += 1;
                    break;

                case (int)CardNum.eight:
                    this.eight += 1;
                    break;

                case (int)CardNum.nine:
                    this.nine += 1;
                    break;

            }
        }


        public bool CheckWin()
        {
            bool result = true;

            //??????????????°?????????????´????????????????
            List<int> cardlist = new List<int>();


            cardlist.Add(this.one);
            cardlist.Add(this.two);
            cardlist.Add(this.therr);
            cardlist.Add(this.four);
            cardlist.Add(this.five);
            cardlist.Add(this.six);
            cardlist.Add(this.seven);
            cardlist.Add(this.eight);
            cardlist.Add(this.nine);


            //?????£??????????????????????????°???????´????????????°
            int totalnum = 0;

            //?????¢?????°???????´????????????°
            int peanum = 0;

            //????????£???????????¢????????°????????´??????????\???¨?????°
            int serialtemp = 0;


            //??£?????????????????????????????????
            int serialcounter = 0;

            //????????£??????????????????????????°(3??§????????????????????´??????false)
            //????????¢?????°(???????????§?????£?????¨??????????????¨????????????????????´??????false)
            //??¢??????????????¢??????????????????????????????
            for (int i = 0; i < 9; i++)
            {
                totalnum += cardlist[i];

                //????????????????????????????????´??????1?????¢
                if (cardlist[i] == 3)
                {
                    peanum += 1;
                    continue;
                }


                if (cardlist[i] == 4)
                {
                    peanum += 1;
                }

                //??£??????????????????
                if (0 != cardlist[i])
                {
                    serialcounter++;

                    //???????????¢????????°????????´????????????
                    if (serialtemp == 0) serialtemp = cardlist[i];
                    else if (serialtemp > cardlist[i]) serialtemp = 1;

                    if (serialcounter == 3)
                    {
                        //??£???????????¢???1or2????????§?????£?????????????????°???????????????2??????2?????¢???????????????
                        peanum += serialtemp;

                        //??£?????????????????°???????????°????????´???????¬??????£?????????????????????????????????
                        if (serialtemp < cardlist[i])
                        {
                            serialcounter = 1;
                            continue;
                        }

                        serialcounter = 0;
                        serialtemp = 0;
                    }
                }
                else
                {
                    serialcounter = 0;
                }

            }


            if (totalnum % 3 != 0) result = false;

            if (totalnum / 3 > peanum) result = false;

            //????????????1??????????????´??????true
            if (totalnum == 0) result = true;

            return result;
        }


    }
}