Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <ctime>
  6. #include <string>
  7.  
  8.  
  9. using namespace std;
  10.  
  11.  
  12. void print(vector<int> v){
  13. for (int i = 0; i<v.size(); i++)
  14. cout << v[i] << " ";
  15. cout << "\n===================================\n";
  16. }
  17.  
  18. void generate_int_array(vector<int> &v, int n){
  19. for (int i = 0; i<v.size(); i++)
  20. v[i] = rand() % 100;
  21. }
  22.  
  23. string generate_string(int cnt_of_char){
  24. char chars[36] = {
  25. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g',
  26. 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '1', '2', '3', '4',
  27. '5', '6', '7', '8', '9', '0' };
  28. string result="";
  29. for (int i = 0; i < cnt_of_char; i++)
  30. result += chars[rand()%36];
  31. return result;
  32. }
  33.  
  34.  
  35. void per(vector<int> &a, vector<int> &b, int i, int &cnt){
  36. if (i == a.size())
  37. cnt++;
  38. else{
  39. for (int j = 0; j<a.size(); j++){
  40. if (b[j] == 0){
  41. b[j] = 1;
  42. a[i] = j + 1;
  43. per(a, b, i + 1, cnt);
  44. b[j] = 0;
  45. }
  46. }
  47. }
  48. }
  49.  
  50. /*
  51. *
  52. */
  53. int main() {
  54. srand(time(0));
  55. int n, cnt = 0;
  56. time_t start_time, finish_time;
  57.  
  58. cout << "Enter count of elements";
  59. cin >> n;
  60. vector<int> v(n), b(n);
  61. generate_int_array(v, n);
  62.  
  63. //INT STL NEXT_PERMUTATION
  64. sort(v.begin(), v.end());
  65. start_time = time(NULL);
  66. do{
  67. cnt++;
  68. //print(v);
  69. } while (next_permutation(v.begin(), v.end()));
  70. finish_time = time(NULL);
  71. cout << "\nTime of STL next_permutation: " << finish_time - start_time << ". Cnt of STL permutation: " << cnt;
  72. cout << endl;
  73.  
  74.  
  75. //INT MY PERMUTATION
  76. start_time = time(NULL);
  77. cnt = 0;
  78. per(v, b, 0, cnt);
  79. finish_time = time(NULL);
  80. cout << "\nTime of my permutation: " << finish_time - start_time << ". Cnt of MY permutation: " << cnt << endl;
  81.  
  82.  
  83. //STRING STL NEXT_PERMUTATION
  84. cnt = 0;
  85. vector<string> sv(n);
  86. for (int i = 0; i < sv.size(); i++)
  87. sv[i] = generate_string(5);
  88. sort(sv.begin(), sv.end());
  89. start_time = time(NULL);
  90. do{
  91. cnt++;
  92. } while (next_permutation(sv.begin(), sv.end()));
  93. finish_time = time(NULL);
  94. cout << "\nTime of STRING STL next_permutation: " << finish_time - start_time << ". Cnt of STRING STL permutation: " << cnt;
  95. cout << endl;
  96.  
  97.  
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement