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

ABC263-C Monotonically Increasing


問題へのリンク


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

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

        var BaseList = new List<int>();
        for (int I = 1; I <= M; I++) {
            BaseList.Add(I);
        }

        var sb = new System.Text.StringBuilder();
        foreach (int[] EachArr in RubyPatternClass<int>.Combination(BaseList, N)) {
            Array.ForEach(EachArr, pX => sb.AppendFormat("{0} ", pX));
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }
}

#region RubyPatternClass
// Rubyの場合の数
internal static class RubyPatternClass<Type>
{
    // 組合せを返す
    private struct JyoutaiDef_Combination
    {
        internal int CurrP;
        internal List<int> SelectedIndList;
    }
    internal static IEnumerable<Type[]> Combination(IEnumerable<Type> pEnum, int pR)
    {
        if (pR == 0) yield break;
        Type[] pArr = pEnum.ToArray();
        if (pArr.Length < pR) yield break;

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

        while (Stk.Count > 0) {
            JyoutaiDef_Combination 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); Popped.CurrP + 1 <= I; I--) {
                WillPush.CurrP = I;
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
                Stk.Push(WillPush);
            }
        }
    }
}
#endregion


解説

ライブラリのRubyのCombinationメソッドもどきを使ってます。