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;
}

  

时间: 2024-08-28 12:28:34

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

hihocoder#1152 : Lucky Substrings

字串处理操作,用到了stl的排序和去重 #include <cstdio> #include <cstring> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; int fib[]={1,2,3,5,8,13,21,34,55,89}; vector<string>sub; i

10天精通Sass 之 处理字符串与数字的函数

Sass的函数简介 Sass中自备了一系列的功能函数,包括: - 字符串函数 - 数字函数 - 列表函数 - 颜色函数 - Introspection函数 - 三元函数 除了Sass中已提供的函数,我们还可以根据自己的需求定义函数,称为自定义函数. 字符串函数 * unquote($string) * :删除字符串中的引号 * quote($string) * :给字符串加引号 unquote()函数 用来删除字符串的引号,如果这个字符串没有引号,则返回原始字符串. .test1 { conte

vb 字符串和数字相互转换函数

VB中的字符串函数比较多,也比较方便,就不一一介绍了.本文主要对字符串相关的转换函数做一些小结.字符串转换的函数主要有: Str()和Val()用于字符串和数字的相互转换; Chr()和Asc()用于字符串和AscII码的相互转换; Chrw()和Ascw()用于Unicode码和中文的相互转换; Format()函数用途十分广泛的一个函数,功能十分强大. 在这些函数中前两对和Format()函数是我们经常用到的,这里只给出前两对的几个简单例子: (1) MyString = Str(-459.

字符串 映射相应的 函数 字符串驱动技术—— MethodAddress , MethodName , ObjectInvoke

http://blog.csdn.net/qustdong/article/details/7267258 字符串驱动技术—— MethodAddress , MethodName , ObjectInvoke 标签: delphiintegerfunctionobjectsoapclass 2012-02-17 11:46 1139人阅读 评论(0) 收藏 举报  分类: Delphi(24)  首先看一段Delphi帮助中的介绍(After Delphi 6 ): Returns the a

数组、字符串在指针和函数中的一些经验总结

这篇主要是记录一下今天下午的坑和教训. 1.对于多维数组,在函数原型声明的形式是 void funct(int arr[ ][10]) 或 void funct (int (*arrp)[10]),后面的数字10必须要指定.不能以 void funct( int arr[ ] [ ]) 或 void funct (int **arr)等其他形式来声明带多维数组参数的函数原型. 而数组在首次声明时必须要指定维数,如 int arr[2]; 不能是 int arr[ ].除非是后面带初始化的数,如

Js中常用的字符串,数组,函数扩展

由于最近辞职在家,自己的时间相对多一点.所以就根据prototytpeJS的API,结合自己正在看的司徒大神的<javascript框架设计>,整理了下Js中常用一些字符串,数组,函数扩展,一来可以练练手,二来也锻炼下自己的代码能力.由于代码里面的注释自认为已经非常详细,所以就直接贴代码了. 1. 字符串扩展: ;(function() { var method, stringExtends = { /** * 删除字符串开始和结尾的空白 * @returns {string} */ stri

笔试题: 不使用中间变量求const字符串长度,即实现求字符串长度库函数strlen函数

笔试题: 不使用中间变量求const字符串长度,即实现求字符串长度库函数strlen函数. 函数接口声明如下:int my_strlen(const char *p); strlen函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符'\0'. 而返回的长度大小不包括'\0'. #include <stdio.h> #include <assert.h> //使用中间变量 //int my_strlen(const  char *str) //{ //   ass

【51CTO/BBS】请教: SQL里有没有字符串组合Join的函数??

[51CTO/BBS]请教: SQL里有没有字符串组合Join的函数?? 原帖地址:http://bbs.51cto.com/thread-1133863-1.html 问题描述: VB 中有两个非常好用的字符串处理函数: Split(字符串,分隔符)作用:将[字符串]以[分隔符]作为边界,分解成数组. 返回:一个字符串数组. Join(字符数组,分隔符)作用:将[字符数组]中的元素,以[分隔符]作为边界,连接成一个字符串.返回:一个字符串. 请教老师们,SQL里是否有类似的函数? 解决方案:

用什么方法来判断字符串是否存在的函数

首先应该知道 strpos 函数可能返回布尔值 FALSE,但也可能返回一个与 FALSE 等值的非布尔值,例如 0 或者"".我们应使用 === 运算符来测试本函数的返回值. <?php /* 判断字符串是否存在的函数 */ function strexists($haystack, $needle) { return !(strpos($haystack, $needle) === FALSE);//注意这里的"===" } /* Test */ $mys