Допилил я таки свой вариант алгоритма с повторами:
import java.util.*;
enum State {
REPLACE, SHIFT, ADD, REMOVE, DECREASE;
}
public class StudySubsetSearch18 {
private static final Random rng = new Random();
private static final int size = 7;
private static final int[] entryValues = new int[size];
private static final int[] cumulativeSums = new int[size];
private static final int[] entryLimits = new int[size];
private static final int[] entryCounts = new int[size];
private static final int[] entryToTheLeft = new int[size];
private static final int[] entryToTheRight = new int[size];
private static int firstEntry, index, leftEntryIndex, rightEntryIndex, sum, tagetSum;
private static State state;
public static void main(String[] args) {
int n, value, totalSum, iterationCount;
boolean isActive;
value = 0;
for (n = 0; size > n; ++n) {
value += 1 + rng.nextInt(3);
entryValues[n] = value;
}
for (n = 0; size > n; ++n) {
entryLimits[n] = rng.nextInt(4);
}
System.out.println(Arrays.toString(entryValues));
System.out.println(Arrays.toString(entryLimits));
index = -1;
firstEntry = -1;
sum = 0;
for (n = 0; size > n; ++n) {
sum += entryLimits[n] * entryValues[n];
cumulativeSums[n] = sum;
entryToTheLeft[n] = index;
if (0 != entryLimits[n]) {
index = n;
if (-1 == firstEntry) {
firstEntry = n;
}
}
}
tagetSum = 1 + (sum >> 3) + rng.nextInt((3 * sum) >> 2);
System.out.println(tagetSum);
System.out.println();
leftEntryIndex = entryToTheLeft[index];
rightEntryIndex = size;
sum = 0;
iterationCount = 0;
state = State.ADD;
isActive = true;
while (isActive) {
switch(state) {
case REPLACE:
sum -= entryValues[index];
--entryCounts[index];
case SHIFT:
if (0 != entryCounts[index]) {
entryToTheRight[index] = rightEntryIndex;
rightEntryIndex = index;
}
index = leftEntryIndex;
leftEntryIndex = entryToTheLeft[index];
case ADD:
sum += entryValues[index];
++entryCounts[index];
display(-11, sum);
if (tagetSum <= sum) {
if (tagetSum == sum) {
System.out.print(" Match!");
} else {
System.out.print(" Overshort");
}
if (-1 == leftEntryIndex) {
state = State.REMOVE;
} else {
state = State.REPLACE;
}
} else {
if (entryLimits[index] == entryCounts[index]) {
if (-1 == leftEntryIndex) {
state = State.REMOVE;
} else {
entryToTheRight[index] = rightEntryIndex;
rightEntryIndex = index;
index = leftEntryIndex;
leftEntryIndex = entryToTheLeft[index];
state = State.ADD;
}
} else {
state = State.ADD;
}
}
System.out.println();
break;
case REMOVE:
sum -= entryCounts[index] * entryValues[index];
entryCounts[index] = 0;
case DECREASE:
if (size == rightEntryIndex) {
display(-1, sum);
System.out.println(" End");
isActive = false;
break;
}
leftEntryIndex = entryToTheLeft[rightEntryIndex];
index = rightEntryIndex;
rightEntryIndex = entryToTheRight[index];
sum -= entryValues[index];
--entryCounts[index];
totalSum = cumulativeSums[leftEntryIndex] + sum;
display(leftEntryIndex, totalSum);
if (tagetSum >= totalSum) {
if (tagetSum == totalSum) {
System.out.print(" Match!");
} else {
System.out.print(" Undershort");
}
if (0 == entryCounts[index]) {
state = State.DECREASE;
} else {
state = State.REMOVE;
}
} else {
state = State.SHIFT;
}
System.out.println();
}
++iterationCount;
if (1000 == iterationCount) {
break;
}
}
}
private static void display(int index, int sum) {
System.out.print("[");
for (int n = 0; size > n; ++n) {
if (0 != n) {
System.out.print(", ");
}
if (index >= n) {
System.out.print("x");
} else {
System.out.print(entryCounts[n]);
}
}
System.out.print("] -> ");
System.out.print(sum);
}
}
Результаты получаются такие:
(НОМЕР РАЗ)
[2, 5, 7, 8, 10, 13, 14]
[2, 1, 1, 3, 1, 3, 3]
64
[0, 0, 0, 0, 0, 0, 1] -> 14
[0, 0, 0, 0, 0, 0, 2] -> 28
[0, 0, 0, 0, 0, 0, 3] -> 42
[0, 0, 0, 0, 0, 1, 3] -> 55
[0, 0, 0, 0, 0, 2, 3] -> 68 Overshort
[0, 0, 0, 0, 1, 1, 3] -> 65 Overshort
[0, 0, 0, 1, 0, 1, 3] -> 63
[0, 0, 0, 2, 0, 1, 3] -> 71 Overshort
[0, 0, 1, 1, 0, 1, 3] -> 70 Overshort
[0, 1, 0, 1, 0, 1, 3] -> 68 Overshort
[1, 0, 0, 1, 0, 1, 3] -> 65 Overshort
[x, x, x, 0, 0, 1, 3] -> 71
[0, 0, 1, 0, 0, 1, 3] -> 62
[0, 1, 1, 0, 0, 1, 3] -> 67 Overshort
[1, 0, 1, 0, 0, 1, 3] -> 64 Match!
[x, x, 0, 0, 0, 1, 3] -> 64 Match!
[x, x, x, x, x, 0, 3] -> 92
[0, 0, 0, 0, 1, 0, 3] -> 52
[0, 0, 0, 1, 1, 0, 3] -> 60
[0, 0, 0, 2, 1, 0, 3] -> 68 Overshort
[0, 0, 1, 1, 1, 0, 3] -> 67 Overshort
[0, 1, 0, 1, 1, 0, 3] -> 65 Overshort
[1, 0, 0, 1, 1, 0, 3] -> 62
[2, 0, 0, 1, 1, 0, 3] -> 64 Match!
[x, x, x, 0, 1, 0, 3] -> 68
[0, 0, 1, 0, 1, 0, 3] -> 59
[0, 1, 1, 0, 1, 0, 3] -> 64 Match!
[1, 0, 1, 0, 1, 0, 3] -> 61
[2, 0, 1, 0, 1, 0, 3] -> 63
[x, x, 0, 0, 1, 0, 3] -> 61 Undershort
[x, x, x, x, 0, 0, 3] -> 82
[0, 0, 0, 1, 0, 0, 3] -> 50
[0, 0, 0, 2, 0, 0, 3] -> 58
[0, 0, 0, 3, 0, 0, 3] -> 66 Overshort
[0, 0, 1, 2, 0, 0, 3] -> 65 Overshort
[0, 1, 0, 2, 0, 0, 3] -> 63
[1, 1, 0, 2, 0, 0, 3] -> 65 Overshort
[x, 0, 0, 2, 0, 0, 3] -> 62 Undershort
[x, x, x, 1, 0, 0, 3] -> 66
[0, 0, 1, 1, 0, 0, 3] -> 57
[0, 1, 1, 1, 0, 0, 3] -> 62
[1, 1, 1, 1, 0, 0, 3] -> 64 Match!
[x, 0, 1, 1, 0, 0, 3] -> 61 Undershort
[x, x, 0, 1, 0, 0, 3] -> 59 Undershort
[x, x, x, 0, 0, 0, 3] -> 58 Undershort
[x, x, x, x, x, x, 2] -> 117
[0, 0, 0, 0, 0, 1, 2] -> 41
[0, 0, 0, 0, 0, 2, 2] -> 54
[0, 0, 0, 0, 0, 3, 2] -> 67 Overshort
[0, 0, 0, 0, 1, 2, 2] -> 64 Match!
[0, 0, 0, 1, 0, 2, 2] -> 62
[0, 0, 0, 2, 0, 2, 2] -> 70 Overshort
[0, 0, 1, 1, 0, 2, 2] -> 69 Overshort
[0, 1, 0, 1, 0, 2, 2] -> 67 Overshort
[1, 0, 0, 1, 0, 2, 2] -> 64 Match!
[x, x, x, 0, 0, 2, 2] -> 70
[0, 0, 1, 0, 0, 2, 2] -> 61
[0, 1, 1, 0, 0, 2, 2] -> 66 Overshort
[1, 0, 1, 0, 0, 2, 2] -> 63
[2, 0, 1, 0, 0, 2, 2] -> 65 Overshort
[x, x, 0, 0, 0, 2, 2] -> 63 Undershort
[x, x, x, x, x, 1, 2] -> 91
[0, 0, 0, 0, 1, 1, 2] -> 51
[0, 0, 0, 1, 1, 1, 2] -> 59
[0, 0, 0, 2, 1, 1, 2] -> 67 Overshort
[0, 0, 1, 1, 1, 1, 2] -> 66 Overshort
[0, 1, 0, 1, 1, 1, 2] -> 64 Match!
[1, 0, 0, 1, 1, 1, 2] -> 61
[2, 0, 0, 1, 1, 1, 2] -> 63
[x, x, x, 0, 1, 1, 2] -> 67
[0, 0, 1, 0, 1, 1, 2] -> 58
[0, 1, 1, 0, 1, 1, 2] -> 63
[1, 1, 1, 0, 1, 1, 2] -> 65 Overshort
[x, 0, 1, 0, 1, 1, 2] -> 62 Undershort
[x, x, 0, 0, 1, 1, 2] -> 60 Undershort
[x, x, x, x, 0, 1, 2] -> 81
[0, 0, 0, 1, 0, 1, 2] -> 49
[0, 0, 0, 2, 0, 1, 2] -> 57
[0, 0, 0, 3, 0, 1, 2] -> 65 Overshort
[0, 0, 1, 2, 0, 1, 2] -> 64 Match!
[0, 1, 0, 2, 0, 1, 2] -> 62
[1, 1, 0, 2, 0, 1, 2] -> 64 Match!
[x, 0, 0, 2, 0, 1, 2] -> 61 Undershort
[x, x, x, 1, 0, 1, 2] -> 65
[0, 0, 1, 1, 0, 1, 2] -> 56
[0, 1, 1, 1, 0, 1, 2] -> 61
[1, 1, 1, 1, 0, 1, 2] -> 63
[2, 1, 1, 1, 0, 1, 2] -> 65 Overshort
[x, 0, 1, 1, 0, 1, 2] -> 60 Undershort
[x, x, 0, 1, 0, 1, 2] -> 58 Undershort
[x, x, x, 0, 0, 1, 2] -> 57 Undershort
[x, x, x, x, x, 0, 2] -> 78
[0, 0, 0, 0, 1, 0, 2] -> 38
[0, 0, 0, 1, 1, 0, 2] -> 46
[0, 0, 0, 2, 1, 0, 2] -> 54
[0, 0, 0, 3, 1, 0, 2] -> 62
[0, 0, 1, 3, 1, 0, 2] -> 69 Overshort
[0, 1, 0, 3, 1, 0, 2] -> 67 Overshort
[1, 0, 0, 3, 1, 0, 2] -> 64 Match!
[x, x, x, 2, 1, 0, 2] -> 70
[0, 0, 1, 2, 1, 0, 2] -> 61
[0, 1, 1, 2, 1, 0, 2] -> 66 Overshort
[1, 0, 1, 2, 1, 0, 2] -> 63
[2, 0, 1, 2, 1, 0, 2] -> 65 Overshort
[x, x, 0, 2, 1, 0, 2] -> 63 Undershort
[x, x, x, 1, 1, 0, 2] -> 62 Undershort
[x, x, x, x, 0, 0, 2] -> 68
[0, 0, 0, 1, 0, 0, 2] -> 36
[0, 0, 0, 2, 0, 0, 2] -> 44
[0, 0, 0, 3, 0, 0, 2] -> 52
[0, 0, 1, 3, 0, 0, 2] -> 59
[0, 1, 1, 3, 0, 0, 2] -> 64 Match!
[1, 0, 1, 3, 0, 0, 2] -> 61
[2, 0, 1, 3, 0, 0, 2] -> 63
[x, x, 0, 3, 0, 0, 2] -> 61 Undershort
[x, x, x, 2, 0, 0, 2] -> 60 Undershort
[x, x, x, x, x, x, 1] -> 103
[0, 0, 0, 0, 0, 1, 1] -> 27
[0, 0, 0, 0, 0, 2, 1] -> 40
[0, 0, 0, 0, 0, 3, 1] -> 53
[0, 0, 0, 0, 1, 3, 1] -> 63
[0, 0, 0, 1, 1, 3, 1] -> 71 Overshort
[0, 0, 1, 0, 1, 3, 1] -> 70 Overshort
[0, 1, 0, 0, 1, 3, 1] -> 68 Overshort
[1, 0, 0, 0, 1, 3, 1] -> 65 Overshort
[x, x, x, x, 0, 3, 1] -> 93
[0, 0, 0, 1, 0, 3, 1] -> 61
[0, 0, 0, 2, 0, 3, 1] -> 69 Overshort
[0, 0, 1, 1, 0, 3, 1] -> 68 Overshort
[0, 1, 0, 1, 0, 3, 1] -> 66 Overshort
[1, 0, 0, 1, 0, 3, 1] -> 63
[2, 0, 0, 1, 0, 3, 1] -> 65 Overshort
[x, x, x, 0, 0, 3, 1] -> 69
[0, 0, 1, 0, 0, 3, 1] -> 60
[0, 1, 1, 0, 0, 3, 1] -> 65 Overshort
[1, 0, 1, 0, 0, 3, 1] -> 62
[2, 0, 1, 0, 0, 3, 1] -> 64 Match!
[x, x, 0, 0, 0, 3, 1] -> 62 Undershort
[x, x, x, x, x, 2, 1] -> 90
[0, 0, 0, 0, 1, 2, 1] -> 50
[0, 0, 0, 1, 1, 2, 1] -> 58
[0, 0, 0, 2, 1, 2, 1] -> 66 Overshort
[0, 0, 1, 1, 1, 2, 1] -> 65 Overshort
[0, 1, 0, 1, 1, 2, 1] -> 63
[1, 1, 0, 1, 1, 2, 1] -> 65 Overshort
[x, 0, 0, 1, 1, 2, 1] -> 62 Undershort
[x, x, x, 0, 1, 2, 1] -> 66
[0, 0, 1, 0, 1, 2, 1] -> 57
[0, 1, 1, 0, 1, 2, 1] -> 62
[1, 1, 1, 0, 1, 2, 1] -> 64 Match!
[x, 0, 1, 0, 1, 2, 1] -> 61 Undershort
[x, x, 0, 0, 1, 2, 1] -> 59 Undershort
[x, x, x, x, 0, 2, 1] -> 80
[0, 0, 0, 1, 0, 2, 1] -> 48
[0, 0, 0, 2, 0, 2, 1] -> 56
[0, 0, 0, 3, 0, 2, 1] -> 64 Match!
[0, 0, 1, 2, 0, 2, 1] -> 63
[0, 1, 1, 2, 0, 2, 1] -> 68 Overshort
[1, 0, 1, 2, 0, 2, 1] -> 65 Overshort
[x, x, 0, 2, 0, 2, 1] -> 65
[0, 1, 0, 2, 0, 2, 1] -> 61
[1, 1, 0, 2, 0, 2, 1] -> 63
[2, 1, 0, 2, 0, 2, 1] -> 65 Overshort
[x, 0, 0, 2, 0, 2, 1] -> 60 Undershort
[x, x, x, 1, 0, 2, 1] -> 64 Match!
[x, x, x, x, x, 1, 1] -> 77
[0, 0, 0, 0, 1, 1, 1] -> 37
[0, 0, 0, 1, 1, 1, 1] -> 45
[0, 0, 0, 2, 1, 1, 1] -> 53
[0, 0, 0, 3, 1, 1, 1] -> 61
[0, 0, 1, 3, 1, 1, 1] -> 68 Overshort
[0, 1, 0, 3, 1, 1, 1] -> 66 Overshort
[1, 0, 0, 3, 1, 1, 1] -> 63
[2, 0, 0, 3, 1, 1, 1] -> 65 Overshort
[x, x, x, 2, 1, 1, 1] -> 69
[0, 0, 1, 2, 1, 1, 1] -> 60
[0, 1, 1, 2, 1, 1, 1] -> 65 Overshort
[1, 0, 1, 2, 1, 1, 1] -> 62
[2, 0, 1, 2, 1, 1, 1] -> 64 Match!
[x, x, 0, 2, 1, 1, 1] -> 62 Undershort
[x, x, x, 1, 1, 1, 1] -> 61 Undershort
[x, x, x, x, 0, 1, 1] -> 67
[0, 0, 0, 1, 0, 1, 1] -> 35
[0, 0, 0, 2, 0, 1, 1] -> 43
[0, 0, 0, 3, 0, 1, 1] -> 51
[0, 0, 1, 3, 0, 1, 1] -> 58
[0, 1, 1, 3, 0, 1, 1] -> 63
[1, 1, 1, 3, 0, 1, 1] -> 65 Overshort
[x, 0, 1, 3, 0, 1, 1] -> 62 Undershort
[x, x, 0, 3, 0, 1, 1] -> 60 Undershort
[x, x, x, 2, 0, 1, 1] -> 59 Undershort
[x, x, x, x, x, 0, 1] -> 64 Match!
[x, x, x, x, x, x, 0] -> 89
[0, 0, 0, 0, 0, 1, 0] -> 13
[0, 0, 0, 0, 0, 2, 0] -> 26
[0, 0, 0, 0, 0, 3, 0] -> 39
[0, 0, 0, 0, 1, 3, 0] -> 49
[0, 0, 0, 1, 1, 3, 0] -> 57
[0, 0, 0, 2, 1, 3, 0] -> 65 Overshort
[0, 0, 1, 1, 1, 3, 0] -> 64 Match!
[0, 1, 0, 1, 1, 3, 0] -> 62
[1, 1, 0, 1, 1, 3, 0] -> 64 Match!
[x, 0, 0, 1, 1, 3, 0] -> 61 Undershort
[x, x, x, 0, 1, 3, 0] -> 65
[0, 0, 1, 0, 1, 3, 0] -> 56
[0, 1, 1, 0, 1, 3, 0] -> 61
[1, 1, 1, 0, 1, 3, 0] -> 63
[2, 1, 1, 0, 1, 3, 0] -> 65 Overshort
[x, 0, 1, 0, 1, 3, 0] -> 60 Undershort
[x, x, 0, 0, 1, 3, 0] -> 58 Undershort
[x, x, x, x, 0, 3, 0] -> 79
[0, 0, 0, 1, 0, 3, 0] -> 47
[0, 0, 0, 2, 0, 3, 0] -> 55
[0, 0, 0, 3, 0, 3, 0] -> 63
[0, 0, 1, 3, 0, 3, 0] -> 70 Overshort
[0, 1, 0, 3, 0, 3, 0] -> 68 Overshort
[1, 0, 0, 3, 0, 3, 0] -> 65 Overshort
[x, x, x, 2, 0, 3, 0] -> 71
[0, 0, 1, 2, 0, 3, 0] -> 62
[0, 1, 1, 2, 0, 3, 0] -> 67 Overshort
[1, 0, 1, 2, 0, 3, 0] -> 64 Match!
[x, x, 0, 2, 0, 3, 0] -> 64 Match!
[x, x, x, 1, 0, 3, 0] -> 63 Undershort
[x, x, x, x, x, 2, 0] -> 76
[0, 0, 0, 0, 1, 2, 0] -> 36
[0, 0, 0, 1, 1, 2, 0] -> 44
[0, 0, 0, 2, 1, 2, 0] -> 52
[0, 0, 0, 3, 1, 2, 0] -> 60
[0, 0, 1, 3, 1, 2, 0] -> 67 Overshort
[0, 1, 0, 3, 1, 2, 0] -> 65 Overshort
[1, 0, 0, 3, 1, 2, 0] -> 62
[2, 0, 0, 3, 1, 2, 0] -> 64 Match!
[x, x, x, 2, 1, 2, 0] -> 68
[0, 0, 1, 2, 1, 2, 0] -> 59
[0, 1, 1, 2, 1, 2, 0] -> 64 Match!
[1, 0, 1, 2, 1, 2, 0] -> 61
[2, 0, 1, 2, 1, 2, 0] -> 63
[x, x, 0, 2, 1, 2, 0] -> 61 Undershort
[x, x, x, 1, 1, 2, 0] -> 60 Undershort
[x, x, x, x, 0, 2, 0] -> 66
[0, 0, 0, 1, 0, 2, 0] -> 34
[0, 0, 0, 2, 0, 2, 0] -> 42
[0, 0, 0, 3, 0, 2, 0] -> 50
[0, 0, 1, 3, 0, 2, 0] -> 57
[0, 1, 1, 3, 0, 2, 0] -> 62
[1, 1, 1, 3, 0, 2, 0] -> 64 Match!
[x, 0, 1, 3, 0, 2, 0] -> 61 Undershort
[x, x, 0, 3, 0, 2, 0] -> 59 Undershort
[x, x, x, 2, 0, 2, 0] -> 58 Undershort
[x, x, x, x, x, 1, 0] -> 63 Undershort
[0, 0, 0, 0, 0, 0, 0] -> 0 End
(НОМЕР ДВА)
[3, 6, 7, 10, 12, 13, 14]
[0, 3, 2, 2, 1, 1, 0]
24
[0, 0, 0, 0, 0, 1, 0] -> 13
[0, 0, 0, 0, 1, 1, 0] -> 25 Overshort
[0, 0, 0, 1, 0, 1, 0] -> 23
[0, 0, 0, 2, 0, 1, 0] -> 33 Overshort
[0, 0, 1, 1, 0, 1, 0] -> 30 Overshort
[0, 1, 0, 1, 0, 1, 0] -> 29 Overshort
[x, x, x, 0, 0, 1, 0] -> 45
[0, 0, 1, 0, 0, 1, 0] -> 20
[0, 0, 2, 0, 0, 1, 0] -> 27 Overshort
[0, 1, 1, 0, 0, 1, 0] -> 26 Overshort
[x, x, 0, 0, 0, 1, 0] -> 31
[0, 1, 0, 0, 0, 1, 0] -> 19
[0, 2, 0, 0, 0, 1, 0] -> 25 Overshort
[x, x, x, x, x, 0, 0] -> 64
[0, 0, 0, 0, 1, 0, 0] -> 12
[0, 0, 0, 1, 1, 0, 0] -> 22
[0, 0, 0, 2, 1, 0, 0] -> 32 Overshort
[0, 0, 1, 1, 1, 0, 0] -> 29 Overshort
[0, 1, 0, 1, 1, 0, 0] -> 28 Overshort
[x, x, x, 0, 1, 0, 0] -> 44
[0, 0, 1, 0, 1, 0, 0] -> 19
[0, 0, 2, 0, 1, 0, 0] -> 26 Overshort
[0, 1, 1, 0, 1, 0, 0] -> 25 Overshort
[x, x, 0, 0, 1, 0, 0] -> 30
[0, 1, 0, 0, 1, 0, 0] -> 18
[0, 2, 0, 0, 1, 0, 0] -> 24 Match!
[x, x, x, x, 0, 0, 0] -> 52
[0, 0, 0, 1, 0, 0, 0] -> 10
[0, 0, 0, 2, 0, 0, 0] -> 20
[0, 0, 1, 2, 0, 0, 0] -> 27 Overshort
[0, 1, 0, 2, 0, 0, 0] -> 26 Overshort
[x, x, x, 1, 0, 0, 0] -> 42
[0, 0, 1, 1, 0, 0, 0] -> 17
[0, 0, 2, 1, 0, 0, 0] -> 24 Match!
[0, 1, 1, 1, 0, 0, 0] -> 23
[0, 2, 1, 1, 0, 0, 0] -> 29 Overshort
[x, x, 0, 1, 0, 0, 0] -> 28
[0, 1, 0, 1, 0, 0, 0] -> 16
[0, 2, 0, 1, 0, 0, 0] -> 22
[0, 3, 0, 1, 0, 0, 0] -> 28 Overshort
[x, x, x, 0, 0, 0, 0] -> 32
[0, 0, 1, 0, 0, 0, 0] -> 7
[0, 0, 2, 0, 0, 0, 0] -> 14
[0, 1, 2, 0, 0, 0, 0] -> 20
[0, 2, 2, 0, 0, 0, 0] -> 26 Overshort
[x, x, 1, 0, 0, 0, 0] -> 25
[0, 1, 1, 0, 0, 0, 0] -> 13
[0, 2, 1, 0, 0, 0, 0] -> 19
[0, 3, 1, 0, 0, 0, 0] -> 25 Overshort
[x, x, 0, 0, 0, 0, 0] -> 18 Undershort
[0, 0, 0, 0, 0, 0, 0] -> 0 End
(НОМЕР ТРИ)
[1, 4, 6, 8, 11, 13, 16]
[2, 0, 2, 0, 2, 0, 2]
41
[0, 0, 0, 0, 0, 0, 1] -> 16
[0, 0, 0, 0, 0, 0, 2] -> 32
[0, 0, 0, 0, 1, 0, 2] -> 43 Overshort
[0, 0, 1, 0, 0, 0, 2] -> 38
[0, 0, 2, 0, 0, 0, 2] -> 44 Overshort
[1, 0, 1, 0, 0, 0, 2] -> 39
[2, 0, 1, 0, 0, 0, 2] -> 40
[x, 0, 0, 0, 0, 0, 2] -> 34 Undershort
[x, x, x, x, x, 0, 1] -> 52
[0, 0, 0, 0, 1, 0, 1] -> 27
[0, 0, 0, 0, 2, 0, 1] -> 38
[0, 0, 1, 0, 2, 0, 1] -> 44 Overshort
[1, 0, 0, 0, 2, 0, 1] -> 39
[2, 0, 0, 0, 2, 0, 1] -> 40
[x, x, x, 0, 1, 0, 1] -> 41 Match!
[x, x, x, x, x, 0, 0] -> 36 Undershort
[0, 0, 0, 0, 0, 0, 0] -> 0 End
Теперь у желающих есть выбор: читать описание на одном непонятном языке или на другом непонятном языке.
Ну почему же непонятном? Я ваш алгоритм теперь понял и даже, думаю, смогу реализовать. В определённом смысле он мне даже кажется более рациональным, чем все те, что я до сих пор сделал.