Đây là giải pháp lưu trữ trạng thái trong một mảng các bool. Nó hoạt động bằng cách tạo các trạng thái sau trên mỗi cuộc gọi Next()
(n = 5, k = 3).
1 1 1 . . Move last 1 right once.
1 1 . 1 . Move last 1 right once.
1 1 . . 1 Move last 1 right once.
1 . 1 1 . Move the second last 1 right once and all 1s from the right back.
1 . 1 . 1 Move last 1 right once.
1 . . 1 1 Move the second last 1 right once (and all 1s from the right back.)
. 1 1 1 . Move the third last 1 right once and all 1s from the right back.
. 1 1 . 1 Move last 1 right once.
. 1 . 1 1 Move the second last 1 right once (and all 1s from the right back.)
. . 1 1 1 Move the third last 1 right once (and all 1s from the right back.)
Trạng thái này có thể được sử dụng để chọn các mục ghi nhớ từ chuỗi được cung cấp cho mọi trạng thái.
Lúc đầu, khởi tạo.
public static Boolean[] Initialize(Int32 n, Int32 k)
{
return Enumerable.Concat(Enumerable.Repeat(true, k),
Enumerable.Repeat(false, n - k)).ToArray();
}
Mã để chuyển sang kết hợp tiếp theo (sau).
public static Boolean Next(this Boolean[] list)
{
Int32 lastOneIndex = Array.LastIndexOf(list, true);
if (lastOneIndex == -1)
{
return false; // All zeros. 0000000
}
else if (lastOneIndex < list.Length - 1)
{
// Move the last one right once. 1100X00 => 11000X0
list.MoveBlock(lastOneIndex, lastOneIndex, lastOneIndex + 1);
}
else
{
Int32 lastZeroIndex = Array.LastIndexOf(list, false, lastOneIndex);
if (lastZeroIndex == -1)
{
return false; // All ones. 1111111
}
else
{
Int32 blockEndIndex = Array.LastIndexOf(list, true, lastZeroIndex);
if (blockEndIndex == -1)
{
// Move all ones back to the very left. 0000XXX => XXX0000
list.MoveBlock(lastZeroIndex + 1, lastOneIndex, 0);
return false; // Back at initial position.
}
else
{
// Move the block end right once. 11X0011 => 110X011
list.MoveBlock(blockEndIndex, blockEndIndex, blockEndIndex + 1);
// Move the block of ones from the very right back left. 11010XX => 1101XX0
list.MoveBlock(lastZeroIndex + 1, lastOneIndex, blockEndIndex + 2);
}
}
}
return true;
}
Cuối cùng là một số phương pháp trợ giúp.
public static void MoveBlock(this Boolean[] list, Int32 oldStart, Int32 oldEnd, Int32 newStart)
{
list.ClearBlock(oldStart, oldEnd);
list.SetBlock(newStart, newStart + oldEnd - oldStart);
}
public static void SetBlock(this Boolean[] list, Int32 start, Int32 end)
{
list.SetBlockToValue(start, end, true);
}
public static void ClearBlock(this Boolean[] list, Int32 start, Int32 end)
{
list.SetBlockToValue(start, end, false);
}
public static void SetBlockToValue(this Boolean[] list, Int32 start, Int32 end, Boolean value)
{
for (int i = start; i <= end; i++)
{
list[i] = value;
}
}
Và ví dụ sử dụng bằng chuỗi thay vì danh sách.
var sequence = "ABCDE";
var state = Initialize(sequence.Count(), 5);
do
{
Console.WriteLine(new String(sequence.Where((_, idx) => state[idx]).ToArray()));
}
while (state.Next());
Có IEnumerable là bất lợi cho bạn, vì bạn sẽ đi qua danh sách nhiều lần, vì vậy việc truy cập chỉ mục sẽ là một lợi ích lớn cho bạn. –
DevinB