Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <algorithm>
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. int check(char *st){
  10.     int n = strlen(st);
  11. //    std::cout<<n;
  12.     for (int i = 0; i<=n/2; i++){
  13.         if (st[i] != st[n-1-i]){
  14.             return 0;
  15.         }
  16.     }
  17.     return 1;
  18. }
  19.  
  20. char * words[8] = {"kniga", "na", "upala", "lapu", "a", "stol", "roza", "azora"};
  21.  
  22. void swap( int a[], int i, int j ) // swap elements a[i], a[j]
  23. {
  24.     int temp = a[i] ;
  25.     a[i] = a[j] ;
  26.     a[j] = temp ;
  27. }
  28.  
  29. void print_array( const int a[], int n )
  30. {
  31.     static int cnt = 0 ;
  32.     char *res = (char*)calloc(128, sizeof(char));
  33.     int * resint=(int*)calloc(100, sizeof(int));
  34.     for( int i = 0 ; i < n ; ++i ) {
  35.         strcat(res,words[a[i]]);
  36. //        std::cout << res<< ' ';
  37.     }
  38. //    for( int i = 0 ; i < n ; ++i ) std::cout<<a[i]<<' ';
  39.     if (check(res)) {
  40.         for (int i = 0; i < n; i++) std::cout<<a[i]<<' ';
  41.         std::cout<<std::endl;
  42.         std::cout << std::setw(3) << ++cnt << ". " ;
  43.         std::cout << res;
  44.         std::cout << '\n';
  45.     }
  46. }
  47.  
  48. // array 'a' of size 'n', permute range starting at position 'from'
  49. void permute( int a[], int n, int from = 0 )
  50. {
  51.     // if the entire range has been permuted, print the permutation
  52.     if( from == n ) return print_array( a, n ) ;
  53.  
  54.     // else
  55.     for( int i = from ; i < n ; ++i ) // repeat: for each element in the range to permute
  56.     {
  57.         swap( a, from, i ) ; // make this element the head (at position from)
  58.         permute( a, n, from+1 ) ; // form all permutations of the tail (starting at from+1)
  59.         swap( a, from, i ) ; // restore the original positions
  60.     }
  61. }
  62.  
  63.  
  64. void comb(int N, int K)
  65. {
  66.     std::string bitmask(K, 1); // K leading 1's
  67.     bitmask.resize(N, 0); // N-K trailing 0's
  68.  
  69.     // print integers and permute bitmask
  70.     do {
  71.         int res[K];
  72.         int q = 0;
  73.         for (int i = 0; i < N; ++i) // [0..N-1] integers
  74.         {
  75.             if (bitmask[i]){
  76.                 res[q] = i;
  77.                 q += 1;
  78.             }
  79.             //std::cout << " " << i;
  80.         }
  81.         q = 0;
  82.         permute(res,K);
  83. //        std::cout << std::endl;
  84.     } while (std::prev_permutation(bitmask.begin(), bitmask.end()));
  85. }
  86.  
  87. int main(){
  88. //    vector <string> words = {'книга', 'на', 'упала', 'лапу', 'а', 'стол', 'роза', 'азора'};
  89. //    vector <string> res;
  90.  
  91. //    int n = words.size();
  92.     for (int i = 1; i<= 8; i++) comb(8,i);
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement