hiho_1061_beautiful_string
题目大意
题目连接: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的更多相关文章
随机推荐
- 谷歌Chrome浏览器如何设置网页的默认编码方法
设置->高级->自定义字体->编码->utf-8
- ZendStudio调试配置(XDebug)
服务器配置:WAMP Sever 3.0.0 调试器环境:Zend Studio 12.0 Build ID: 12.0.0.v20141117-20141115-2331-278 License: ...
- web简单连接html文件测试
Web工程: 条件: apache-tomcat-6.0.20(文件夹/7.0)=======位于E盘 标题:链接服务器 步骤: 第一步:打开apache-tomcat-6.0.20-bin-star ...
- java交换两个数字位置
第一种:在main输出,通过反射实现 1 public static void main(String[] args) throws Exception { Integer a = 1; Intege ...
- Codeforces Round #368 (Div. 2) B
Description Masha wants to open her own bakery and bake muffins in one of the n cities numbered from ...
- ios沙盒路径
http://www.cnblogs.com/ios-wmm/p/3299695.html iOS沙盒路径的查看和使用 NSString *path = NSHomeDirectory();//主目录 ...
- jquery之each()、$.each()[jQuery.each()]
导航: 1,jQuery对象(实例)的each()函数 2,全局jQuery对象的each()函数 一:jQuery对象(实例)的each()函数 each()函数用于以当前jQuery对象匹配到的每 ...
- WebRTC的学习(二)
英文原文的链接地址为:https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Overview WebRTC是由一些关联的API和协议一 ...
- QLibrary执行load失败
QLibrary clyy("ledsender.so"); if(!clyy.load());在执行该行代码时会导致程序崩溃. ------------------------- ...
- Shell概述
一,Shell原理