勿忘《暴力解》:总是想着O(n),经常忽略暴力解。substr函数是个好函数
来源:互联网 发布:java实现三级菜单栏 编辑:程序博客网 时间:2024/05/17 08:28
题目描述
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
总结:1.勿忘《暴力解》:总是想着O(n),经常忽略暴力解。2.substr函数 find 函数 是个好函数 充分利用C++ <string>里面的函数 不要自己造轮子。substr 截取子字符串 find 字符串匹配 直接拿来用3. 当涉及到字符串(或其他类型)排序(多字符串排序。不要造轮子)且去重时,可直接利用STL set<string>4. auto 不能用于 set<>???c++ string substr 方法 (截取子字符串)
返回一个从指定位置开始,并具有指定长度的子字符串。
str.substr(startpos, length);
其中 startpos 是起始字符的序号,必选,是所需的子字符串的起始位置。字符串中第一个字符的索引为 0。length 是[从 startpos 开始]取的字符串长度(包括startpos )。 如果 length 为 0 或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到字符串的结尾。如果要取得 str 中序号 m 到 n 之间(不包括n)的子字符串需要用str.substr(m, n-m);
c++ string find 方法 (字符串匹配) 手写KMP算法?
查找函数size_type find(const string& str, size_type pos = 0) const 当前string从pos开始向后查找str第一次出现的位置。
size_type find(const char* s, size_type pos, size_type n) const 当前string从pos开始向后查找“s子串内前n个字符组成字符串”第一次出现的位置。
pos是描述当前string的,而n是描述s的。
代码如下:
#include <iostream>#include <set>#include <string>using namespace std;const int fibo[]={1,2,3,5,8,13,21};int count_str(string s){ int hash[26]={0}; int num=0; for (int i = 0; i < s.size(); ++i) { if(hash[s[i]-'a']==0){ ++num; hash[s[i]-'a']=1; } } return num;}int main(){ int hash[22]={0}; for (int i = 0; i <=6 ; ++i) { hash[fibo[i]]=1; } string str; cin>>str; set<string> setstr; for (int len = 1; len <=str.size() ; ++len) { for (int i = 0; i <=str.size()-len; ++i) { string substr=str.substr(i,len); if(hash[count_str(substr)]==1){ setstr.insert(substr); } } } /* for (auto str:setstr) { cout<<str<<endl; }*/ set<string>::iterator it; for (it = setstr.begin(); it != setstr.end(); it++) cout << *it << endl; return 0;}
0 0
- 勿忘《暴力解》:总是想着O(n),经常忽略暴力解。substr函数是个好函数
- Hduoj2069 【母函数】【暴力】【水题】
- HDU 1081 To The Max 暴力模拟O(n^4) dp优化O(n^3)
- 暴力??
- 暴力
- 暴力
- 暴力
- 暴力
- 暴力
- 杭电 2566(母函数 暴力)
- lightoj1035 欧拉函数(暴力)
- 暴力破解之全排列函数
- hdu 2566 暴力枚举+母函数
- substr函数
- substr() 函数
- substr函数
- substr 函数
- SUBSTR()函数
- mpi环境搭建
- 余杭大板同 网游上线了
- 111111111
- EL表达式的使用
- iOS 给tableView 每行设置预估行高estimatedRowHeight,达到cell高度的自适应
- 勿忘《暴力解》:总是想着O(n),经常忽略暴力解。substr函数是个好函数
- HTML简单使用
- Arukas Docker 制作自己的镜像---Docker hub
- Codeforces Round #402 (Div. 2) C. Dishonest Sellers 简单模拟
- heap和stack有什么区别
- Python之路【1】:Python简介和入门
- elasticsearch
- MongoDB系列(一):简介及安装
- 三星笔记本电脑光驱改SSD