hihoCoder 1061 Beautiful String
2016-05-25 14:01
573 查看
题目的意思是判断一个字符串是否包含三个及三个以上的连续递增子串,像abc,aabbcc都是beautiful string ,题目不难,是一道水题
注意点:
连续序列的个数不一定需要完全一样,实际上首尾字符的个数可以比中间的字符的数目多,即aabccc也是满足要求的字符串
在顺序扫描的过程如果发现当前序列不满足要求的时候需要返回到处理序列的第一个字符的下一个字符,不能接着当前字符处理,比如aabbcd,在处理到c的时候发现c的个数比b的少一,这时候需要返回到b处进行处理,这样可以找到bcd这个递增子串,如果是接着处理d的话就会出错。下边是c++代码。
注意点:
连续序列的个数不一定需要完全一样,实际上首尾字符的个数可以比中间的字符的数目多,即aabccc也是满足要求的字符串
在顺序扫描的过程如果发现当前序列不满足要求的时候需要返回到处理序列的第一个字符的下一个字符,不能接着当前字符处理,比如aabbcd,在处理到c的时候发现c的个数比b的少一,这时候需要返回到b处进行处理,这样可以找到bcd这个递增子串,如果是接着处理d的话就会出错。下边是c++代码。
#include<iostream> #include <vector> #include <string> using namespace std; bool isBeautifulString(string &str) { if (str.empty()) return false; vector<int> count; string singleNum; char pre = str[0]; int currCount = 1; for (int i = 1;i <= str.size();i++) { if (i == str.size() || str[i] != pre) { singleNum.push_back(pre); count.push_back(currCount); currCount = 1; pre = str[i]; } else { currCount++; } } int continusCount = 1; //char preChar = str[0]; int preIndex = 0; for (int i = 1;i<singleNum.size();i++) { if (singleNum[i] == singleNum[preIndex] + 1 && (count[i] == count[preIndex] || (continusCount == 2 && count[i] >= count[preIndex])||(continusCount == 1 && count[i] <= count[preIndex]))) { continusCount++; if (continusCount >= 3) return true; } else { i -= continusCount-1; continusCount = 1; } preIndex = i; } return false; } int main() { int t; cin >> t; while (t--) { int len; cin >> len; string str; cin>>str; if (isBeautifulString(str)) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
相关文章推荐
- 正则表达式获取URL参数
- socket编程:简单UDP服务器/客户端编程
- C++之动态空间分配与释放:new & delete
- 韩顺平Spring框架学习,学习笔记(八)
- Hibernate 单向一对多配置以及增删改查
- Android 判断SD卡是否存在及容量查询
- SQL模糊查询
- VC++异常处理
- AngularJs学习笔记(二) 指令directive
- Linux chattr/lsattr
- Octave 线性代数 行列式 4
- hdu2092 整数解
- C++异常处理
- GStreamer基础教程02——GStreamer概念
- linux fuse文件系统在 android fuse sdcard的 运用
- Cas(5)-修改Cas Server的其它配置
- 使用apache的ab工具做压力测试
- Automated e2e testing- WebDriverJS, Jasmine and Protractor
- Git 服务器搭建
- DOM操作(createElement()、createTextNode()、appendChild())