Is correct second example ??
Posted by
coder 7 Aug 2023 15:51
12
12 5 3 11 2 8 4 9 1 6 10 7
answer: 34650 in example output.
But, following full check test prints 6300.
-----
static bool match(int a[], int n, int b[], int m){
if (n != m) return false;
if (n == 0 && m == 0) return true;
int i = std::max_element(a, a + n) - a;
int j = std::max_element(b, b + m) - b;
return i == j && match(a, i, b, j) && match(a + i + 1, n - i - 1, b + j + 1, m - j - 1);
}
int main()
{
int n = 12;
int a[] = { 12, 5, 3, 11, 2, 8, 4, 9, 1, 6, 10, 7 };
int b[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int ans = 0;
do{
if (match(a, n, b, n)) ++ ans;
} while (std::next_permutation(b, b + 12) );
printf("ans = %d\n", ans);
return 0;
}