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 main(){
  int T;
//  freopen("data","r",stdin);
  scanf("%d",&T);
  while(T--){
   scanf("%d%d",&N,&H);

   znum=N-H;//0的个数
   onum=H;//1的个数
   p[N]='\0';

   dfs(0,0,0);

   if(T) putchar('\n');

  }

  return 0;
}

void dfs(int num1,int num0,int cur){
  int c1,c2,i;

  if(cur==N){
    printf("%s\n",p);
    return;
  }

  c1=c2=0;

 if(num0<znum){//0未用完,则先放0
   p[cur]='0';
   dfs(num1,num0+1,cur+1);
 }

 if(num1<onum){//1未用完,则再放1
   p[cur]='1';
   dfs(num1+1,num0,cur+1);
 }

 return;
}
时间: 2024-12-23 18:58:21

The Hamming Distance Problem UVA 729 (01串的全排列)的相关文章

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

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>

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>

64. 海明距离(Hamming Distance)

[本文链接] http://www.cnblogs.com/hellogiser/p/hamming-distance.html [介绍] 在信息领域,两个长度相等的字符串的海明距离是在相同位置上不同的字符的个数,也就是将一个字符串替换成另一个字符串需要的替换的次数. 例如: xxxxyy和xxxxzz的海明距离是2: 111100 和 111111 的海明距离是2: 对于二进制数字来说,海明距离的结果相当于a^b结果中1的个数. [字符串] C++ Code 12345678910111213

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): 1806    Accepted Submission(s): 714 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal

461. Hamming Distance(leetcode)

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note:0 ≤ x, y < 2^31. Example: Input: x = 1, y = 4 Output: 2 Explanation:

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