Lucky Substrings HihoCoder

来源:互联网 发布:网络搞笑剧有哪些 编辑:程序博客网 时间:2024/06/03 23:42

题目

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.

INPUT

A string consisting no more than 100 lower case letters.

OUTPUT

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

sample input

aabcd

sample output

a
aa
aab
aabc
ab
abc
b
bc
bcd
c
cd
d

使用了c++的字符串处理函数

ac代码

#include <cstdio>#include <cstring>#include <cmath>#include <set>#include <string>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;const int mod = 1e9+7;int a[26];int main(){    set<string> t;    string s,cur;    cin>>s;    int len = s.size();    //j是长度 i是位置    for (int i = 0;i < len; ++i){        for (int j = 1;j+i <= len; ++j){            cur = s.substr(i,j);            int lh = cur.size();            memset(a,0,sizeof(a));            for (int k = 0;k < lh; ++k){                a[cur[k]-'a'] = 1;            }            int cnt = 0;            for (int k = 0;k < 26; ++k){                if (a[k]) cnt++;            }            if (cnt==1||cnt==2||cnt==3||cnt==5||cnt==8||cnt==13||cnt==21){                t.insert(cur);            }        }    }    set<string>::iterator it = t.begin();    while (it!=t.end()){        cout<<*it<<endl;        it++;    }    return 0;}