#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 题目大意:给定一个只包含小写字母的字符串S。对于S的任意一个非空子串,若其包含的不同字母个数为fibonacci数列中的数,
则我们认为这个子串为幸运的。请找出S的所有幸运的子串。不要重复输出。

解题思路

一个简单的解题思路是直接枚举S的所有子串,并对其不同字母个数进行统计。

S均由小写字母组成,因此其包含的不同字母个数最多为26个。而在26以内且属于fibonacci数列的数为1,2,3,5,8,13,21。因此只有当子串中不同字母的个数为1,2,3,5,8,13,21时,该子串才是幸运的。

接下来即是如何统计一个子串的不同字母个数,下面给出一种比较朴素的方法:

isLucky(subString):
alphabet[] = false
count = 0
For c in subString
If not alphabet[c] Then
alphabet[c] = true
count = count + 1
End If
End For
Return (count is Fibonaccid number)

S的最大长度为 N = 100,该朴素算法的时间复杂度为O(N^3),是可以通过所有数据的。

同时,我们可以通过一个小的优化,将算法的时间复杂度减少的O(N^2)。

在已经知道S[i..j]字母个数的情况下,我们可以直接推导出S[i..j+1]的不同字母个数。

首先我们需要改进isLucky函数:

alphabet[] = false
count = 0
isLucky(c):
If not alphabet[c] Then
alphabet[c] = true
count = count + 1
End If
Return (count is Fibonaccid number)

这里我们把alphabetcount从函数中抽取出来,作为了全局变量。同时,isLucky的参数变为单个字符,每次将新增的S[j+1]加入统计中。

下面是函数的主体部分:

For i = 0 .. len - 1
alphabet[] = false
count = 0
For j = i .. len - 1
// 此时isLucky返回的是S[i..j]的不同字母数量是否满足条件
If isLucky(S[j]) Then
ansList.push(S[i..j])
End If
End For
End For

最后只需要将ansList所保存的子串进行排序去重后输出,即可顺利通过该题目。


 #include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <set>
using namespace std;
string s; bool IsFibonaccidNum(int n){
return (n == || n == || n == || n == || n == || n == || n == );
} bool isLucky(int i, int j){
int alphabet[] = {};
//memset(alphabet, 0, sizeof(alphabet));
int count = ;
for(int k = i; k <= j; k++){
if(alphabet[s[k] - 'a'] == ){
alphabet[s[k] - 'a'] = ;
count++;
}
}
return (IsFibonaccidNum(count));
} int main(){
set<string> set1;
cin >> s;
int len = s.length();
for(int i = ; i < len; i++){
for(int j = i; j < len; j++){
if(isLucky(i, j)){
string str = s.substr(i, j - i + );
set1.insert(str);
}
}
} for(set<string>::iterator it = set1.begin(); it != set1.end(); it++){
cout << *it << endl;
}
//system("pause");
return ;
}

 #include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <set>
using namespace std;
int alphabet[] = {}, cnt; bool IsFibonaccidNum(int n){
return (n == || n == || n == || n == || n == || n == || n == );
} bool isLucky(char c){
if(alphabet[c - 'a'] == ){
alphabet[c - 'a'] = ;
cnt++;
}
return (IsFibonaccidNum(cnt));
} int main(){
set<string> set1;
string s;
cin >> s;
int len = s.length();
for(int i = ; i < len; i++){
memset(alphabet, , sizeof(alphabet));
cnt = ;
for(int j = i; j < len; j++){
if(isLucky(s[j])){
string str = s.substr(i, j - i + );
set1.insert(str);
}
}
} for(set<string>::iterator it = set1.begin(); it != set1.end(); it++){
cout << *it << endl;
}
//system("pause");
return ;
}


