Okay, so I am trying to compile something right now and I am new to C++ so maybe the code itself is causing the error however no red marks show up in the code itself that Eclipse is showing me.
Here is what the error says
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/move.h:128:7:
error: assignment of read-only reference '__a'
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/move.h:129:7:
error: assignment of read-only reference '__b'
Any ideas on what I need to do? on a Win7, using Eclipse Juno for C++ with MingwCC
Here is what I am compiling, the only new thing I added was this "swap" thing that someone told me to use for my permutation program.
UPDATED
Permutation.cc
#include <iostream> // for cout
#include <cstdio> // for printf()
#include <sstream> // for stringstream
#include <stdio.h>
#include <string.h>
#include "Permutation.h"
using namespace std;
Permutation::Permutation() {
/* nothing needed in the constructor */
}
void Permutation::permute(string str) {
int low = 0;
int high = str.length();
int j;
if (low == high) {
cout << str << endl;
} else {
for (j = low; j <= high; j++) {
std::swap(str[low], str[j]);
permute(str, low + 1, high);
std::swap(str[low], str[j]);
}
}
}
void Permutation::permute(string str, int low, int high) {
// int j;
// if (low == high) {
// cout << str << endl;
// } else {
// for (j = low; j <= high; j++) {
// std::swap(str[j + low], str[j + j]);
// permute(str, low + 1, high);
// std::swap(str[j + low], str[j + j]);
// }
// }
}
Permutation.h
#pragma once
#include <string>
using namespace std;
class Permutation {
public:
Permutation();
void permute (string);
void permute (string, int, int);
private:
/* attemp to solve this problem without adding
* any instance variables/data members, but
* you may add private helper function members
* as many as you need */
};
main.cc
#include "Permutation.h"
int main()
{
Permutation p;
p.permute ("Permute");
p.permute ("--*--", 2, 3);
}
I rewrote the C code you linked to in C++:
// this method should be private or protected because
// str is passed by reference and will be modified !
// if you prefer a free standing function, don't add the
// declaration to the header, this for internal use only
void do_permute(std::string& str, unsigned i, unsigned n) {
// you COULD pass str by value here, which
// would remove the need to backtrack.
// however, it would create a new copy for every
// iteration which is terrible for performance,
// especially with long strings.
if(i==n)
std::cout << str << '\n';
else
for(unsigned j=i; j<=n; ++j) {
std::swap(str[i],str[j]);
do_permute(str,i+1,n);
std::swap(str[i],str[j]); // backtrack (undo swap)
}
}
// this is the public method;
// pass string by value (copy), to allow do_permute()
// to modify the string.
void permute(std::string str, unsigned i=0, unsigned n=0) {
if( n >= str.length() )
return; // prevent out of bounds access
// if n is 0 (default value) use the string length instead
do_permute(str, i, n ? n : (str.length()-1) );
}
int main() {
permute("BAR");
permute("FO0BAR", 3); // FOO***
permute("FO0BAR", 0, 2); // ***BAR
}
Figured out how to swap it correctly.
int low = 0;
int high = str.length() - 1;
// make sure the string is a permutation and not a partial mix.
if (low == high) {
cout << str << endl;
} else {
//Takes each initial letter, then permutes the remaining string. Then moves to next character.
for (int i = low; i <= high; i++) {
std::swap(str[low], str[i]);
permute(str, low + 1, high);
std::swap(str[low], str[i]);
}
}
Related
I'm trying to implement the quicksort on a string of characters. The output should give me an alphabetical order version of the input, however right now it's just giving me the original input. This was an attempt trying to translate the pseudo code from Intro to Algorithm 3rd edition on Quicksort.
Any help would be greatly appreciated, thanks!
Here's the pseudo code of quicksort from the book
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;
int partition_str(string A, int start, int finish){
char x = A[finish], temp;
int i = start - 1;
for (int j = start; j <= finish -1; j++){
if (A[j] <= x){
i ++;
temp = A[i]; A[i] = A[j]; A[j] = temp;
}
temp = A[i+1]; A[i+1] = A[finish]; A[finish] = temp;
return i+1;
}
}
string quicksort_char(string A, int start, int finish)
{
if (start < finish)
{
start = partition_str(A, start, finish);
quicksort_char(A, start, finish -1);
quicksort_char(A, start+1, finish);
}
return A;
}
int main(){
string test = "gsgkdsdkjs";
string result = quicksort_char(test, 0, 10);
cout << result << endl;
return 0;
}
In the pseudocode you linked, it mentions that partition() alters subarrays in place. This insinuates that you need to pass by reference, rather than by value. Notice the ampersand (&) I add in the function signature. Your code was passing by value, so it was making a copy of the input string, rather than altering it in place. In your quicksort() function, you wrote the code expecting that A will be altered by the function.
I cleaned up your code a bit here with the intent of making it clearer and look more like the pseudocode...
#include <iostream>
#include <string>
using namespace std;
void exchange(char& a, char& b)
{
char value_of_a = a;
char value_of_b = b;
a = value_of_b;
b = value_of_a;
};
int partition(string& A, int p, int r)
{
char x = A[r];
int i = p-1;
for (int j=p; j<=(r-1); ++j)
{
if (A[j] <= x)
{
i++;
exchange(A[i], A[j]);
}
}
exchange(A[i+1], A[r]);
return i+1;
};
void quicksort(string& A, int p, int r)
{
if (p < r)
{
int q = partition(A, p, r);
quicksort(A, p, q-1);
quicksort(A, q+1, r);
}
};
int main()
{
string input = "gsgkdsdkjs";
cout << "input string: " << input << endl;
quicksort(input, 0, input.size());
cout << "sorted string: " << input << endl;
return 0;
}
In your partition_str() function you pass in string A by value, which makes a copy of A rather than using the same A you passed in. It then performs some operations and returns an integer. The copy of A is then thrown away and your original A variable was never modified. This means that if you want your variable A to be changed, you must pass by reference.
Also, don't be confused by the function argument naming. Your partition_str() function signature is:
int partition_str(string A, int start, int finish)
The fact the 'string A' is defined as an argument does not mean that it is related to any other variable in your code called 'A'. It is merely a way of referring to particular argument that was passed in.
#include <iostream>
#include <cstdlib>
using std:: cin;
using std:: cout;
using std:: endl;
const int N=10;
void readarray(int array[], int N);
int bubble_sort (int array[], int size, int round,
int place);
int main ()
{
int array[N];
readarray( array, N );
int round, place;
cout << bubble_sort(array, N, place, round);
return EXIT_SUCCESS;
}
void readarray(int array[], int N)
{
int i=0;
if (i < N)
{
cin >> array[i];
readarray(array+1, N-1);
}
}
int bubble_sort (int array[], int size, int round,
int place)
{
round =0;
place =0;
if (round < N-1) // this goes over the array again making sure it has
// sorted from lowest to highest
{
if (place < N - round -1) // this sorts the array only 2 cells at a
// time
if (array[0] > array[1])
{
int temp = array[1];
array[1]=array[0];
array[0]=temp;
return (array+1, size-1, place+1, round);
}
return (array+1, size-1, place, round+1);
}
}
I know how to do a bubble sort using two for loops and I want to do it using recursion. Using loops you require two for loops and I figured for recursion it might also need two recursive functions/calls. This is what I have so far. The problem is that its outputting only one number, which is either 1 or 0. I'm not sure if my returns are correct.
In c++11, you can do this:
#include <iostream>
#include <vector>
void swap(std::vector<int &numbers, size_t i, size_t j)
{
int t = numbers[i];
numbers[i] = numbers[j];
numbers[j] = t;
}
bool bubble_once(std::vector<int> &numbers, size_t at)
{
if (at >= numbers.size() - 1)
return false;
bool bubbled = numbers[at] > numbers[at+1];
if (bubbled)
swap(numbers, at, at+1);
return bubbled or bubble_once(numbers, at + 1);
}
void bubble_sort(std::vector<int> &numbers)
{
if ( bubble_once(numbers, 0) )
bubble_sort(numbers);
}
int main() {
std::vector<int> numbers = {1,4,3,6,2,3,7,8,3};
bubble_sort(numbers);
for (size_t i=0; i != numbers.size(); ++i)
std::cout << numbers[i] << ' ';
}
In general you can replace each loop by a recursive function which:
check the guard -> if fail return.
else execute body
recursively call function, typically with an incremented counter or something.
However, to prevent a(n actual) stack overflow, avoiding recursion where loops are equally adequate is good practice. Moreover, a loop has a very canonical form and hence is easy to read for many programmers, whereas recursion can be done in many, and hence is harder to read, test and verify. Oh, and recursion is typically slower as it needs to create a new stackframe (citation needed, not too sure).
EDIT
Using a plain array:
#include <iostream>
#include <vector>
#define N 10
void swap(int *numbers, size_t i, size_t j)
{
int t = numbers[i];
numbers[i] = numbers[j];
numbers[j] = t;
}
bool bubble_once(int *numbers, size_t at)
{
if (at >= N - 1)
return false;
bool bubbled = numbers[at] > numbers[at+1];
if (bubbled)
swap(numbers, at, at+1);
return bubbled or bubble_once(numbers, at + 1);
}
void bubble_sort(int *numbers)
{
if ( bubble_once(numbers, 0) )
bubble_sort(numbers);
}
int main() {
int numbers[N] = {1,4,3,6,2,3,7,8,3,5};
bubble_sort(numbers);
for (size_t i=0; i != N; ++i)
std::cout << numbers[i] << ' ';
}
Please read this post
function pass(i,j,n,arr)
{
if(arr[i]>arr(j))
swap(arr[i],arr[j]);
if(j==n)
{
j=0;
i=i+1;
}
if(i==n+1)
return arr;
return pass(i,j+1,n,arr);
}
Okay, so I need some help getting my string to swap around.
Here is the overall code of what I am trying to do, but I can't just move the string around. I started off trying to convert it to characters but the majority of replies said to just use the std::swap function, however I am really lost in using this...
My overall goal is to permute a string, which can be specified to a certain section of the string. I am new to C++, I am just unsure how to use C++ methods/functions in order to achieve this.
(there is also a main.cc and Permutation h. but its only for defining variables, skeletal code basically)
All help appreciated, I shall check back here in about 2 hours.
UPDATED CODE)
#include <iostream> // for cout
#include <cstdio> // for printf()
#include <sstream> // for stringstream
#include <stdio.h>
#include <string.h>
#include "Permutation.h"
using namespace std;
Permutation::Permutation() {
/* nothing needed in the constructor */
}
void Permutation::permute(const string& str) {
string stringnew = str;
int j;
int low = 0;
int high = str.length();
cout << stringnew << endl;
for (j = 0; j <= high; j++) {
string strtemp = stringnew[j];
std::swap((strtemp + low), (strtemp + j));
permute(str, low + 1, high);
std::swap(str[j + low], str[j + j]);
}
}
void Permutation::permute(const string& str, int low, int high) {
// int j;
// if (low == high) {
// cout << str << endl;
// } else {
// for (j = low; j <= high; j++) {
// std::swap(str[j + low], str[j + j]);
// permute(str, low + 1, high);
// std::swap(str[j + low], str[j + j]);
// }
// }
}
You must work through the class interface. You cannot get a writeable character array from a std::string.
What you can do is use the array subscript operator and access it as str[i]. You can also use iterators.
The reason for this is that prior to C++03, std::string was not required to be a character array. It could be discontinuous. At least one implementation used a std::deque style "array of pointers to arrays" backing store, which gave it fast insert, prepend and delete-from-the-middle abilities.
Also, from an Object Oriented programming design perspective, it is Not Nice to reach into an object's guts and rearrange them.
Just for fun because I wanted a break from work, some code that messes with a string using array subscripts:
#include <cctype>
#include <string>
#include <iostream>
void uc(std::string &s)
{
size_t i;
const size_t len = s.length();
for(i=0; i<len; ++i) {
s[i] = toupper(s[i]);
}
}
void mix(std::string &s)
{
size_t i;
const size_t len = s.length();
for(i=1; i<len/2+1; ++i) {
std::swap(s[i-1], s[len-i]);
}
}
int main()
{
std::string s("Test String");
uc(s);
std::cout << s << std::endl;
mix(s);
std::cout << s << std::endl;
return 0;
}
Just take the c_str()-function
std::string str("I'm a text");
char *pStr = str.c_str();
This is C++ not java as in thread you pointed.
First of all
char[] x
is valid declaration only for compile time known sizes of table.
The other thing is that std::string does not have .toCharArray method but it has .c_str() method you can use to get const char* from std::string.
HTH
I'm writing a program that counts all binary trees with n nodes and height k. Every node has 0 or 2 children. The program works but I wanted to add some memoization because the answer is always the same for some particular n and k.
I could create an multidimensional array of pairs but I already have my useful struct now. How could I declare and use this mem variable. I didn't find a good answer on this. I understand pointers but I would prefer a method without memory management.
This is an exercise from the USACO training program btw.
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
struct state {
int down, half;
state(int d, int h) : down(d), half(h) {}
int valid() {
return down != -1 && half != -1;
}
};
state mem[200][100];
state cnt(int n, int k)
{
if (mem[n][k].valid())
return mem[n][k];
if (n == 1)
return state(k == 1, k != 1);
if (n > pow(2, k) - 1)
return state(-1, -1);
state total(0, 0);
for (int i = 1; i < n - 1; ++i) {
state left = cnt(i, k - 1);
state right = cnt(n - i - 1, k - 1);
if (left.valid() && right.valid()) {
total.down += left.down * right.down +
left.down * right.half +
left.half * right.down;
total.half += left.half * right.half;
}
}
return mem[n][k] = state(total.down % 9901, total.half % 9901);
}
int main()
{
ofstream fout ("nocows.out");
ifstream fin ("nocows.in");
int n, k;
fin >> n >> k;
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= k; ++j)
mem[i][j] = state(-1, -1);
cout << cnt(n, k).down << endl;
return 0;
}
You can use a vector of vectors:
std::vector<std::vector<state> > mem;
You can dynamically add to it and needn't worry about size (although if you roughly know the size, you can pre-allocate it to avoid resizing), and also memory clean-up is automatic - when the vector goes out of scope, its components will also be deleted.
Your code doesn't work because you don't have a default constructor for state.
The thing is, when you write state mem[200][100]; the compiler will try to create 100*200 state objects, but it can't. To make this work, you'd need a default constructor in state:
struct state {
state() : down(0), half(0) {} //default constructor
int down, half;
state(int d, int h) : down(d), half(h) {}
int valid() {
return down != -1 && half != -1;
}
};
I need to generate all permutation of a string with selecting some of the elements. Like if my string is "abc" output would be { a,b,c,ab,ba,ac,ca,bc,cb,abc,acb,bac,bca,cab,cba }.
I thought a basic algorithm in which I generate all possible combination of "abc" which are {a,b,c,ab,ac,bc,abc} and then permute all of them.
So is there any efficient permutation algorithm by which I can generate all possible permutation with varying size.
The code I wrote for this is :
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
using namespace std;
int permuteCount = 1;
int compare (const void * a, const void * b)
{
return ( *(char*)a - *(char*)b);
}
void permute(char *str, int start, int end)
{
// cout<<"before sort : "<<str;
// cout<<"after sort : "<<str;
do
{
cout<<permuteCount<<")"<<str<<endl;
permuteCount++;
}while( next_permutation(str+start,str+end) );
}
void generateAllCombinations( char* str)
{
int n, k, i, j, c;
n = strlen(str);
map<string,int> combinationMap;
for( k =1; k<=n; k++)
{
char tempStr[20];
int index =0;
for (i=0; i<(1<<n); i++) {
index =0;
for (j=0,c=0; j<32; j++) if (i & (1<<j)) c++;
if (c == k) {
for (j=0;j<32; j++)
if (i & (1<<j))
tempStr[ index++] = str[j];
tempStr[index] = '\0';
qsort (tempStr, index, sizeof(char), compare);
if( combinationMap.find(tempStr) == combinationMap.end() )
{
// cout<<"comb : "<<tempStr<<endl;
//cout<<"unique comb : \n";
combinationMap[tempStr] = 1;
permute(tempStr,0,k);
} /*
else
{
cout<<"duplicated comb : "<<tempStr<<endl;
}*/
}
}
}
}
int main () {
char str[20];
cin>>str;
generateAllCombinations(str);
cin>>str;
}
I need to use a hash for avoiding same combination, so please let me know how can I make this algorithm better.
Thanks,
GG
#include <algorithm>
#include <iostream>
#include <string>
int main() {
using namespace std;
string s = "abc";
do {
cout << s << '\n';
} while (next_permutation(s.begin(), s.end()));
return 0;
}
Next_permutation uses a constant size, but you can add a loop to deal with varying size. Or just store in a set to eliminate the extra dupes for you:
#include <set>
int main() {
using namespace std;
string s = "abc";
set<string> results;
do {
for (int n = 1; n <= s.size(); ++n) {
results.insert(s.substr(0, n));
}
} while (next_permutation(s.begin(), s.end()));
for (set<string>::const_iterator x = results.begin(); x != results.end(); ++x) {
cout << *x << '\n';
}
return 0;
}
I don't think you can write much faster program than you have already. The main problem is the output size: it has order of n!*2^n (number of subsets * average number of permutations for one subset), which is already > 10^9 for a string of 10 different characters.
Since STL's next_permutation adds very limited complexity for such small strings, your program's time complexity is already nearly O(output size).
But you can make your program a bit simpler. In particular, for( k =1; k<=n; k++) loop seems unnecessary: you already calculate size of subset in variable c inside. So, just have int k = c instead of if (c == k). (You'll also need to consider case of empty subset: i == 0)
edit
Actually, there's only 9864100 outputs for n == 10 (not ~ 10^9). Still, my point remains the same: your program already wastes only "O(next_permutation)" time for each output, which is very, very little.