hihocoder #1152 Lucky Substrings 【字符串处理问题】strsub()函数+set集合去重

#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

题目分析:给你一个字符串,找出这个串的连续子串,但是我们不全部输出,只输出那些子串中不同字母数刚好也是斐波那契数的

子串。举例:子串:a(斐波那契数=1)合法;  aabcd(有4个不同字母,4不是斐波那契数) 不合法,不输出。

算法实现:用c++的strsub()函数,一次划分出每次子串,判断每个子串是否合法。如果合法,将其插入到STL set集合里面,这

样做也省去了去除重复子串的麻烦,输出集合set里面的内容时,是按顺序已经排好的。

代码:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <math.h>

#include <iostream>

#include <string>

#include <set>

#include <algorithm>



using namespace std;



int main()

{

    string s, cur;

    set<string>t;

    cin>>s;

    int len=s.size();

    int i, j, k;

    int a[26];

    for(i=0; i<len; i++)

    {

        for(j=1; i+j<=len; j++)

        {

            cur = s.substr(i, j);

            //cout<<cur<<endl;

            int length=cur.size();

            memset(a,0,sizeof(a));

            for(k=0; k<length; k++)

            {

                a[cur[k]-97]=1;

            }

            int cnt=0;

            for(k=0; k<26; k++){

                if(a[k]>0)

                    cnt++;

            }

            if(cnt==1||cnt==2||cnt==3||cnt==5||cnt==8||cnt==13||cnt==21

               ||cnt==34||cnt==55||cnt==89)

               t.insert(cur);

        }

    }

    set<string>::iterator it=t.begin();

    while(it!=t.end())

    {

        cout<<*it<<endl;

        it++;

    }

    return 0;

}

  

 

你可能感兴趣的:(substring)