Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>     // std::cout
  2. #include <algorithm>    // std::random_shuffle
  3. #include <vector>       // std::vector
  4. #include <ctime>        // std::time
  5. #include <cstdlib>      // std::rand, std::srand
  6. #include <string>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. string to_string(int i)
  12. {
  13.     stringstream ss;
  14.     ss << i;
  15.     return ss.str();
  16. }
  17.  
  18. string convert(int base, int num)
  19. {
  20.     string s = "";
  21.     while(num != 0){
  22.         s += to_string(num%base);
  23.         num /= base;
  24.     }
  25.     reverse(s.begin(), s.end());
  26.     return s;
  27. }
  28.  
  29. string gen(vector<string> ar){
  30.     string s, s2 = "";
  31.     for(auto t: ar){
  32.         if(s.find(t) == string::npos){
  33.             s += t;
  34.         }
  35.     }
  36.     reverse(ar.begin(), ar.end());
  37.     for(auto t: ar){
  38.         if(s2.find(t) == string::npos){
  39.             s2 += t;
  40.         }
  41.     }
  42.     if(s.length() > s2.length()){
  43.         return s2;
  44.     }
  45.     else{
  46.         return s;
  47.     }
  48. }
  49.  
  50. int main () {
  51.   int i, n, k, entropy;
  52.   srand(unsigned(time(0)));
  53.   vector<string> nums;
  54.   string t, res;
  55.   cin >> n >> k >> entropy;
  56.   for (int i=0; i<pow(k, n); i++) {
  57.         //cout << convert(k, i);
  58.         nums.push_back(convert(k, i));
  59.   }
  60.  
  61.   res = gen(nums);
  62.   for (i=0; i<entropy; i++){
  63.     random_shuffle(nums.begin(), nums.end());
  64.     t = gen(nums);
  65.     if(t.length() < res.length()){
  66.         res = t;
  67.     }
  68.     //cout << t << '\n';
  69.   }
  70.   cout << "--------------\n" << res;
  71.   return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement