Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
呃,感觉坑多得数不过来,请自己对代码吧#include<cstdio> #include<cstring> #include<cstdlib> using namespace std; int n, m, dp[1 << 16], num[105], que[1 << 16], ans, dif; inline int nextBin(void) { int res = 0; while (getchar() != '\n'); for (int i = 0; i < n; i++) (res <<= 1) |= (getchar() - '0'); return res; } signed main(void) { scanf("%d%d", &n, &m); for (int i = 0; i <= m; i++)num[i] = nextBin(); memset(dp, -1, 1 << (n + 2)); memcpy(que, num + 1, m << 2); for (int i = 1; i <= m; i++)dp[num[i]] = 0; for (int hd = 0, tl = m; hd < tl; hd++) for (int i = 1, top = que[hd], tmp = dp[top]; i <= m; i++) if (dp[top^num[i]] < 0)dp[top^num[i]] = tmp + 1, que[tl++] = top^num[i]; for (int i = 1, tmp = dp[0] ? 2 : 1; i <= m; i++)dp[num[i]] = tmp; for (int i = 0; i < n; i++)dif += (num[0] >> i) & 1; for (int i = 0, j, diff; i < (1 << n); i++)if (dp[i] > 0) { for (j = diff = 0; j < n; j++)diff += (num[0] >> j) & 1 ^ (i >> j) & 1; ans = ((diff < dif) || (dif == diff&&dp[i] < dp[ans])) ? i : ans, dif = diff < dif ? diff : dif; } printf("%d\n", dp[ans]); for (int i = n - 1; i >= 0; i--)putchar('0' + ((ans >> i) & 1)); putchar('\n'); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator