题目大意

题目连接: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. zend studio 注释快捷键

    使用zend studio编写程序时,我们经常要做一些注释.zend studio为我们提供了行注释和块注释的快捷键ctrl+slash和ctrl+shift+slash,开始不明白什么是slash, ...

  2. randperm函数

    randperm功能是随机打乱一个数字序列. 语法格式: y = torch.randperm(n) y是把1到n这些数随机打乱得到的一个数字序列. th> torch.randperm() [ ...

  3. Intellij IDEA svn的使用记录

    这里的忽略一直灰色的,可以进入 这里的版本控制里进行忽略选择 或者 这里进行添加 这里有三个选择 按照顺序 1.忽略指定的文件 2.忽略文件夹下所有文件 3.忽略符合匹配规则的文件 到Commit C ...

  4. ie兼容整理

    那里面有东西要长研究 ie bug集合关于如何给各种浏览器打bug,可查询:browser hacks 几篇处理ie问题的帖子,帖子1

  5. 2017年1月6日 星期五 --出埃及记 Exodus 21:32

    2017年1月6日 星期五 --出埃及记 Exodus 21:32 If the bull gores a male or female slave, the owner must pay thirt ...

  6. 2016年10月12日 星期三 --出埃及记 Exodus 18:23

    2016年10月12日 星期三 --出埃及记 Exodus 18:23 If you do this and God so commands, you will be able to stand th ...

  7. python学习笔记三 深浅copy,扩展数据类型(基础篇)

    深浅copy以及赋值 对于字符串和数字而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy n1 = #n1 = 'hahahaha' #赋值n2 = n1#浅co ...

  8. 在C++工程中设置全局函数

    在头文件中对该函数进行全局函数的声明: extern void Test(); 在cpp文件中进行函数的定义: void Test() { MessageBox(NULL,L"调用了C++的 ...

  9. Html basic tag

    The <p> tag defines a paragraph. http://www.w3schools.com/tags/tag_p.asp The <td> tag de ...

  10. django-crontab定时任务

    django-crontab实现定时任务 1 django-crontab安装 django-crontab安装: django-crontab加入:只需要将INSTALLED_APPS即可.如下代码 ...