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) ...
随机推荐
- 嵌入式 VFS: Cannot open root device "mtdblock2" or unknown-block(2,0)
系统启动后,虽然nand驱动表现正常,但是最后挂载rootfs时候出错: Kernel command line: root=/dev/mtdblock2 rw init=/linuxrc conso ...
- 小结JS中的OOP(中)
此篇文章主要是提炼<JavaScript高级程序设计>中第六章的一些内容. 一:JS中OOP相关的概念 开始之前先总结JS中OOP相关的一些概念: 构造函数:JS中的构造函数就是普通的函数 ...
- call Kernelized Correlation Filters Tracker(Matab) in Qt(c++)
recently, i need call the KCF tracker in my graduation project. the KCF tracker is fast and best per ...
- jQuery 关于点击菜单项,使子条目“向上”展开效果的实现
为什么做了这样一个的功能呢?原因是前一段时间jQuery群里有个朋友想实现这样一个东东,大家都知道jQuery中有现成的slideDown和slideUp方法,但那是向下展开,而这个是一个完全相反的效 ...
- 比赛组队问题 --- 递归解法 --- java代码 --- 八皇后问题
两队比赛,甲队为A.B.C3人,乙队为X.Y.Z3人.已知A不和X比,C不和X.Z比,请编程序找出3队赛手名单 采用了与八皇后问题相似的解法,代码如下: 如有疑问请链接八皇后问题的解法:http:// ...
- bzoj1036 树的统计Count
第一次写链剖,于是挑了个简单的裸题写. 以下几点要注意: 1.链剖中的height是从根到该店经过的轻边个数 2.分清num与sum..... #include<cstdio> #incl ...
- 棒棒的毛笔字PS教程
跟大家分享一下毛笔字怎么做出来的,主要通过字体和素材叠加,十分简单,喜欢的一起练习.做完记得交作业. 先看看最终效果: 在网上是不是经常看这些碉堡了的毛笔感觉是不是很羡慕啊,现在我就教大家怎么做出这样 ...
- Linux网卡启动报错(this device is not active)
重启网络服务 service network restart 报如下错误: shutting down interface eth0: error:device "eth0" ...
- Raspberry Pi上手
2013-05-21 买的树莓派终于到手了,嘿嘿.我在官方代理ICKEY买的,是英国版,B型. 上手教程可以根据Getting Started with Raspberry Pi(网上有电子版免费下载 ...
- LeetCode(2) - Add Two Numbers
一道比较基本的LinkedList的题目.题目要求是这样,现在有两个LinkedList,(2 -> 4 -> 3)和(5 -> 6 -> 4),然后从头开始,把每个node ...