您的位置:首页 > 其它

hihoCoder 1061 Beautiful String

2016-05-25 14:01 573 查看
题目的意思是判断一个字符串是否包含三个及三个以上的连续递增子串,像abc,aabbcc都是beautiful string ,题目不难,是一道水题

注意点:

连续序列的个数不一定需要完全一样,实际上首尾字符的个数可以比中间的字符的数目多,即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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: