配列要素の全配列アルゴリズムを繰り返し、全配列アルゴリズム、サブセットの解を求めるアルゴリズム、任意の配列アルゴリズムを実行します.

3583 ワード

出力文字配列の全配列は、再帰的な方式を利用するのが簡単です.配列の繰り返し全配列と全配列の違いは主に同じです.
出力文字配列のすべての配列は、完全な配列、単一の要素、複数の要素の配列を含み、これはやや複雑であり、良い考えは、配列を最初にサブセットの抽出を行い、各サブセットを完全に配置することである.サブセットの取得には二つの道があります.
最初の循環の方法は、配列などの大きさのバイナリ数(数学では非空セットの個数=(^size)-1)を利用することで、バイナリ数のうち1の位置はサブセットの要素を表します.
第二の再帰的な方法は、主に再帰的な思想を利用して、行列などの大きさのすべての可能な(0,1)シーケンスを出力し、後続の考え方は上記の循環の方法と同じである.
#include 
#include 
#include 

using namespace std;

//     
void mul_quanpailie(vector* s, int position, vector* result){
    //      ,result             , s   
    if(position == int(s->size())){
        for(auto i = result->begin();i!=result->end();i++){
            cout << *i << " ";
        }
        cout << endl;
    }
    else{
        //result      ,    s      
        for(auto it = s->begin(); it != s->end(); it++){
//            (*result)[position] = *it;
            result->push_back(*it);
            mul_quanpailie(s,position+1,result);
            result->pop_back();
        }
    }
}

//     
void quanpailie(vector* s, int position){
    //      
    if(position == int(s->size())){
        for(auto it = s->begin();it!=s->end();it++){
            cout << *it << " ";
        }
        cout << endl;
    }
    else{
        //     
        for(auto i = position;isize());i++){
            char temp = (*s)[i];
            (*s)[i] = (*s)[position];
            (*s)[position] = temp;
            quanpailie(s,position+1);
            temp = (*s)[i];
            (*s)[i] = (*s)[position];
            (*s)[position] = temp;
        }
    }
}

//     ,    
void print_alls(vector* s){
    int size = s->size();
    int t = 1<* temp = new vector;
        for (int j = 0; j <= size; j++)
        {
            //   j   ,     i     1
            if ((1 << j)&i){
//                cout << (*s)[j] << " ";
                temp->push_back((*s)[j]);
            }
        }
        //       temp 
        if(temp!=NULL){
            quanpailie(temp,0);
        }
        delete temp;
        temp = NULL;
    }
}

//     ,    
void print_alls_DG(vector* s, int position, vector* cur){
    if(position == int(s->size())){
        int i = 0;
        vector* temp = new vector;
        while(ipush_back((*s)[i]);
            }
            i++;
        }
        if(temp->size()>0){
            quanpailie(temp,0);
        }
        delete temp;
        temp = NULL;
    }
    else{
        //       (0,1)   cur ,        
        for(int i = 0; i < 2; i++){
            (*cur)[position] = i;
            print_alls_DG(s,position+1,cur);
        }
    }
}


int main(){
    vector* s = new vector;
    *s = {'a','b','c'};
    cout << "xunhuan+quanpailie" << endl;
    print_alls(s);
    int position = 0;
    vector* result = new vector;
    result->resize(s->size());
    cout << "digui+quanpailie" << endl;
    print_alls_DG(s,position,result);
    vector* result1 = new vector;
    cout << "quanpailie" << endl;
    quanpailie(s,0);
    cout << "chongfuquanpailie" << endl;
    mul_quanpailie(s,0,result1);
    return 0;
}