AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

AOJ 0514 品質検査


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("2 2 2");
            WillReturn.Add("4");
            WillReturn.Add("2 4 5 0");
            WillReturn.Add("2 3 6 0");
            WillReturn.Add("1 4 5 0");
            WillReturn.Add("2 3 5 1");
            WillReturn.Add("0 0 0");
            //2
            //1
            //1
            //0
            //1
            //0
        }

        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        int CurrInd = 0;
        while (true) {
            SplitAct(InputList[CurrInd]);
            int ACnt = wkArr[0];
            int BCnt = wkArr[1];
            int CCnt = wkArr[2];
            if (ACnt == 0 && BCnt == 0 && CCnt == 0) break;

            int N = int.Parse(InputList[CurrInd + 1]);
            var ResultList = new List<string>();
            for (int I = CurrInd + 2; I <= CurrInd + 2 + N - 1; I++) {
                ResultList.Add(InputList[I]);
            }
            Solve(ACnt, BCnt, CCnt, ResultList);

            CurrInd += 1 + N + 1;
        }
    }

    struct ResultDef
    {
        internal int CheckA;
        internal int CheckB;
        internal int CheckC;
        internal int Result;
    }
    static List<ResultDef> mResultList = new List<ResultDef>();

    static void Solve(int pACnt, int pBCnt, int pCCnt, List<string> pResultList)
    {
        mResultList.Clear();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        var OKSet = new HashSet<int>();
        var NGSet = new HashSet<int>();

        foreach (string EachStr in pResultList) {
            SplitAct(EachStr);

            ResultDef WillAdd;
            WillAdd.CheckA = wkArr[0];
            WillAdd.CheckB = wkArr[1];
            WillAdd.CheckC = wkArr[2];
            WillAdd.Result = wkArr[3];
            mResultList.Add(WillAdd);
        }

        foreach (ResultDef EachResult in mResultList) {
            if (EachResult.Result == 1) {
                OKSet.Add(EachResult.CheckA);
                OKSet.Add(EachResult.CheckB);
                OKSet.Add(EachResult.CheckC);
            }
        }

        mResultList.RemoveAll(pX => pX.Result == 1);

        foreach (ResultDef EachResult in mResultList) {
            bool ContainsA = OKSet.Contains(EachResult.CheckA);
            bool ContainsB = OKSet.Contains(EachResult.CheckB);
            bool ContainsC = OKSet.Contains(EachResult.CheckC);

            if (ContainsA && ContainsB && ContainsC == false) {
                NGSet.Add(EachResult.CheckC);
            }
            if (ContainsA && ContainsB == false && ContainsC) {
                NGSet.Add(EachResult.CheckB);
            }
            if (ContainsA == false && ContainsB && ContainsC) {
                NGSet.Add(EachResult.CheckA);
            }
        }

        Action<int> wkAct = (pNum) =>
        {
            if (OKSet.Contains(pNum)) {
                Console.WriteLine(1);
            }
            else if (NGSet.Contains(pNum)) {
                Console.WriteLine(0);
            }
            else {
                Console.WriteLine(2);
            }
        };

        int StaNum = 1;
        for (int I = StaNum; I <= StaNum + pACnt - 1; I++) {
            wkAct(I);
        }
        StaNum += pACnt;

        for (int I = StaNum; I <= StaNum + pBCnt - 1; I++) {
            wkAct(I);
        }
        StaNum += pBCnt;

        for (int I = StaNum; I <= StaNum + pCCnt - 1; I++) {
            wkAct(I);
        }
    }
}


解説

最初に合格が確定するものをHashSetにAddします。
次に、結果が不合格の検査で、合格2つと検査した部品は、不合格が確定するので調べてます。