hihoCoder#1152 (set及substr的使用)

来源:互联网 发布:如何进行店铺数据分析 编辑:程序博客网 时间:2024/06/10 15:53

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Given a string consisting of only lower case letters, output all its lucky non-empty substrings in lexicographical order. Same substrings should be printed once.

输入
A string consisting no more than 100 lower case letters.

输出
Output the lucky substrings in lexicographical order, one per line. Same substrings should be printed once.

样例输入
aabcd
样例输出
a
aa
aab
aabc
ab
abc
b
bc
bcd
c
cd
d
**这道题主要用到substr和set就可以很好的解决:
str.substr(s,e) 截取str字符串从第s位开始的长度为e的字符串
set容器可以达到去重且按字典序从小到大排序的效果
若要使set容器里面的元素从大到小排列,
则可以用set < int,greater> M;
而默认情况也可写成set< int,less> M**
代码如下:

#include<bits/stdc++.h>using namespace std;set<string>st;map<char,int>mp;int main(){    string s;    cin>>s;    int arr[7]={1,2,3,5,8,13,21};    int i,j;    st.clear();    for(i=0;i<s.size();i++)    {        mp.clear();        int cnt=0;        for(j=i;j<s.size();j++)        {            if(mp[s[j]]==0)                cnt++;            mp[s[j]]++;            for(int k=0;k<7;k++)                if(arr[k]==cnt)                {                    string str=s.substr(i,j-i+1);                    //cout<<123<<str<<endl;                    st.insert(str);                    break;                }        }    }    for(set<string>::iterator it=st.begin();it!=st.end();it++)    {        //cout<<123<<endl;        cout<<*it<<endl;    }    return 0;}
原创粉丝点击