0

我到目前为止是这样的:

#include <iostream>
#include <algorithm>

using namespace std;

int main(){

    string genePool[16] = {"aa", "ab", "ac", "ad", "ba", "bb", "bc", "bd", "ca", "cb", 
                       "cc", "cd", "da", "db", "dc", "dd"};

    string coco, code, deco, dede;

    int total = 0;

    for (int i = 0; i < 16; i++){
        coco = genePool[i];
        for (int j = 0; j < 15; j++){
            code = genePool[j];
                 for (int k = 0; k < 14; k++){
                 code = genePool[k];
                      for (int l = 0; l < 13; l++){
                      code = genePool[l];

                      size_t a = count(coco.begin(), coco.end(), 'a') +
                                 count(code.begin(), code.end(), 'a') +
                                 count(deco.begin(), deco.end(), 'a') +
                                 count(dede.begin(), dede.end(), 'a');
                      size_t b = count(coco.begin(), coco.end(), 'b') +
                                 count(code.begin(), code.end(), 'b') +
                                 count(deco.begin(), deco.end(), 'b') +
                                 count(dede.begin(), dede.end(), 'b');
                      size_t c = count(coco.begin(), coco.end(), 'c') +
                                 count(code.begin(), code.end(), 'c') +
                                 count(deco.begin(), deco.end(), 'c') +
                                 count(dede.begin(), dede.end(), 'c');
                      size_t d = count(coco.begin(), coco.end(), 'd') +
                                 count(code.begin(), code.end(), 'd') +
                                 count(deco.begin(), deco.end(), 'd') +
                                 count(dede.begin(), dede.end(), 'd');

                        if (a = 2 || b = 2 || and c = 2 || d = 2){
                           total++;
                           cout << total << ") "coco << "," << code << "," << deco 
                                << "," << dede << endl;
                        }
                      }    
                }
        }
    }
    return 0;
}

自从我用 C++ 做任何事情以来已经有一段时间了,即使那时我也没有走得太远,所以请原谅任何令人反感的明显错误。每当有人指出更好时,我都会尽心尽责地做得更好:)

4

3 回答 3

1

那么,您想要一个 8 个字符的序列(4 个点 x 每个点 2 个字符),每个 4 个字符包含 2 个实例?

听起来您实际上想要 的所有可能排列"aabbccdd",因为您选择了所有 8 个字符。

因此,这将打印每个排列:

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

void print_permutations(const char *source)
{
    // current permutation
    std::string s(source);
    // this must start off sorted
    std::sort(s.begin(), s.end());
    do {
        std::cout << s << '\n';
    } while (std::next_permutation(s.begin(), s.end()));
}

// original gene pool
const char *pool = "aabbccdd";

int main()
{
    print_permutations(pool);
}

这基本上就是这里的例子。

如果这不是您正在寻找的,也许您可​​以显示一些示例输出?

于 2013-01-18T08:01:04.157 回答
0

我会采用图表方法。构造一个图,其中 geenpool 的每个元素都由一个节点表示,并且每个可能的配对都与一条边。

有点减少可能看起来像这样。

\#include "iostream"

\#include "string"

\#include "algorithm"

\#include "map"

using namespace std;

using namespace std;

int main(){

string genePool[16] = {"aa", "ab", "ac", "ad", "ba", "bb", "bc", "bd", "ca", "cb",
                   "cc", "cd", "da", "db", "dc", "dd"};


map< string,map< string,bool > * > Graph;

for(int i=0;i < 16;++i){

Graph[genePool[i]]= new map< string,bool>;

    for(int j=0;j < 16;++j){

    string combined = genePool[i]+genePool[j];

    unsigned found;

    int count_a =count(combined.begin(),combined.end(),'a');

    int count_b =count(combined.begin(),combined.end(),'b');

    int count_c =count(combined.begin(),combined.end(),'c');

    int count_d =count(combined.begin(),combined.end(),'d');

    if( (count_a == 2 ||count_a ==0)   && (count_b == 2 ||count_b ==0)  && (count_c == 2             ||count_c ==0) && (count_d == 2 ||count_d ==0))

    (*Graph[genePool[i]])[genePool[j]] = true;

    }

}

map<string,map<string,bool>* >::iterator it1 =Graph.begin();

while(it1 != Graph.end() ){

    map<string,bool>::iterator it2 =it1->second->begin();

    while(it2 != it1->second->end()){

        cout <<it1->first<<it2->first<<endl;

        ++it2;

    }

   ++it1;

}


}
于 2013-01-18T10:30:57.950 回答
0

似乎您想创建长度为 4 的序列,从 4 个字母中选择 2 个,每个字母包含两次?

我的(相当粗鲁的)方法是这样的:它从头到尾、一个字符一个字符地添加到一个序列中,并在每个步骤中根据您的规则缩小可能的解决方案。

这将创建类似:

aabb, abba, baba, abab an so on

您可以取消注释某些行以仅允许原子对,这是在前两个字母之后完成的,以相应地添加第二对。(见代码中的注释)

aabb, abab, baba, bbaa ....

这里有一些代码:

#include <vector>
using namespace std,

// .....

string letters = "abcd", tmpStr;
string wrkString; // string to hold sequence during creation
<vector>string sequences; // vector to hold all valid sequences

for(int first=0;i<letters.length();first++){ // every char as first char
    wrkString = letters.at(first);
    for(int  second=0;i<letters.length();second++){ // every char as second char
        wrkString += letters.at(second);
        // got first 2 now differentiate
        if(first == second) // having aa
            letters.erase(first,1); // if same twice, erase from possible elements
        else{ // having ab
            tmpStr = letters;  // if different only this two remain possible solutions
            letters = tmpStr.at(first) + tmpStr.at(second);   

            // uncomment here to add this pair again in same order (ab ab)
            // wrkString += wrkString;
            // sequences.push_back(wrkString);  
            // letters = '';        
        }
        for(int third=0;i<letters.length();third++){ // having aa or ab or ba or bb or ac ...
            wrkString += letters.at(third);
            if(wrkString.find(letters.at(third))==1) // having aac
               letters = letters.at(third);  // if included it has to be added once more
            else // having acc 
               letters -= letters.at(third); // if included twice, remove from possible elements

            for(int fourth=0;i<letters.length();fourth++){ // here allways 1 letter left
                wrkString += letters.at(fourth);
                sequences.push_back(wrkString);    // adding copy of sequence to vector
            }
        }
    }
    letters = "abcd"; // reset possible elements for next iteration
}
于 2013-01-18T07:49:29.000 回答