hihoCoder - 1152 - Lucky Substrings

hihoCoder 1152 : Lucky Substrings

时间限制: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


题意:给你一个字符串,找出这个串的连续子串,输出那些子串中不同字母数刚好也是斐波那契数的(一定要注意是不同字母数,刚开始没注意到这一点WA了好多次)


思路:用strsub()函数,一次划分出每次子串,判断每个子串是否合法。如果合法,将其插入到set集合里面,这样可以直接去掉重复的子串,输出集合set里面的内容时,是按顺序已经排好的。


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

int n[110];

void fib(){             //先标记好110以内的斐波那契数,后面用 
    memset(n, 0, sizeof(n));
    n[0] = n[1] = 1;
    int i = 2, a = 1, b = 2;
    while(b < 110){
        n[b] = 1;
        int t = a+b;
        a = b;
        b = t;
    }
}

int main() {
    fib();
    string str;
    int a[26];
    while(cin >> str){
        string c;
        int len = str.size();
        for(int i = 0; i < len; i++){
            for(int j = 1; i+j <= len; j++){
                c = str.substr(i, j);
                int leng = c.size();
                memset(a, 0, sizeof(a));

                for(int k = 0; k < leng; k++)        
                    a[c[k]-97] = 1;     //每找到一个子串,就用数组a标记哪些字母出现过

                int co = 0;
                for(int k = 0; k < 26; k++){   //用co记录字串中出现的字母数 
                    if(a[k])
                        co++;
                }
                if(n[co])   //判断出现的字母数是不是斐波那契数 
                    t.insert(c);
            }
        }
        set<string>::iterator i = t.begin();
        while(i!=t.end()){
            cout << *i << endl;
            i++;
        }
    }
    return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值