gpt4 book ai didi

algorithm - 理解 "Cow Id"编码竞赛解决方案

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:49:35 26 4
gpt4 key购买 nike

我理解解决方案的 DP 部分,但我不理解“追溯”部分,以及如何找到 i 设置位的 j 个长度数字的数量有助于解决问题?解决方案的粗体部分让我感到困惑。

问题:

Being a secret computer geek, Farmer John labels all of his cows with
binary numbers. However, he is a bit superstitious, and only labels cows
with binary numbers that have exactly K "1" bits (1 <= K <= 10). The
leading bit of each label is always a "1" bit, of course. FJ assigns
labels in increasing numeric order, starting from the smallest possible
valid label -- a K-bit number consisting of all "1" bits. Unfortunately,
he loses track of his labeling and needs your help: please determine the
Nth label he should assign (1 <= N <= 10^7).

INPUT FORMAT:

* Line 1: Two space-separated integers, N and K.

SAMPLE INPUT (file cowids.in):

7 3

INPUT DETAILS:

Among all binary numbers containing exactly 3 "1" bits, FJ wants to output
the 7th in increasing sorted order.

SAMPLE OUTPUT (file cowids.out):

10110

解决方案:

This problem can be solved by dynamic programming. We treat inputs with K=1 as a special case, since this involves just printing a one followed by N-1 zeros. For K at least 2, a quick back-of-the-envelope calculation shows us that the total number of digits in the answer will be at most 5000. For the two-dimensional array A[0..10][0..5000], we let A[i][j] denote the number of j-digit binary numbers (including those that start with leading zeros) with exactly i 1-bits. We can fill in this table by setting A[i][j] = A[i-1][j-1] + A[i][j-1], since a j-digit number with i 1-bits can be obtained either by appending a 0 bit to a (j-1)-digit number with i 1-bits, or by appending a 1 bit to a (j-1)-digit number with (i-1) 1-bits. Once we have filled in the table, the appropriate "traceback path" from A[K][5000] gives us the binary number we seek (taking care not to print leading zeros). "

解决方案代码:

#include <stdio.h>
#define M 5000

int A[11][M+1];
int leading_zeros = 1;

void print_sol(int n,int k,int m)
{
if (k==0 && m==1) return;
if (k==0 || A[k][m-1] >= n) {
if (!leading_zeros) printf ("0");
print_sol(n,k,m-1);
} else {
leading_zeros = 0;
printf ("1");
print_sol(n-A[k][m-1],k-1,m-1);
}
}

int main(void)
{
int i,j,N,K;

freopen ("cowids.in", "r", stdin);
freopen ("cowids.out", "w", stdout);

scanf ("%d %d", &N, &K);

if (K==1) {
printf ("1");
for (i=0; i<N-1; i++) printf ("0");
printf ("\n");
return 0;
}

A[0][1] = 1;
for (j=1; j<=M; j++) {
for (i=0; i<=10; i++) {
if (i==0) A[i][j] = 1;
else A[i][j] = A[i-1][j-1] + A[i][j-1];
if (A[i][j] > 10000000) A[i][j] = 10000000; /* avoid overflow */
}
}

print_sol(N,K,M);
printf ("\n");

return 0;
}

最佳答案

我已经简要地注释了解决方案代码。我不会放弃答案,而是将其作为大脑锻炼(良好做法)。如果有什么地方我没有解释清楚或者解释的太含糊,我会修改我的注释。

void print_sol(int n,int k,int m)
{
if (k==0 && m==1) // base case
return;
if (k==0 || A[k][m-1] >= n) { // If there are more than n numbers that satisfy A[k][m-1], we can afford to decrease the number of digits in the answer.
if (!leading_zeros) printf ("0"); // don't print 0's if they lead the number (i.e. 0001 is wrong)
print_sol(n,k,m-1); // decrement number of digits, m
} else {
leading_zeros = 0; // There is no longer a need to consider leading 0's
// because we are gonna print a "1". Notice there's no code "leading_zeros = 0"?
// That's because once we've printed the first "1", we don't have to worry about leading 0's.
printf ("1");
print_sol(n-A[k][m-1],k-1,m-1); // We just printed a 1-bit, so we only need to print k-1 more 1's.
// Decrement m since we just printed out a digit.
// We initially had to find the n-th number with k 1-bits, but we just took care of a few by printing that "1". Decrease n.
// In basic terms, this line is saying "print out the n-A[k][m-1]-largest number with m-1 digits and k-1 1-bits"
}
}

关于algorithm - 理解 "Cow Id"编码竞赛解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57195236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com