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>
 9 #include<queue>
10 #include<algorithm>
11 using namespace std;
12
13 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
14
15 typedef long long LL;
16 const int INF = (1<<30)-1;
17 const int mod=1000000007;
18 const int maxn=100005;
19
20 int p[maxn];
21
22 int main(){
23     int T;
24     scanf("%d",&T);
25     while(T--){
26
27         int n,h;
28         scanf("%d %d",&n,&h);
29
30         for(int i=0;i<n;i++){
31             if(i<h) p[i]=1;
32             else p[i]=0;
33         }
34
35         sort(p,p+n);
36
37         do{
38             for(int i=0;i<n;i++) printf("%d",p[i]);
39             printf("\n");
40         }while(next_permutation(p,p+n));
41
42         if(T)  printf("\n");
43     }
44     return 0;
45 }

时间: 2024-12-13 11:47:57

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 (枚举排列)

思路:数组中有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

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