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 std;

int a[20];
int T;
int N, H;

int main() {
	scanf("%d", &T);
	while(T--) {
		cin >> N >> H;
		for(int i = 0 ; i < N - H; i++) a[i] = 0;
		for(int i = N - H; i < N; i++) a[i] = 1;
		do
		{
			for(int i = 0; i < N; i++) printf("%d", a[i]);
			printf("\n");
		}while(next_permutation(a, a + N));
		if(T) printf("\n");
	}
	return 0;
} 

AC代码②:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

char a[20];
int T;
int N, H;

int main() {
	scanf("%d", &T);
	while(T--) {
		cin >> N >> H;
		for(int i = 0 ; i < N - H; i++) a[i] = '0';
		for(int i = N - H; i < N; i++) a[i] = '1';
		a[N] = '\0';
		do
		{
			printf("%s\n", a);
		}while(next_permutation(a, a + N));
		if(T) printf("\n");
	}
	return 0;
} 

AC代码③(不调用库函数的生成可重集的排列):

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

char s[20], str[20];

void print_permutation(char *s, char *str, int len, int cur) {
	if(cur == len) printf("%s\n", str);
	else for(int i = 0; i < len; i++) {
		if(!i || s[i] != s[i - 1]) {
			int c1 = 0, c2 = 0;
			for(int j = 0; j < len; j++) if(s[i] == s[j]) c1 ++;
			for(int j = 0; j < cur; j++) if(s[i] == str[j]) c2 ++;
			if(c2 < c1) {
				str[cur] = s[i];
				print_permutation(s, str, len, cur + 1);
			}
		}
	}
}

int main() {
	int T;
	cin >> T;
	while(T) {
		int N, H;
		scanf("%d %d", &N, &H);
		memset(s, 0, sizeof(s));
		for(int i = 0; i < N - H; i++) s[i] = '0';
		for(int i = N - H; i < N; i++) s[i] = '1';
		memset(str, 0, sizeof(str));
		print_permutation(s, str, N, 0);
		if(T) printf("\n" );
	}
	return 0;
}
时间: 2024-12-13 11:47:50

UVA - 729 - The Hamming Distance Problem (枚举排列)的相关文章

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

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

UVa 216 Getting in Line【枚举排列】

题意:给出n个点的坐标,(2<=n<=8),现在要使得这n个点连通,问最小的距离的和 因为n很小,所以可以直接枚举这n个数的排列,算每一个排列的距离的和, 保留下距离和最小的那个排列就可以了(这个地方和带宽那题有点像,用memcpy将整个排列保留下来) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack

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

UVA - 11076 Add Again (重复元素的排列)

Summation of sequence of integersis always a common problem in Computer Science. Rather than computing blindly,some intelligent techniques make the task simpler. Here you have to find thesummation of a sequence of integers. The sequence is an interes

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