lucky string

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011386173/article/details/50757572

时间限制:1秒 空间限制:32768K

题目描述

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.(lucky字符串的特征是字符串中字符种类数是斐波拉契数列中的数字,例如:aa,字符种类是1; abc 字符种类是3)
输入描述:
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

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>

std::string result[10000];
char Fibonacci[11] = {1,1,2,3,5,8,13,21,34,55,89};
int vis[26] = {0};
char str[110];
int main()
{
    //freopen("lucky_string.out" , "w" , stdout);
    while(gets(str) != NULL)
    {
        int pointer = 0;
        int len = strlen(str);
        for(int i = 0; i < len ; i++)
        {
            for(int j = i ; j < len ; j++)
            {
                 int _count = 0;
                 memset(vis , 0 ,sizeof(int)*26);
                 for(int k = i ; k <= j ; k++)
                 {
                     vis[str[k] - 'a'] = 1;
                 }
                 for(int m = 0 ; m < 26 ; m++)
                 {
                     if(vis[m])
                        _count++;
                 }
                 for(int p = 0 ; p < 11 ; p++)
                 {
                     if(_count == Fibonacci[p])
                     {
                         result[pointer++] = std::string(str+i,j-i+1);
                         break;
                     }
                 }
            }
        }
        std::sort(result , result+pointer);//将子串按字典序排列
        for(int q = 0 ; q < pointer ; q++)
        {
            if(q == 0 || result[q] != result[q-1])
                puts(result[q].c_str());
        }
    }
    //fclose(stdout);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u011386173/article/details/50757572