Creating a power set of a Sequence

后端 未结 4 1546
南旧
南旧 2020-12-01 06:49

I am trying to create a program that is a base for creating possible combinations of a sequence, string or a number. This is some sort of encryption / decryption program. I

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 07:33

    Same algorithm SergeyS mention using Linq (where inputSet is the input and outputPowerSet is the output):

    int setLength = inputSet.Count;
    int powerSetLength = 1 << setLength;
    for (int bitMask = 0; bitMask < powerSetLength; bitMask++)
    {
        var subSet = from x in inputSet 
                     where ((1 << inputSet.IndexOf(x)) & bitMask) != 0 
                     select x;
        outputPowerSet.Add(subSet.ToList());
    }
    

提交回复
热议问题