// 题意:
// 输入两个整数N, H,按照字典序输出所有长度为N,恰好包含H个1的01串
// 规模:1<=H<=N<=16
// 算法A:2^N枚举,输出1的个数为H的。采用递归枚举

// 从bits[d]开始确定,已经用了c0个0和c1个1

算法A:递归枚举

// 算法A:2^N枚举,输出1的个数为H的。采用递归枚举
#include <cstdio>
#include <cstring>
const int maxn = 20;
int N, H, bits[maxn]; // 从bits[d]开始确定,已经用了c0个0和c1个1
void gen(int d, int c0, int c1) {
if(d == N) {
if(c1 != H) return;
for(int i = 0; i < N; i++) printf("%d", bits[i]);
printf("\n");
} else {
bits[d] = 0; gen(d+1, c0+1, c1);
bits[d] = 1; gen(d+1, c0, c1+1);
}
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &N, &H);
gen(0, 0, 0);
if(T) printf("\n");
}
return 0;
}

 

算法A改进:递归枚举加剪枝

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std; int n,h;
int buf[16];
void solve(int c0, int c1, int d)
{
if(d==n)
{
if(c1==h)
{
for(int i=0;i<n;i++)
printf("%d", buf[i]);
printf("\n");
}
return;
} if(c0<n-h)
{
buf[d]=0;
solve(c0+1, c1, d+1);
} if(c1<h)
{
buf[d]=1;
solve(c0, c1+1, d+1);
} } int main()
{
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &h);
solve(0, 0, 0);
if(T)
printf("\n");
} return 0;
}

 

算法B:二进制枚举子集

// 算法B:2^N枚举,输出1的个数为H的,采用直接枚举子集
#include <cstdio>
#include <cstring>
int N, H; int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &N, &H);
for(int i = 0; i < (1<<N); i++) {
int cnt = 0;
for(int j = 0; j < N; j++) if(i & (1<<j)) cnt++;
if(cnt == H) {
for(int j = N-1; j >= 0; j--) printf("%d", (i & (1<<j)) ? 1 : 0);
printf("\n");
}
}
if(T) printf("\n");
}
return 0;
}

算法C:

// 算法C:C(N,H)枚举,枚举的对象为0,所以枚举顺序就是字典序
#include <cstdio>
#include <cstring>
const int maxn = 20;
int N, H, zero[maxn]; // zero[i]为第i为是否为0 // 从第d个0的位置开始确定,取值范围是from~N-1
void gen(int d, int from) {
if(d == N-H) {
for(int i = 0; i < N; i++) printf("%d", zero[i] ? 0 : 1);
printf("\n");
} else {
for(int i = from; i < N; i++) {
zero[i] = 1;
gen(d+1, i+1);
zero[i] = 0;
}
}
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &N, &H);
memset(zero, 0, sizeof(zero));
gen(0, 0);
if(T) printf("\n");
}
return 0;
}

算法D: stl next_permutation

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std; const int N=100;
int s[N];
int n, r; void rcom()
{
do
{
for(int i=0;i<n;i++)
{
printf("%d", s[N-n+i]);
}
printf("\n");
}while(next_permutation(s+N-n, s+N));
} int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>r;
memset(s, 0, sizeof s);
for(int i=0;i<r;i++)
{
s[N-1-i]=1;
}
rcom();
if(T)
printf("\n");
} return 0;
}

729 - The Hamming Distance Problem的更多相关文章

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

    题意:给出数组的长度n,给出h,表示这个数组里面含有h个1,求其所有的排列 用next_permutation就可以了 #include<iostream> #include<cst ...

  2. [LeetCode&Python] Problem 461. Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. 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/Other ...

  4. HDU 4712:Hamming Distance

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  5. hdu 4712 Hamming Distance 随机

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  6. 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 pro ...

  7. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...

  8. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  9. hdu 4712 Hamming Distance ( 随机算法混过了 )

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

随机推荐

  1. [转] C# 绘制报表,使用Graphics.DrawString 方法

    原文 Graphics.DrawString 方法 在指定位置并且用指定的 Brush 和Font 对象绘制指定的文本字符串. public void DrawString( string s, Fo ...

  2. LR 解决中文乱码(来源——百度)

    因为我们使用的中文操作系统默认的中文编码格式是GB2312,所以LR对服务器的返回内容自动使用GB2312方式阅读的,但是几乎所有的中文网站现在都在使用UTF-8的方式来编码,由于解码编码的方式不同最 ...

  3. ifstream 流 判断文件是否结尾的函数eof(.xml

    pre{ line-height:1; color:#800080; font-size:16px;}.sysFunc{color:#627cf6;font-style:italic;font-wei ...

  4. Unity 5 引擎收费版和免费版的区别(转)

    最新Unity 5的Professional Edition(收费版)具备全新而强大的功能,除了全局动态光照或是最新的基于物理的着色器之外,也把原本分开销售的Team License放入,并含有12个 ...

  5. 捣蛋phpwind过滤器执行流程

    从上一篇我们就大概就知道过滤器的定义和怎样去配置,这一节来说说执行流程 public function run($handlerAdapter = null) { $handlerAdapter != ...

  6. Treap树

    Treap树算是一种简单的优化策略,这名字大家也能猜到,树和堆的合体,其实原理比较简单,在树中维护一个"优先级“,”优先级“ 采用随机数的方法,但是”优先级“必须满足根堆的性质,当然是“大根 ...

  7. effective c++:尽量替换define,确保对象使用前初始化

    #define ASPECT_RATIO 1.653 名为ASPECT_RATIO的值在预编译阶段被替换成1.653,如果在这个常量上出现编译错误,我们可能会困惑1.653的值是什么意思,于是将因为跟 ...

  8. 关于面试mysql优化的几点纪要

    1.减少查询次数 ,如何减少 ?  2. 表结构优化,如何优化 ? 3. 列选取原则  ? 4.建索引原则   ? 5.mysql语句优化 ?   6.增加mysql处理性能 ? 通过这几点, 再来说 ...

  9. windows 无法启动asp.net 状态服务 错误 0x8007277a

    处理方法:如果在启动ASP.NET State Service服务时遇到问题0x8007277a即无法启动或初始化,请尝试在命令行(CMD)中输入netsh winsock  reset(有可能是wi ...

  10. LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)

    http://lightoj.com/volume_showproblem.php?problem=1336 Sigma Function Time Limit:2000MS     Memory L ...