到目前为止,我得到的是:
#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++做过任何事情了,即使是这样,我也不会走得太远,所以请原谅任何冒犯性的明显错误。只要有人指出我做得更好,我都会乐意做得更好:)
发布于 2013-01-18 16:01:04
那么,您想要一个8个字符的序列(4个spot x 2个字符/ spot),包含2个实例,每个实例4个字符?
听起来您实际上想要"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);
}这实际上就是示例here。
如果这不是您想要的,也许您可以显示一些示例输出?
发布于 2013-01-18 15:49:29
似乎你想创建长度为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-18 18:30:58
我会采用图的方法。构建一个图,其中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;
}
}https://stackoverflow.com/questions/14393520
复制相似问题