题目描述
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.
示例1
输入
复制
aabcd
输出
复制
a
aa
aab
aabc
ab
abc
b
bc
bcd
c
cd
d
题意:若一个串的某个子串中元素集合的个数是一个斐波那契数,则它就是幸运串,以字典序输出一个串中的所有幸运串。
思路:数据温暖,set + 暴力 + 预处理数值小于26的斐波那契表。
#include<bits/stdc++.h>
using namespace std;
set<int> jj;
void init()
{
vector<int> a;
a.push_back(1);
a.push_back(1);
jj.insert(1);
int i = 2;
while(a[i-1] + a[i-2] <= 26)
{
int doll = a[i-1] + a[i-2];
jj.insert(doll);
a.push_back(doll);
++i;
}
}
int main()
{
char s[110];
set<string> v;
gets(s);
init();
for(int i = 0 ; s[i] ; ++ i)
{
string ss = "";
set<char> SET;
ss += s[i];
v.insert(ss);
SET.insert(s[i]);
for(int j = i + 1 ; s[j] ; ++ j)
{
ss += s[j];
SET.insert(s[j]);
if(jj.count(SET.size()))v.insert(ss);
}
}
set<string> :: iterator it = v.begin();
while(it != v.end())
{
cout << *it << endl;
it++;
}
}