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
算法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的更多相关文章
- UVa 729 The Hamming Distance Problem【枚举排列】
题意:给出数组的长度n,给出h,表示这个数组里面含有h个1,求其所有的排列 用next_permutation就可以了 #include<iostream> #include<cst ...
- [LeetCode&Python] Problem 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 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 ...
- HDU 4712:Hamming Distance
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu 4712 Hamming Distance 随机
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- 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 ...
- Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和
B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- hdu 4712 Hamming Distance ( 随机算法混过了 )
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
随机推荐
- Symfony2学习笔记之表单
对于一个Web开发者来说,处理HTML表单是一个最为普通又具挑战的任务.Symfony2集成了一个Form组件,让处理表单变的容易起来.在这一节里,我们将从基础开始创建一个复杂的表单,学习表单类库中最 ...
- vector 释放内存 swap
相 信大家看到swap这个词都一定不会感到陌生,甚至会有这样想法:这不就是简单的元素交换嘛.的确,swap交换函数是仅次于Hello word这样老得不能老的词,然而,泛型算法东风,这个小小的玩意儿却 ...
- hdu 1527(威佐夫博奕)
题意:容易理解. 分析:威佐夫博奕的模板题. 代码实现: #include<stdio.h> #include<string.h> #include<math.h> ...
- hdu 1074(状态压缩dp+记录路径)
题意:给了n个家庭作业,然后给了每个家庭作业的完成期限和花费的实践,如果完成时间超过了期限,那么就要扣除分数,然后让你找出一个最优方案使扣除的分数最少,当存在多种方案时,输出字典序最小的那种,因为题意 ...
- 【转载】HBase基本概念和hbase shell常用命令用法
1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...
- 一篇关于apache commons类库的详解
1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...
- linux笔记_20150825_linux有什么好处
那么多人在用,linux到底有毛好处? 其实我也不太清楚,有人说免费,可是大家用windows也不要钱的.我想在天朝,要钱的软件不多吧.一个子也不用花.真心感谢为人民服务的那些大牛. 现在,除了在ub ...
- IGF职业组比赛
IGF职业组比赛 参赛资格: 面向亚太区所有独立游戏开发者(参见详细规则) 截止日期: 2015年7月20日 2015年IGF职业组七大奖项设置如下: * 最佳游戏(RMB20, 000) * 最佳移 ...
- python中的多线程【转】
转载自: http://c4fun.cn/blog/2014/05/06/python-threading/ python中关于多线程的操作可以使用thread和threading模块来实现,其中th ...
- TV端产品设计法则和分析
对TV端产品设计的分析太特么少了.翻遍网络,大多也是针对UI设计的分析,这篇从产品设计的角度,梳理下现有的TV端产品设计法则,顺道做点分析.(前方多图,高能预警) 目录: 1. TV端产品使用场景 2 ...