How to get all possible binary numbers in a vector - binaryfiles

I'm looking for a way to generate all possible binary combinations and insert that into a vector of strings.
For example, if I have n=3, I would like to create a string of vectors that contains all the possible combinations from ["000", ..., "111"].
I have created a code that can generate binary strings of the values.
std::vector<std::string> get_combs(std::vector<std::string> vector, const int Z){
//I'm not sure why this line causes the error
constexpr size_t N = Z;
for(size_t i=0; i<(1<<N); ++i) {
std::bitset<N> bs = i;
vec.push_back(bs.to_string());
}
return vec;
}
// Driver Code
int main()
{
int Z = 2;
std::vector<std::string> values;
values = get_combs(values, const int Z);
}
But right now it's just printing as 0 0 0 1 1 0 1 1

A non-recursive converting version (loop over all the numbers and convert them to strings of binary digits):
std::string to_binstring(int x, int width)
{
std::string s(width, '0');
for (int i = 0; i < width; i++)
{
s[i] = '0' + x % 2;
x /= 2;
}
std::reverse(s.begin(), s.end());
return s;
}
std::vector<std::string> all_binstrings(int width)
{
const unsigned int limit = 1 << width;
std::vector<std::string> bins;
for (unsigned int i = 0; i < limit; i++)
{
bins.push_back(to_binstring(i, width));
}
return bins;
}
A recursive string-building version (attach '0' and '1' to all of the shorter strings):
std::vector<std::string> recursive_binstrings(int width)
{
if (width == 1)
{
return {"0", "1"};
}
std::vector<std::string> rest = recursive_binstrings(width-1);
std::vector<std::string> appended;
for (const auto& s: rest)
{
appended.push_back(s + '0');
appended.push_back(s + '1');
}
return appended;
}

Is N known at compile-time? If so std::bitset<N> could be useful:
#include <iostream>
#include <bitset>
#include <vector>
using namespace std;
int main()
{
constexpr size_t N = 3;
std::vector<std::string> vec;
for (size_t i=0; i<(1<<N); ++i) {
std::bitset<N> bs = i;
vec.push_back(bs.to_string());
}
// Prints "000 001 010 011 100 101 110 111"
for (auto& elem: vec) cout << elem << ' ';
cout << endl;
return 0;
}
https://godbolt.org/z/WxWz43

Related

Template Type Matrix Class Addition: error in Proper Addition

Can anyone help me out finding why the sum of my matrices is not how they should be?
template <typename T>
Matrix<T> Matrix<T>::operator + (const Matrix<T> &M){
Matrix<T> tmp(m,n,M.x);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
tmp.Mat[i][j]+= Mat[i][j]+ M.Mat[i][j];
return tmp;
}
This is my program's body:
#include <iostream>
#include <vector>
template <typename T>
class Matrix {
private:
unsigned int m; unsigned int n;
std::vector<T> x;
std::vector<T> y;
std::vector<std::vector<int>> Mat;
public:
Matrix (unsigned int m, unsigned int n, std::vector<T> x);
Matrix (const Matrix<T> &M); //= default;
// Matrix ();
Matrix<T> operator = (const Matrix<T> &M); // Assignment
Matrix<T> operator + (const Matrix<T> &M); // Addition
Matrix<T> operator - (const Matrix<T> &M); // Subtraction
Matrix<T> operator * (const T &scalar); // Scalar Multiplication
Matrix<T> operator * (const Matrix<T> &M); // Matrix Multiplication
friend std::ostream& operator << (std::ostream& os, const Matrix<T> &M){
for (int i = 0; i< M.m; i++){
for (int j = 0; j< M.n; j++){
os << M.Mat[i][j] << ' ';
}
os << '\n';
}
os << '\n' ;
return os;
}
};
template <typename T>
Matrix<T>::Matrix (unsigned int m, unsigned int n, std::vector<T> x){ //constructor
this -> m = m;
this -> n = n;
this -> x = x;
int index = 0;
Mat.resize(m);
for (unsigned int i = 0; i < Mat.size(); i++) {
Mat[i].resize(n);
}
for (unsigned int i = 0; i<m; i++){
for (unsigned int j = 0; j<n; j++){
Mat[i][j] = x[index];
index++;
}
}
}
template<typename T>
Matrix<T>::Matrix(const Matrix &M) //copy constructor
:
m(M.m),
n(M.n),
Mat(M.Mat)
{}
And this is my main:
Note: We were required in the initialization that the constructor should take in 2 unsigned integers and a linear container where the elements will be divided to rows and columns according to the inputted.
int main(){
std::vector<int> x = {1,2,3,4};
std::vector<int> y = {5,6,7,8};
std::vector<int> z = {9,10,11,12};
Matrix<int> A{2,2,x};
Matrix<int> B{2,2,y};
Matrix<int> C{2,2,z};
C = B;
std::cout << "A\n" << A;
std::cout << "B\n" << B;
std::cout << "C\n" << C;
Matrix<int> E(A + B);
std::cout << "E\n" << E;
}
When I add A and B I always get
11 14
17 20
It's like the matrix B doubles before adding to A
Thanks in advance!
A.operator+(M) computes A + 2*M. You use the contents of M twice - once when constructing tmp ( Matrix<T> tmp(m,n,M.x) ) and again when updating it ( tmp.Mat[i][j]+= Mat[i][j]+ M.Mat[i][j] )

