// 题意:
// 输入两个整数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. WPF为提示信息文本增加闪烁效果

    程序通常需要显示某些提醒用户警示的信息,如:收件箱(40)其中数量闪烁就会起到警示效果.可以适用如下Storyboard实现: <ItemsControl.ItemTemplate> &l ...

  2. spring TaskExecutor

    TaskExecutor抽象 Spring 2.0 为执行器(Executor)处理引入了一个新的抽象层.Executor是Java 5的名词,用来表示线程池的概念.之所以用这个奇怪的名词,是因为实际 ...

  3. PHP 优化详解

    笔者收集的这些技巧来源较广,完整性不能保证. 由于数量较多,这些优化技巧没有经过测试.请各位看官在使用之前自行测试,毕竟这些技巧是否能派上用场,还是需要由PHP所在的独特环境所决定的. 目录索引 找到 ...

  4. pick定理:面积=内部整数点数+边上整数点数/2-1

    //pick定理:面积=内部整数点数+边上整数点数/2-1 // POJ 2954 #include <iostream> #include <cstdio> #include ...

  5. MVC linq To SQL更新数据库操作

    首先在视图中提交数据,使用Html.BeginForm() @using(Html.BeginForm()) { @Html.EditorForModel() //编辑模板.控制器中传过来的数据 &l ...

  6. win7中protel99添加元件库

    protel打开时默认添加Miscellaneous Devices.lib,点击ADD/REMOVE按钮添加所需要的库时,弹出“文件无法识别”. 点击面板右下方“FIND”按钮,弹出如下对话框时不要 ...

  7. 类的大小——sizeof 的研究

    类的大小——sizeof 的研究(1) 先看一个空的类占多少空间? class Base { public: Base(); ~Base(); }; 注意到我这里显示声明了构造跟析构,但是sizeof ...

  8. Linux_系统信息

    公司里一些仿真软件得进Linux系统,好奇公司用的什么Linux版本,于是搜罗了几个命令如下: 1  uname - Print system info -a, print all info -s, ...

  9. android读取data下得数据

    拥有Root权限的情况 adb shell su cd  /data/data/com.package 然后就可以直接读取 没有Root的情况 adb shell run-as com.package ...

  10. 【数据结构与算法分析——C语言描述】第二章总结 算法分析

    算法 算法(algorithm)是为求解一个问题需要遵循的.被清楚地指定的简单指令的集合. 数学基础 四个定义: 1.大O表示法: 如果存在正常数 c 和 n0 使得当 N ≥ n0时,T(N) ≤ ...