粗い全配列アルゴリズム


/*
This is a free Program, You can modify or redistribute it under the terms of GNU
*Description:       ,       ,
			       abc,  abc,bac,bca,acb,cab,cba 6   
*Language: C++
*Development Environment: VC6.0
*Author: Wangzhicheng
*E-mail: [email protected]
*Date: 2012/10/7
*/

#include <iostream>
#include <string>
#include <set>
#include <algorithm>

using namespace std;

string s; //       
int len;   //      
int cnt=0;  //        
const int max=10;
set<string>str_array; //        

/*
                    
             
@a,@b:        
*/
inline static void swapChar(char &a,char &b) {
	a=a^b;
	b=b^a;
	a=a^b;
}

/*
@str:      
                     ,           
                  ,     
*/
void _FullArrange(string str) {
	size_t i,j;
	for(i=0;i<str.size()-1;i++) {
		for(j=i+1;j<str.size();j++) {
			if(str[i]!=str[j]) {
				swapChar(str[i],str[j]);
				if(str_array.find(str)==str_array.end()) str_array.insert(str);
			}
		}
	}
}

/*
        ,                ,           
  _FullArrange()       
*/
void FullArrange() {
	str_array.insert(s);
	set<string>::iterator it;
	for(it=str_array.begin();it!=str_array.end();it++) {
		_FullArrange(*it);
	}
	copy(str_array.begin(),str_array.end(),ostream_iterator<string>(cout,"
")); cnt=str_array.size(); cout<<" "<<cnt<<" "<<endl; } /* max, , set */ void main() { cout<<" :"; cin>>s; len=s.length(); if(len>=max || len<=0) { cerr<<" , !"<<endl; return; } FullArrange(); }