fork(1) download
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<unordered_map>
  4. #include<array>
  5. #include<list>
  6.  
  7. template < typename T1 >
  8. std::unordered_map< int, int > count_frequency( T1 first, T1 last )
  9. {
  10. std::unordered_map< int, int > temp_unordered_map;
  11. auto temp_unordered_map_end = std::end( temp_unordered_map );
  12. while( first != last )
  13. {
  14. auto it_temp_unordered_map = temp_unordered_map.find( *first );
  15. if( it_temp_unordered_map == temp_unordered_map_end )
  16. {
  17. temp_unordered_map.emplace( *first, 1 );
  18. }
  19. else
  20. {
  21. ++( it_temp_unordered_map->second );
  22. }
  23. ++first;
  24. }
  25. return temp_unordered_map;
  26. }
  27.  
  28. template < typename T1, typename T2 >
  29. bool is_permutation( const T1 first1, const T1 last1, const T2 first2, const T2 last2 )
  30. {
  31. if( std::distance( first1, last1 ) != std::distance( first2, last2 ) )
  32. {
  33. return false;
  34. }
  35. std::unordered_map< int, int > first_map = count_frequency( first1, last1 );
  36. std::unordered_map< int, int > second_map = count_frequency( first2, last2 );
  37.  
  38. std::pair<std::unordered_map< int, int >::iterator,std::unordered_map< int, int >::iterator > myPair=
  39. std::mismatch( std::begin( first_map ), std::end( first_map ), std::begin( second_map ),
  40. []( std::pair< const int, int >& seed1, std::pair< const int, int > & seed2)
  41. { return seed1.second == seed2.second; }
  42. );
  43.  
  44. return myPair.first == first_map.end() && myPair.second == second_map.end();
  45. }
  46.  
  47. int main()
  48. {
  49. const std::array< int, 5> array1 { { 1, 3, 2, 4, 5 } };
  50. const std::array<int,4> array2 { { 1, 2, 4, 3 } };
  51.  
  52. if( ::is_permutation( std::begin( array1 ), std::end( array1 ), std::begin( array2 ), std::end( array2 ) ) )
  53. {
  54. std::cout<< " Elements are permutation of each other\n";
  55. }
  56. else
  57. {
  58. std::cout<< " Elements are not permutation of each other\n";
  59. }
  60.  
  61. const std::vector< int > vec1 { { 1, 3, 2, 4 } };
  62. const std::vector< int > vec2 { { 1, 2, 4, 3 } };
  63.  
  64. if( ::is_permutation( std::begin( vec1 ), std::end( vec1 ), std::begin( vec2 ), std::end( vec2 ) ) )
  65. {
  66. std::cout<< " Elements are permutation of each other\n";
  67. }
  68. else
  69. {
  70. std::cout<< " Elements are not permutation of each other\n";
  71. }
  72.  
  73. const std::list< int > list1 { { 1, 3, 2, 4 } };
  74. const std::list< int > list2 { { 1, 2, 4, 3 } };
  75.  
  76. if( ::is_permutation( std::begin( list1 ), std::end( list1 ), std::begin( list2 ), std::end( list2 ) ) )
  77. {
  78. std::cout<< " Elements are permutation of each other\n";
  79. }
  80. else
  81. {
  82. std::cout<< " Elements are not permutation of each other\n";
  83. }
  84. return 0;
  85. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
 Elements are not permutation of each other
 Elements are permutation of each other
 Elements are permutation of each other