AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC356-C Keys


問題へのリンク


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("3 2 2");
            WillReturn.Add("3 1 2 3 o");
            WillReturn.Add("2 2 3 x");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 5 3");
            WillReturn.Add("3 1 2 3 o");
            WillReturn.Add("3 2 3 4 o");
            WillReturn.Add("3 3 4 1 o");
            WillReturn.Add("3 4 1 2 o");
            WillReturn.Add("4 1 2 3 4 x");
            //0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("11 4 9");
            WillReturn.Add("10 1 2 3 4 5 6 7 8 9 10 o");
            WillReturn.Add("11 1 2 3 4 5 6 7 8 9 10 11 o");
            WillReturn.Add("10 11 10 9 8 7 6 5 4 3 2 x");
            WillReturn.Add("10 11 9 1 4 3 7 5 6 2 10 x");
            //8
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct ResultInfoDef
    {
        internal HashSet<int> KeySet;
        internal bool IsMaru;
    }
    static List<ResultInfoDef> mResultInfoList = new List<ResultInfoDef>();

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

        int HasKeyCnt = wkArr[0];
        int NeedCorrectKeyCnt = wkArr[2];

        foreach (string EachStr in InputList.Skip(1)) {
            ResultInfoDef WillAdd;

            var KeyList = new List<int>();

            string[] SplitArr = EachStr.Split(' ');
            for (int I = 1; I <= SplitArr.GetUpperBound(0) - 1; I++) {
                KeyList.Add(int.Parse(SplitArr[I]));
            }
            WillAdd.KeySet = new HashSet<int>(KeyList);

            WillAdd.IsMaru = false;
            if (SplitArr.Last() == "o") WillAdd.IsMaru = true;

            mResultInfoList.Add(WillAdd);
        }

        int Answer = 0;
        int[] BaseArr = { 0, 1 };
        foreach (int[] EachArr in RubyPatternClass<int>.RepeatedPermutation(BaseArr, HasKeyCnt)) {
            bool IsOK = true;
            foreach (ResultInfoDef EachResultInfo in mResultInfoList) {
                int MatchCnt = 0;
                for (int I = 0; I <= EachArr.GetUpperBound(0); I++) {
                    if (EachArr[I] == 1) {
                        if (EachResultInfo.KeySet.Contains(I + 1)) {
                            MatchCnt++;
                        }
                    }
                }

                if (MatchCnt >= NeedCorrectKeyCnt) {
                    if (EachResultInfo.IsMaru == false) {
                        IsOK = false;
                    }
                }
                if (MatchCnt < NeedCorrectKeyCnt) {
                    if (EachResultInfo.IsMaru) {
                        IsOK = false;
                    }
                }
            }
            if (IsOK) {
                Answer++;
            }
        }
        Console.WriteLine(Answer);
    }

    // セパレータとInt型の列挙を引数として、結合したstringを返す
    static string IntEnumJoin(string pSeparater, IEnumerable<int> pEnum)
    {
        string[] StrArr = Array.ConvertAll(pEnum.ToArray(), pX => pX.ToString());
        return string.Join(pSeparater, StrArr);
    }
}

#region RubyPatternClass
// Rubyの場合の数
internal static class RubyPatternClass<Type>
{
    // 重複順列を返す
    private struct JyoutaiDef_RepeatedPermutation
    {
        internal List<int> SelectedIndList;
    }
    internal static IEnumerable<Type[]> RepeatedPermutation(IEnumerable<Type> pEnum, int pR)
    {
        if (pR == 0) yield break;
        Type[] pArr = pEnum.ToArray();

        var Stk = new Stack<JyoutaiDef_RepeatedPermutation>();
        JyoutaiDef_RepeatedPermutation WillPush;
        for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
            WillPush.SelectedIndList = new List<int>() { I };
            Stk.Push(WillPush);
        }

        while (Stk.Count > 0) {
            JyoutaiDef_RepeatedPermutation Popped = Stk.Pop();

            // クリア判定
            if (Popped.SelectedIndList.Count == pR) {
                var WillReturn = new List<Type>();
                Popped.SelectedIndList.ForEach(X => WillReturn.Add(pArr[X]));
                yield return WillReturn.ToArray();
                continue;
            }

            for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
                Stk.Push(WillPush);
            }
        }
    }
}
#endregion


解説

RubyのRepeatedPermutationもどきで
正しい鍵の候補を列挙し、
矛盾がない候補を数えてます。