Generating random numbers into an array which prints to a Txt file. Before hitting the text file, the numbers need to be sorted

so my objective is to basically print out random numbers from 40,000 to 1,000,000 to a txt file, sorted using the heap method. I can print to the text file just fine with random numbers, but I am a bit stuck at sorting them using a heap method. I started a method and got a bit lost half way through after looking at some tutorials. Any thoughts/ helpful comments? Im literally new to stack overflow (posting wise) so forgive me if I didnt place this here correctly. Thank you!
//Heapsort algorithm
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/*class generateDataSet {
public: static const int MAX_VALUE = 1000000;
public: static const bool SORTED = false;
public: static const bool REVERSE_SORTED = false;
};
*/
int main()
{
const int array= 16;
int arrayNum[array];
srand(time(NULL));
int upB = 1000000;
int loB = 40000;
int temp;
ofstream inputFile;
inputFile.open("randomData.txt");
if (inputFile.is_open())
{
for (int i = 0; i < array; i++)
{
arrayNum[i] = (rand()% (upB - loB + 1)) + loB;
inputFile << arrayNum[i] << "\n";
}
}
return 0;
}
void MaxHeapify(int d[], int i, int n)
{
int j;
int temp;
temp = d[i];
j = 2 * 1;
while (j <= n)
{
if (j < n && d[j + 1] > d[j])
j = j + 1;
if (temp > d[j])
break;
else if (temp <= d[j])
{
d[j / 2] = d[j];
j = 2 * j;
}
}
}
void heapSort(int d[], int n)
{
int i;
int temp;
for (i = n; i >= 2; i);
}

Why do I keep getting TLE for POJ 1984 -- Navigation Nightmare?

