蓝桥杯_手链样式(排列)

来源:互联网 发布:苹果6支持4g网络吗 编辑:程序博客网 时间:2024/04/29 06:37

题目:

手链样式

小明有3颗红珊瑚,4颗白珊瑚,5颗黄玛瑙。
他想用它们串成一圈作为手链,送给女朋友。
现在小明想知道:如果考虑手链可以随意转动或翻转,一共可以有多少不同的组合样式呢?

思路:

全排列,对每种情况转动,翻转检测;

代码:

#include <cstdio>#include <iostream>#include <string>#include <algorithm>#include <vector>using namespace std;vector<string> uni;int main(){    string s("aaabbbbccccc");    int tot = 1, flag = 0;    uni.push_back(s);    while(next_permutation(s.begin(), s.end()))    {        flag = 0;        // 转动        string s2  = s + s;        vector<string>::iterator it = uni.begin();        for(; it != uni.end(); it++)        {            if(s2.find(*it) != string::npos) { flag = 1; break; }        }        // 翻转        if(!flag)        {            reverse(s2.begin(), s2.end());            for(it = uni.begin(); it != uni.end(); it++)            {                if(s2.find(*it) != string::npos) { flag = 1; break; }            }        }        if(!flag) { tot++; uni.push_back(s); }    }    cout << tot;    return 0;}
0 0
原创粉丝点击