hihoCoder#1152 (set及substr的使用)

时间限制: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值