競技プログラミング用のライブラリ    次のライブラリへ    前のライブラリへ

005-02 Rubyの場合の数を求めるメソッド


C#でRubyの場合の数を求めるメソッドを実装します。
●順列
●組合せ
●重複順列
●重複組合せ
を求めます。


C#のソース

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

class Program
{
    static void Main()
    {
        int[] BaseArr = { 11, 22, 33 };

        Console.WriteLine("■■■RubyPatternClass.Permutation■■■");
        foreach (int[] EachArr in RubyPatternClass<int>.Permutation(BaseArr, 2)) {
            Array.ForEach(EachArr, pX => Console.Write("{0},", pX));
            Console.WriteLine();
        }

        Console.WriteLine("■■■RubyPatternClass.Combination■■■");
        foreach (int[] EachArr in RubyPatternClass<int>.Combination(BaseArr, 2)) {
            Array.ForEach(EachArr, pX => Console.Write("{0},", pX));
            Console.WriteLine();
        }

        Console.WriteLine("■■■RubyPatternClass.RepeatedPermutation■■■");
        foreach (int[] EachArr in RubyPatternClass<int>.RepeatedPermutation(BaseArr, 2)) {
            Array.ForEach(EachArr, pX => Console.Write("{0},", pX));
            Console.WriteLine();
        }

        Console.WriteLine("■■■RubyPatternClass.RepeatedCombination■■■");
        foreach (int[] EachArr in RubyPatternClass<int>.RepeatedCombination(BaseArr, 2)) {
            Array.ForEach(EachArr, pX => Console.Write("{0},", pX));
            Console.WriteLine();
        }
    }
}

#region RubyPatternClass
// Rubyの場合の数
internal static class RubyPatternClass<Type>
{
    // 順列を返す
    private struct JyoutaiDef_Permutation
    {
        internal List<int> SelectedIndList;
    }
    internal static IEnumerable<Type[]> Permutation(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_Permutation>();
        JyoutaiDef_Permutation WillPush;
        for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
            WillPush.SelectedIndList = new List<int>() { I };
            Stk.Push(WillPush);
        }

        while (Stk.Count > 0) {
            JyoutaiDef_Permutation 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--) {
                if (Popped.SelectedIndList.Contains(I)) continue;
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
                Stk.Push(WillPush);
            }
        }
    }

    // 組合せを返す
    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);
            }
        }
    }

    // 重複順列を返す
    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);
            }
        }
    }

    // 重複組合せを返す
    private struct JyoutaiDef_RepeatedCombination
    {
        internal int CurrP;
        internal List<int> SelectedIndList;
    }
    internal static IEnumerable<Type[]> RepeatedCombination(IEnumerable<Type> pEnum, int pR)
    {
        if (pR == 0) yield break;
        Type[] pArr = pEnum.ToArray();

        var Stk = new Stack<JyoutaiDef_RepeatedCombination>();
        JyoutaiDef_RepeatedCombination 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_RepeatedCombination 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 <= I; I--) {
                WillPush.CurrP = I;
                WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
                Stk.Push(WillPush);
            }
        }
    }
}
#endregion


実行結果

// int[] BaseArr = { 11, 22, 33 };

■■■RubyPatternClass<int>.Permutation(BaseArr, 2)■■■
11,22,
11,33,
22,11,
22,33,
33,11,
33,22,

■■■RubyPatternClass<int>.Combination(BaseArr, 2)■■■
11,22,
11,33,
22,33,

■■■RubyPatternClass<int>.RepeatedPermutation(BaseArr, 2)■■■
11,11,
11,22,
11,33,
22,11,
22,22,
22,33,
33,11,
33,22,
33,33,

■■■RubyPatternClass<int>.RepeatedCombination(BaseArr, 2)■■■
11,11,
11,22,
11,33,
22,22,
22,33,
33,33,


解説

深さ優先探索を使ってます。