hihocoder 1152 Lucky Substrings
#1152 : Lucky Substrings
描述
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[] = falsecount = 0For c in subStringIf not alphabet[c] Thenalphabet[c] = truecount = count + 1End IfEnd ForReturn (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] Thenalphabet[c] = truecount = count + 1End IfReturn (count is Fibonaccid number)
这里我们把
alphabet
和count
从函数中抽取出来,作为了全局变量。同时,isLucky
的参数变为单个字符,每次将新增的S[j+1]加入统计中。下面是函数的主体部分:
For i = 0 .. len - 1alphabet[] = falsecount = 0For j = i .. len - 1// 此时isLucky返回的是S[i..j]的不同字母数量是否满足条件If isLucky(S[j]) ThenansList.push(S[i..j])End IfEnd For End For
最后只需要将
ansList
所保存的子串进行排序去重后输出,即可顺利通过该题目。1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdlib> 5 #include <set> 6 using namespace std; 7 string s; 8 9 bool IsFibonaccidNum(int n){ 10 return (n == 1 || n == 2 || n == 3 || n == 5 || n == 8 || n == 13 || n == 21); 11 } 12 13 bool isLucky(int i, int j){ 14 int alphabet[26] = {0}; 15 //memset(alphabet, 0, sizeof(alphabet)); 16 int count = 0; 17 for(int k = i; k <= j; k++){ 18 if(alphabet[s[k] - 'a'] == 0){ 19 alphabet[s[k] - 'a'] = 1; 20 count++; 21 } 22 } 23 return (IsFibonaccidNum(count)); 24 } 25 26 int main(){ 27 set<string> set1; 28 cin >> s; 29 int len = s.length(); 30 for(int i = 0; i < len; i++){ 31 for(int j = i; j < len; j++){ 32 if(isLucky(i, j)){ 33 string str = s.substr(i, j - i + 1); 34 set1.insert(str); 35 } 36 } 37 } 38 39 for(set<string>::iterator it = set1.begin(); it != set1.end(); it++){ 40 cout << *it << endl; 41 } 42 //system("pause"); 43 return 0; 44 }
1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdlib> 5 #include <set> 6 using namespace std; 7 int alphabet[26] = {0}, cnt; 8 9 bool IsFibonaccidNum(int n){ 10 return (n == 1 || n == 2 || n == 3 || n == 5 || n == 8 || n == 13 || n == 21); 11 } 12 13 bool isLucky(char c){ 14 if(alphabet[c - 'a'] == 0){ 15 alphabet[c - 'a'] = 1; 16 cnt++; 17 } 18 return (IsFibonaccidNum(cnt)); 19 } 20 21 int main(){ 22 set<string> set1; 23 string s; 24 cin >> s; 25 int len = s.length(); 26 for(int i = 0; i < len; i++){ 27 memset(alphabet, 0, sizeof(alphabet)); 28 cnt = 0; 29 for(int j = i; j < len; j++){ 30 if(isLucky(s[j])){ 31 string str = s.substr(i, j - i + 1); 32 set1.insert(str); 33 } 34 } 35 } 36 37 for(set<string>::iterator it = set1.begin(); it != set1.end(); it++){ 38 cout << *it << endl; 39 } 40 //system("pause"); 41 return 0; 42 }
转载于:https://www.cnblogs.com/qinduanyinghua/p/5828450.html
hihocoder 1152 Lucky Substrings相关推荐
- Lucky Substrings
而在26以内且属于fibonacci数列的数为1,2,3,5,8,13,21时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if ...
- LUCKY STRING(微软校招)
题目描述 A string s is LUCKY if and only if the number of different characters in s is a fibonacci numbe ...
- LUCKY STRING
题目描述 A string s is LUCKY if and only if the number of different characters in s is a fibonacci numbe ...
- 2016微软校招笔试题
标题 A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. ...
- 浙大pat | 浙大pat 牛客网PAT顶级(Top Level)练习题 1001
1001 LUCKY STRING 1872 8254 22% 题目描述 A string s is LUCKY if and only if the number of differentchara ...
- 【CodeForces - 122B 】Lucky Substring (字符串,水题)
题干: Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decima ...
- LeetCode刷题记录6——696. Count Binary Substrings(easy)
LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...
- 枚举 + 进制转换 --- hdu 4937 Lucky Number
Lucky Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) T ...
- hihoCoder 网络流四·最小路径覆盖
题面带解释 hihoCoder感觉很好. 网络流的精华就是建图 #include<cstdio> #include<iostream> #include<algorith ...
最新文章
- mysql实战38 | 都说InnoDB好,那还要不要使用Memory引擎?
- C语言作业不足之处,C语言和汇编语言的优缺点分析-控制器/处理器-与非网
- Bootstrap响应式工具类
- linux pam limits.so,Linux 无法本地登录解决方法 报错/lib/security/pam_limits.so
- Linux openwrt 树莓派 香蕉派 嵌入式 usb 声卡 PCM2704 2705 2706 CM108 PCM2704/2705/2706 linux专用声卡 专用usb声卡
- 嵌入式:ARM相关开发工具概述
- bpe分词算法的原理以及在机器翻译中的应用
- 如何检测ip和端口是否连通
- mysql中sum函数使用_MySQL中的SUM函数使用教程_MySQL
- python编程:从入门到实践(持续更新)
- 计算机组成原理实验总结,计算机组成原理实验报告总结归纳.docx
- 计算机电缆传输频率,传输频率
- 泡芙噶的计算机网络(2)-紧张刺激的Wireshark实验
- 痞子衡嵌入式:PCM编码与Waveform音频文件(.wav)格式详解
- IDEA自动生成实体类
- 三生三世十里桃花用计算机怎么弄,三生三世十里桃花ios如何用电脑玩 三生三世十里桃花ios电脑教程...
- 分布式存储架构一-分布式存储概念
- NYOJ--懒省事的小明
- NXP汽车电子芯片路线图和命名规范
- Linux学习——Linux常用文件和目录管理命令(超详细)
热门文章
- 学习linux要会mysql吗_linux 学习 mysql安装到连接
- python 一张图画多条线_Gnuplot.py在一张图上绘制多条线
- egg extend ts_KPL官方给各战队排T次:大王DYG,AG是老2、TS仅K
- ubuntu 禁用透明大页_Linux关于透明大页的使用与禁用介绍
- html中legend设置大小,HTML_如何给 legend 标签设定宽度,我们在做表单的时候经常会使 - phpStudy...
- php的环境怎么配置文件,php环境下所有的配置文件以及作用
- eclipse java调用c 代码吗_linux下通过eclipse开发用java调用c程序的方法
- 基于python的手写数字识别knn_KNN分类算法实现手写数字识别
- java arraylist和list_Java中ArrayList和LinkedList区别
- 计算机导论简答芯片,吉大计算机 - 计算机导论简答题 (2011级)