hihoCoder - 1152 - Lucky Substrings
2015-08-02 11:08
197 查看
hihoCoder 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样例输出
aaa
aab
aabc
ab
abc
b
bc
bcd
c
cd
d
题意:给你一个字符串,找出这个串的连续子串,输出那些子串中不同字母数刚好也是斐波那契数的(一定要注意是不同字母数,刚开始没注意到这一点WA了好多次)
思路:用strsub()函数,一次划分出每次子串,判断每个子串是否合法。如果合法,将其插入到set集合里面,这样可以直接去掉重复的子串,输出集合set里面的内容时,是按顺序已经排好的。
[code]#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<set> using namespace std; set<string>t; int n[110]; void fib(){ //先标记好110以内的斐波那契数,后面用 memset(n, 0, sizeof(n)); n[0] = n[1] = 1; int i = 2, a = 1, b = 2; while(b < 110){ n[b] = 1; int t = a+b; a = b; b = t; } } int main() { fib(); string str; int a[26]; while(cin >> str){ string c; int len = str.size(); for(int i = 0; i < len; i++){ for(int j = 1; i+j <= len; j++){ c = str.substr(i, j); int leng = c.size(); memset(a, 0, sizeof(a)); for(int k = 0; k < leng; k++) a[c[k]-97] = 1; //每找到一个子串,就用数组a标记哪些字母出现过 int co = 0; for(int k = 0; k < 26; k++){ //用co记录字串中出现的字母数 if(a[k]) co++; } if(n[co]) //判断出现的字母数是不是斐波那契数 t.insert(c); } } set<string>::iterator i = t.begin(); while(i!=t.end()){ cout << *i << endl; i++; } } return 0; }
相关文章推荐
- 机房收费之充值窗体
- iOS Quartz2D - 屏幕截图
- iOS Quartz2D - 给图片加水印
- 常用mac/unix/linux命令
- 重载 vs 重写
- php-面向对象(6)
- acm-UVA11935解题报告
- 学单片机预备知识、如何点亮一个发光管、流水灯程序
- MongoDB 在windows shell环境下的基本操作和命令的使用示例(三)
- 用Model-View-ViewModel构建iOS App
- 虚函数与虚继承
- 计数排序
- HDU 5336 XYZ and Drops(模拟十滴水游戏 BFS啊)
- iOS Quartz2D - 裁剪图片为带边框的圆形图片
- 早期(编译期)优化
- android 中 View的构造函数
- win7应用程序出现乱码 解决方法
- 图层损坏 E/ArcGIS﹕ The map or layer has been destroyed or recycled. 资源未释放
- 第六课,T语言表达式(版本5.0)
- java学习笔记之-构造函数