#include <iostream>
#include <vector>
#include <cassert>
#include <queue>
using namespace std;
struct vectors{
int x;
int y;
bool visited;
vectors(int x = 0, int y = 0){
this -> x = x;
this -> y = y;
visited = false;
}
bool isNotFound(){
return visited == false;
}
void setZero(){
x = 0;
y = 0;
}
vectors operator+(vectors others){
return vectors(x + others.x, y + others.y);
}
vectors operator*(int others){
return vectors(x * others, y * others);
}
};
struct node{
int adjFarm;
int length;
char direction;
node(int f, int l, char d){
adjFarm = f;
length = l;
direction = d;
}
};
int num_villages;
char inverse_direction(char d){
assert(d == 'N' || d == 'E' || d == 'W' || d == 'S' );
switch(d){
case 'S': return 'N';
case 'N': return 'S';
case 'E': return 'W';
case 'W': return 'E';
}
}
void AddToPaths(int farm1, int farm2, char direction, int length, vector< vector<node> > &pathGraph){
pathGraph.at(farm1).push_back(node(farm2, length, direction));
pathGraph.at(farm2).push_back(node(farm1, length, inverse_direction(direction)));
}
vectors directionVector(int d){
switch(d){
case 'N': return vectors(0, 1);
case 'E': return vectors(1, 0);
case 'W': return vectors(- 1, 0);
case 'S': return vectors(0, - 1);
}
}
void print_coords(vector< vectors > coords){
for(int i = 1; i < num_villages + 1; i ++){
cout << "farm: " << i << " coordinates x at farm: " << coords.at(i).x << " coordinated y at farm: " << coords.at(i).y << endl;
}
}
void update(char direction, int newFarm, int length, int currentFarm, vector <vectors> &coords){
vectors directions = directionVector(direction);
coords.at(newFarm) = coords.at(currentFarm) + directions * length;
coords.at(newFarm).visited = true;
}
void computeCoords(vector <vectors> &coords, vector< vector<node> > &pathGraph){
queue <int> frontier;
frontier.push(1);
coords.at(1).visited = true;
while(!frontier.empty()){
int currentFarm = frontier.front();
frontier.pop();
for(int i = 0; i < pathGraph.at(currentFarm).size(); i ++){
node options = pathGraph.at(currentFarm).at(i);
if(coords.at(options.adjFarm).isNotFound()){
update(options.direction, options.adjFarm, options.length, currentFarm, coords);
frontier.push(options.adjFarm);
}
}
}
}
struct UnionFind{
vector<int> L;
UnionFind(int num_villages){
L.resize(num_villages + 1);
for(int i = 1; i <= num_villages; i ++){
L.at(i) = i;
}
}
int find(int x){
if(x == L.at(x)) return x;
int root = find(L.at(x));
L.at(x) = root;
return root;
}
int Union(int x, int y){
int root1 = find(x);
int root2 = find(y);
L.at(y) = root1;
}
};
int pos;
int query(int start, int destination, int order, UnionFind &reachables, vector<vectors> &coords, vector<vector<int> > &source_destination_order){
while(pos <= order){
reachables.Union(reachables.find(source_destination_order.at(pos).at(0)), reachables.find(source_destination_order.at(pos).at(1)));
pos ++;
}
if(reachables.find(start) == reachables.find(destination)){
return abs(coords.at(start).x - coords.at(destination).x) + abs(coords.at(start).y - coords.at(destination).y);
}
else{
return -1;
}
}
int main(void){
int num_roads;
cin >> num_villages;
cin >> num_roads;
vector <vector<node> > pathGraph(num_villages + 1);
vector <vectors > coords(num_villages + 1);
vector <vector <int> > source_destination_order(num_villages + 1, vector<int> (2));
//Adding inforamtion about the farms
int farm1;
int farm2;
char direction;
int length;
int source;
for(int i = 0; i < num_roads; i ++){
cin >> farm1;
cin >> farm2;
cin >> length;
cin >> direction;
AddToPaths(farm1, farm2, direction, length, pathGraph);
source_destination_order.at(i + 1).at(0) = farm1;
source_destination_order.at(i + 1).at(1) = farm2;
}
computeCoords(coords, pathGraph);
int numQueries;
cin >> numQueries;
int start;
int destination;
int order;
int result;
UnionFind reachables(num_villages);
pos = 1;
for(int i = 0; i < numQueries; i ++){
cin >> start;
cin >> destination;
cin >> order;
result = query(start, destination, order, reachables, coords, source_destination_order);
cout << result << endl;
}
}
I tried to create an undirected acyclic graph with the farms as the vertices and the roads as the edges, and then use BFS to compute the coordinates of each farm relative to the first farm. Afterwards I used the union find structure to create disjoint sets of farms that are reachable from each other at the time of the query. However, it seems like my code takes too long to run, how should I fix it?

c++ - how to convert char to int?

I try to write a function who convert a string to an integer (like atoi). I don't see why my function "convertir" don't print my variable "res " whereas "test 1 " "test 2"... "test 4" is printed. I let you look at my code and if you see something bad tell me please.
#include "stdio.h"
#include "stdlib.h"
int xpown(int x, int n); // x^n
int lent(char str[]); // return length of string
int convertir(char s[]); //convert char to int
int main(){
char s[] ="1234";
convertir(s);
return 0;
}
int xpown(int x, int n){
int res = 1;
while (n != 1){
res= res*x;
n--;
}
return res;
}
int lent(char str[]){
int res =0;
int i=0;
while (str[i] != '\0'){
res=res+1;
i++;
}
return res;
}
int convertir(char s[]){
int res = 0;
int i = lent(s);
int j = 0;
char c = s[j];
while (c != '\0'){
c=s[j];
printf("test %d \n", j);
res = res + (c - 48) * xpown(10,i);
i--;
j++;
}
printf("%d", res);
}
You're setting i too high. Consider the simplest case, where s has 1 digit. You want to multiply that digit by 1 (100), not 10 (101). So it should be:
int i = lent(s) - 1;
BTW, you shouldn't hard code the value 48, use '0':
res += (c - '0') * xpown(10, i);
The standard function atoi() will likely do what you want.

