The Hamming Distance Problem Solution
#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int n, r;
int number[20];
vector<string>s;
void combination(int pos, int last)
{
if(pos == r+1)
{
int j = 1;
string str = "";
for(int i=1; i<=n; i++)
{
if(i == number[j] && j<=r)
{
j++;
str += "1";
}
else
str += "0";
}
s.push_back(str);
return;
}
for(int i=last+1; i<=n-r+pos; i++)
{
number[pos] = i;
combination(pos+1, i);
}
}
int main()
{
fast
int t, f = 1;
cin >> t;
while(t--)
{
cin >> n >> r;
if(f == 1)
f = 0;
else
cout << "\n";
combination(1, 0);
sort(s.begin(), s.end());
for(int i=0; i<s.size(); i++)
cout << s[i] << "\n";
s.clear();
}
return 0;
}
Comments
Post a Comment