题目大意

题目连接:beautiful string 
    写代码之前,考虑清楚流程,以及需要维护的变量....

实现

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
char str[10 * 1024 * 1024 + 2];
int main(){
int T;
scanf("%d", &T);
while (T--){
int n;
scanf("%d", &n);
getchar();
scanf("%s", str);
int i = 0;
bool valid = false;
int beg = 0, end = 0;
int count_1 = 0; //前2个字符连续的个数
int count_2 = 0; //前1个字符连续的个数
while (beg < n && !valid){
char cur = str[beg]; //当前字符
while (end < n && !valid){
if (str[end] == cur){
//如果前两个字符都有,且当前字符数目大于等于中间字符数目,
//则肯定可以形成一个valid的子串
if (count_2 && count_1 && end - beg + 1 >= count_2){
valid = true;
break;
}
end++;
}
else{
if (str[end] - cur != 1){//出现了一个不连续字符,则清空count_1,count_2
count_1 = 0;
count_2 = 0;
}
else{
//当前字符之前的那个字符的连续个数大于count_2的个数,
//则只能以当前字符作为新的中间字符 count_2,且
//count_1 清空
if (count_2 && count_2 < end - beg){
count_1 = 0;
count_2 = end - beg;
}
else{
//更新count_1 为count_2
//更新 count_2为 end-beg;
count_1 = count_2;
count_2 = end - beg;
}
}
break;
}
}
beg = end;
}
if (valid)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

hiho_1061_beautiful_string的更多相关文章

随机推荐

  1. combox绑定后添加自定义列

    DataTable dt = shUBll.FindAllByWhere(""); DataRow dr = dt.NewRow(); dr["SUID"] = ...

  2. ip地址定位库

    ip2region 1.2.1 发布了,新增 Python 内存查询+数据文件更新. 准确率99.9%的ip地址定位库,0.0x毫秒级查询,数据库文件大小只有1.5M,提供了java, php, c, ...

  3. gulp 建立一个简单的自动化

    前端项目需要的功能: 1.图片(压缩图片支持jpg.png.gif) 2.样式 (支持sass 同时支持合并.压缩.重命名) 3.javascript (检查.合并.压缩.重命名) 4.html (压 ...

  4. Easyui Layout Center 全屏方法扩展

    $.extend($.fn.layout.methods, { full : function (jq) { return jq.each(function () { var layout = $(t ...

  5. 取出一个int的每一位,用算法

    int a=1234: int current: while(a) { current=a%10://4 cout<<current; a=a%10; }

  6. 自动布局(Masonry)设置tabbar

    //自定义标签工具栏 - (void) initTabBarView{ // self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(, k ...

  7. svn转移版本库

    1.导出 svnadmin dump命令语法svnadmin dump REPOS_PATH [-r LOWER[:UPPER]] [--incremental] 示例:svnadmin dump E ...

  8. strip_tags,htmlspecialchars,htmlentities,stripslashes,addslashes学习小结

    一.strip_tags 从字符串中去除 HTML 和 PHP 标记 string strip_tags ( string $str [, string $allowable_tags ] ) str ...

  9. C#支持文件拖拽

    private void listBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataForm ...

  10. UVA 10453 十七 Make Palindrome

    Make Palindrome Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit St ...