UVa 729 - The Hamming Distance Problem

题目:构造n位01串,其中有m个1的所有组合。

分析:搜索、枚举。可以利用库函数,求解,也可以利用dfs求解;我这里采用位运算计算组合数。

说明:注意库啊!

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int S[20];

int main()
{
	int T,N,M;
	while ( cin >> T )
	for ( int t = 1 ; t <= T ; ++ t ) {
		if ( t > 1 ) printf("\n");

		cin >> N >> M;
		int xx,yy,comb = (1<<M)-1,j,count;
        while ( comb < (1<<N) ) {
            /* 计算当前状态对应的集合 */
            j = 0; count = 0;
            do {
                S[count ++] = ((1<<j)&comb)>0;
                j ++;
            }while ( j < N );
			while ( count -- )
				printf("%d",S[count]);
            printf("\n");
            /* 位运算计算下一集合,按照顺序递增 */
            xx = comb&-comb,yy = comb+xx;
            comb = ((comb&~yy)/xx>>1)|yy;
        }
	}

	return 0;
}

UVa 729 - The Hamming Distance Problem,布布扣,bubuko.com

时间: 2024-10-13 12:38:33

UVa 729 - The Hamming Distance Problem的相关文章

UVA - 729 - The Hamming Distance Problem (枚举排列)

思路:数组中有H个1, N-H个0,按照字典序全排列,注意这里数组可以开int型的也可以开char型的,char型的在这里感觉用起来更方便,至少不要for循环,用char型的数组记得要初始化(memset),或者s[N] = '\0',因为这里有多组数据. AC代码①: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace s

UVa 729 The Hamming Distance Problem【枚举排列】

题意:给出数组的长度n,给出h,表示这个数组里面含有h个1,求其所有的排列 用next_permutation就可以了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set>

729 - The Hamming Distance Problem

// 题意: // 输入两个整数N, H,按照字典序输出所有长度为N,恰好包含H个1的01串 // 规模:1<=H<=N<=16 // 算法A:2^N枚举,输出1的个数为H的.采用递归枚举 // 从bits[d]开始确定,已经用了c0个0和c1个1 #include<cstdio> #include<cstring> #include<iostream> #include<string> #include<algorithm>

The Hamming Distance Problem UVA 729 (01串的全排列)

说说: 这题的意思就是给你一个01串的总长度和其中1的个数,要你求出该串的所有排列,并按照字典升序输出.其实这道题和前一道Generating Fast是非常类似的,甚至更为简单.要做的就是一个DFS给每个位分配位置,若0没有用完,则先分配0.1没有用完,则接着分配1.最后将结果输出即可. 源代码: #include <stdio.h> #define MAX 16+5 int N,H,onum,znum; char p[MAX]; void dfs(int,int,int); int mai

HDU 4712 Hamming Distance (随机函数)

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1806    Accepted Submission(s): 714 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal

hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1610    Accepted Submission(s): 630 Problem Description (From wikipedia) For bina

hdu 4712 Hamming Distance 随机

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal to the number of ones in a XOR b. For calculating Hammi

Codeforces Round #336 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和

B. Hamming Distance Sum Genos needs your help. He was asked to solve the following programming problem by Saitama: The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si i

HDU - 4712 Hamming Distance(坑爹的随机数算法 + 暴力求解)

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1728    Accepted Submission(s): 680 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equa