71 lines
1.5 KiB
C++
71 lines
1.5 KiB
C++
/*
|
|
VIM: let b:cf5build="clang++ -std=c++20 -O2 -pthread -I. {SRC} -o {OUT}"
|
|
VIM: let b:cf5run="{OUT}"
|
|
*/
|
|
#include <iostream>
|
|
#include <exception>
|
|
#include <chrono>
|
|
|
|
void permutate(auto b, auto e)
|
|
{
|
|
auto bt = std::reverse_iterator(e);
|
|
auto et = std::reverse_iterator(b);
|
|
|
|
auto it = bt+1;
|
|
for ( ; it != et && *it >= *(it-1); ++it);
|
|
|
|
for (auto qt = bt, rt = it-1; rt > qt; qt++, rt--){
|
|
std::swap(*qt, *rt);
|
|
}
|
|
|
|
if (it != et){
|
|
auto pt = it-1;
|
|
for( ; pt != bt && *it >= *pt; --pt);
|
|
std::swap(*it, *pt);
|
|
}
|
|
}
|
|
|
|
void test(std::string char_set)
|
|
{
|
|
std::sort(char_set.begin(), char_set.end());
|
|
std::string perm = char_set;
|
|
int i = 0;
|
|
do {
|
|
++i;
|
|
std::cout << perm << '\n';
|
|
permutate(perm.begin(), perm.end());
|
|
} while (perm != char_set);
|
|
|
|
std::cout << i << '\n' << "-------------------------------------\n";
|
|
}
|
|
|
|
int main ( void )
|
|
{try{
|
|
auto begin = std::chrono::high_resolution_clock::now();
|
|
|
|
test( "abcd" );
|
|
test( "aab" );
|
|
test( "aabb" );
|
|
test( "aaabbb" );
|
|
|
|
//......
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> seconds = end - begin;
|
|
std::cout << "Time: " << seconds.count() << std::endl;
|
|
return 0;
|
|
}
|
|
catch ( const std::exception& e )
|
|
{
|
|
std::cerr << std::endl
|
|
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
|
return 2;
|
|
}
|
|
catch ( ... )
|
|
{
|
|
std::cerr << std::endl
|
|
<< "unknown exception." << std::endl;
|
|
return 1;
|
|
}}
|
|
|