码迷,mamicode.com
首页 > 其他好文 > 详细

16年蓝桥杯第七题_(string)

时间:2017-03-01 22:49:22      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:style   eve   pre   str   return   组合   news   algo   new   


手链样式

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

请你提交该整数。不要填写任何多余的内容或说明性的文字。


 

 一开始以为是搜索,发现不行,想了一会儿没有思路,看了题解。。。用了一个比较慢,但是想法简单的方法。

主要用了一些stl的函数,

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

vector<string> strs;

int main()
{
    int sum=0;
    string str("aaabbbbccccc");
    do
    {
        vector<string>::iterator it;
        for(it=strs.begin();it!=strs.end();it++)
        {
            if((*it).find(str,0)!=string::npos)   //string的find函数
                break;
        }
        if(it!=strs.end())
            continue;
        sum++;
        string newstr=str+str;  //转动
        strs.push_back(newstr);
        reverse(newstr.begin(),newstr.end());   //翻动
        strs.push_back(newstr);
    }while(next_permutation(str.begin(),str.end()));   //next_permutation函数
    cout<<sum<<endl;
    return 0;
}

 

16年蓝桥杯第七题_(string)

标签:style   eve   pre   str   return   组合   news   algo   new   

原文地址:http://www.cnblogs.com/jasonlixuetao/p/6486461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!