Find longest non-decreasing sequence

Given the following question,
Given an array of integers A of length n, find the longest sequence {i_1, ..., i_k} such that i_j < i_(j+1) and A[i_j] <= A[i_(j+1)] for any j in [1, k-1].
Here is my solution, is this correct?
max_start = 0; // store the final result
max_end = 0;
try_start = 0; // store the initial result
try_end = 0;
FOR i=0; i<(A.length-1); i++ DO
if A[i] <= A[i+1]
try_end = i+1; // satisfy the condition so move the ending point
else // now the condition is broken
if (try_end - try_start) > (max_end - max_start) // keep it if it is the maximum
max_end = try_end;
max_start = try_start;
endif
try_start = i+1; // reset the search
try_end = i+1;
endif
ENDFOR
// Checking the boundary conditions based on comments by Jason
if (try_end - try_start) > (max_end - max_start)
max_end = try_end;
max_start = try_start;
endif
Somehow, I don't think this is a correct solution but I cannot find a counter-example that disapprove this solution.
anyone can help?
Thank you
I don't see any backtracking in your algorithm, and it seems to be suited for contiguous blocks of non-decreasing numbers. If I understand correctly, for the following input:
1 2 3 4 10 5 6 7
your algorithm would return 1 2 3 4 10 instead of 1 2 3 4 5 6 7.
Try to find a solution using dynamic programming.
You're missing the case where the condition is not broken at its last iteration:
1, 3, 5, 2, 4, 6, 8, 10
You'll never promote try_start and try_end to max_start and max_end unless your condition is broken. You need to perform the same check at the end of the loop.
Well, it looks like you're finding the start and the end of the sequence, which may be correct but it wasn't what was asked. I'd start by reading http://en.wikipedia.org/wiki/Longest_increasing_subsequence - I believe this is the question that was asked and it's a fairly well-known problem. In general cannot be solved in linear time, and will also require some form of dynamic programming. (There's an easier n^2 variant of the algorithm on Wikipedia as well - just do a linear sweep instead of the binary search.)
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <assert.h>
template<class RandIter>
class CompM {
const RandIter X;
typedef typename std::iterator_traits<RandIter>::value_type value_type;
struct elem {
value_type c; // char type
explicit elem(value_type c) : c(c) {}
};
public:
elem operator()(value_type c) const { return elem(c); }
bool operator()(int a, int b) const { return X[a] < X[b]; } // for is_sorted
bool operator()(int a, elem b) const { return X[a] < b.c; } // for find
bool operator()(elem a, int b) const { return a.c < X[b]; } // for find
explicit CompM(const RandIter X) : X(X) {}
};
template<class RandContainer, class Key, class Compare>
int upper(const RandContainer& a, int n, const Key& k, const Compare& comp) {
return std::upper_bound(a.begin(), a.begin() + n, k, comp) - a.begin();
}
template<class RandIter>
std::pair<int,int> lis2(RandIter X, std::vector<int>& P)
{
int n = P.size(); assert(n > 0);
std::vector<int> M(n);
CompM<RandIter> comp(X);
int L = 0;
for (int i = 0; i < n; ++i) {
int j = upper(M, L, comp(X[i]), comp);
P[i] = (j > 0) ? M[j-1] : -1;
if (j == L) L++;
M[j] = i;
}
return std::pair<int,int>(L, M[L-1]);
}
int main(int argc, char** argv)
{
if (argc < 2) {
fprintf(stderr, "usage: %s string\n", argv[0]);
return 3;
}
const char* X = argv[1];
int n = strlen(X);
if (n == 0) {
fprintf(stderr, "param string must not empty\n");
return 3;
}
std::vector<int> P(n), S(n), F(n);
std::pair<int,int> lt = lis2(X, P); // L and tail
int L = lt.first;
printf("Longest_increasing_subsequence:L=%d\n", L);
for (int i = lt.second; i >= 0; --i) {
if (!F[i]) {
int j, k = 0;
for (j = i; j != -1; j = P[j], ++k) {
S[k] = j;
F[j] = 1;
}
std::reverse(S.begin(), S.begin()+k);
for (j = 0; j < k; ++j)
printf("%c", X[S[j]]);
printf("\n");
}
}
return 0;
}

Resources