Best shuffle: Difference between revisions

enhancements for C++
(C++ snippet)
(enhancements for C++)
Line 673:
#include <random>
#include <time.h>
 
using namespace std;
 
template <class S>
Line 680 ⟶ 678:
public:
S operator()(const S& s1) {
S s2 = shuffle(s1);
shuffle(s2);
for (unsigned i = 0; i < s2.length(); i++)
if (s2[i] == s1[i])
Line 691 ⟶ 688:
break;
}
return s1 + '\n' + s2 + " (" + std::to_string(count(s2, s1)) + ')';
}
 
private:
static voidS shuffle(const S& sin) {
shuffle(s2)S s = in;
for (int i = s.length() - 1 ; i >= 1; i--) {
auto r = rand() % i + 1;
auto tmp = s[i];
s[i] = s[r];
s[r] = tmp;
}
return s;
}
 
Line 716 ⟶ 715:
srand(time(NULL));// initialize random seed
 
using namespace std;
string words[] = {"abracadabra", "seesaw", "grrrrrr", "pop", "up", "a"};
BestShuffle<stringbasic_string<char>> bs;
for (stringauto w : words) cout << bs(w) << endl;
return 0;
}</lang>
Anonymous user