问题描述:手链样式
小明有3颗红珊瑚,4颗白珊瑚,5颗黄玛瑙。
他想用它们串成一圈作为手链,送给女朋友。
现在小明想知道:如果考虑手链可以随意转动或翻转,一共可以有多少不同的组合样式呢?
参考答案:1170
分析
可以随意转动表面这个手链是一个圈圈,是一个圆,旋转的情况相当于同一种情况。
可以随意翻转,排除一些对称的情况??
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
vector<string> v; //存储已经找出的情况
int sum = 0;
int main(){
string str = "aaabbbbccccc";
do{
vector<string>::iterator it;
for(it=v.begin(); it != v.end(); it++){
if((*it).find(str, 0) != string::npos){ //如果在已得的里面能找到,就判断下一个
// 如果找到了就会跳出for循环
break;
}
}
// 如果在存储的容器v里面已经找到了,就会跳出for循环,就会没有到v.end(),就没有到容器末尾
//说明这种情况已经在容器里了,这次do while循环都不用在进行了
if( it != v.end() ) continue;
// 因为可以随意转动
string str2 = str + str;
v.push_back(str2);
// 因为可以翻转
reverse(str2.begin(), str2.end()); //需要algorithm头文件
v.push_back(str2);
sum ++;
}while(next_permutation(str.begin(), str.end()));
cout << sum ;
}
缕清思路参考链接:https://www.cnblogs.com/lemonbiscuit/p/7776008.html
代码解题参考链接:https://blog.csdn.net/jinglelia/article/details/79344537