hihocoder 1152 Lucky Substrings的更多相关文章

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

    #1152 : Lucky Substrings时间限制:10000ms单点时限:1000ms内存限制:256MB描述A string s is LUCKY if and only if the nu ...

  2. 微软2016校园招聘在线笔试第二场 题目1 : Lucky Substrings

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if and only if the number of different ch ...

  3. Lucky Substrings

    而在26以内且属于fibonacci数列的数为1,2,3,5,8,13,21时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if ...

  4. Lucky String

    Lucky String -- 微软笔试 标签(空格分隔): 算法 A string s is LUCKY if and only if the number of different charact ...

  5. 【每天一道算法题】Lucky String

    A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Gi ...

  6. hihoCoder 1426 : What a Ridiculous Election(总统夶选)

    hihoCoder #1426 : What a Ridiculous Election(总统夶选) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - ...

  7. hihoCoder 1432 : JiLi Number(吉利数)

    hihoCoder #1432 : JiLi Number(吉利数) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 Driver Ji l ...

  8. lucky 的 时光助理(2)

    lucky小姐说:昨天晚上他喝醉了,发消息说他想我了,说他后悔了. 我很惊讶. 我问lucky:你们很久都没有联系, 突然说... 你怎么想. 没错,'他'就是lucky的前男友. lucky看着我, ...

  9. lucky 的 时光助理

    2017年的lucky小姐,厌倦了现在的工作,她觉得这些的工作对于她而言不具备挑战性,她在迷茫春节过后该如何选择, 这里是距她走出校门整整一年的时光. lucky小姐从开发走向了实施,目的是想周游这个 ...

随机推荐

  1. CDocument类的UpdateAllViews()成员函数

    (一)UpdateAllViews() 与 Invalidate()的区别 UpdateAllViews()是在DOC/VIEW结构中,当一个视图的数据改变后,通知所有视图作相应的改变,和重画毫无关系 ...

  2. 大数据平台搭建(hadoop+spark)

    大数据平台搭建(hadoop+spark) 一.基本信息 1. 服务器基本信息 主机名 ip地址 安装服务 spark-master 172.16.200.81 jdk.hadoop.spark.sc ...

  3. js 格式化数字

    http://www.jb51.net/article/61585.htm 这篇文章主要介绍了JS实现的4种数字千位符格式化方法分享,本文给出了4种千分位格式化方法并对它们的性能做了比较,需要的朋友可 ...

  4. ubuntu: qemu+gdb 调试linux kernel 学习笔记

    声明: 本笔记内容并非本人原创,90%来自网络资料的整合.同时,由于自己是刚刚接触qemu & gdbserver remote debug,本文也就算不得教程,仅供有缘人参考而已. ---- ...

  5. Java数据结构之线性表(2)

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  6. 【转】REST on Rails指南

    REST on Rails指南1:理解资源 这是来自http://www.softiesonrails.com的REST简明指南. PART I 在理解REST on Rails之前,有必要先思考一下 ...

  7. BZOJ 3930: [CQOI2015]选数 递推

    3930: [CQOI2015]选数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pro ...

  8. ANativeWindow是个什么东西

    公司经常组织一些培训,培训的都是些奇技淫巧.什么设计模式啦,开发策略啦,git啦,repo啦,另外就是培训一些开发流程的东东,例如CMMI啦.可是,却忘记了,程序员终究要归结到三个问题上: 1.解决什 ...

  9. Java生成 Word文档的并打印解决方案

    户要求用程序生成标准的word文档,要能打印,而且不能变形,以前用过很多解决方案,都在客户严格要求下牺牲的无比惨烈. POI读word文档还行,写文档实在不敢恭维,复杂的样式很难控制不提,想象一下一个 ...

  10. 9款风格华丽的jQuery/CSS3插件

    今天向大家分享9款效果相当不错的jQuery/CSS3插件,不多说,直接来看看这些插件吧. 1.jQuery动画下拉菜单Smart Menu 这是一款基于jQuery的动画下拉菜单,子菜单外观比较时尚 ...