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) ...
随机推荐
- ubuntu 上更新安装 openoffice.org3的过程
方法一:手动安装1首先在“应用成程序--添加/删除”里卸载openoffice 2.4,可能openoffice.org 2.4 Draw因为关联而无法卸载,忽略,卸载其他几项.然后在中文官方网上下载 ...
- 百度地图Api之自定义标注:(获得标注的经纬度和中心经纬度即缩放度)
百度地图Api之自定义标注:(获得标注的经纬度和中心经纬度即缩放度) <%@ Page Language="C#" AutoEventWireup="true&qu ...
- selenium python (十一)alert/confirm/prompt的处理(js中的弹出框)
webdriver中处理js所生成的alert.confirm以及prompt,采用switch_to_alert()方法定位到alert/confirm/prompt.然后使用text/accept ...
- JavaScript高级程序设计(第三版)第五章 引用类型
5.2 Array类型 var colors = new Array(3); //创建一个包含3项的数组 var names = new Array("Greg"); //创建一个 ...
- Thu夏令营 总结
感觉这次thu夏令营简直就是爆RP啊 竟然签了无条件本一 [Waring]RP已空 话说这次考试设定 竟然是下午两点开始考试 考到五点- - 导致中午必须午睡 宾馆里清华也不近 按原本试机安排到12点 ...
- 解决SQL Server Always 日志增大的问题-摘自网络
配置了Alwayson之后,因为没有只能使用完全恢复模式,不能使用简单或大容量日志模式,所以日志不断增长,不能使用改变恢复模式的方式清空日志 手动操作收缩或截断日志也无效 读了一些文章后发现,有人使用 ...
- Good Number
Time Limit: 1000ms Problem Description: Let's call a number k-good if it contains all digits not exc ...
- vim插件开发初步
[vim插件开发初步] 将如下代码存在helloworld.vim, 放在~/.vim/plugin目录下,插件即可生效.:w保存代码后, 用:source命令执行后,也可以使用Helloworld命 ...
- HDU 5927 Auxiliary Set (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5927 题意: 给你一棵树,其中有一些'不重要'的点,要是这些'不重要'的点的子树中有两个重要的点的LC ...
- A - Oulipo
A - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...