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的更多相关文章
随机推荐
- JAVA基础知识之JVM-——类初始化
我们通常说的类初始化,其实要分为三个阶段,类加载,连接,和初始化.他们大致完成以下功能.类加载将class文件载入内存,类连接进行内存分配,初始化进行变量赋值. 类的加载,连接和初始化 java.la ...
- vsftp 使用匿名帐号登陆
1.正常安装. 2.改配置文件:vi /etc/vsftpd/vsftpd.conf #允许匿名用户登录FTP anonymous_enable=YES #设置匿名用户的登录目录(如需要,需自己添加并 ...
- phpcms 02
头部和尾部包含 1 默认的首页模板 C:\wamp\www\phpcms\templates\ypzy2014\content\index.html 打开模板查看 第一句 {template &quo ...
- C++导出文件后缀dll,lib,exp,def
简要的介绍一下在微软开发工具中(VC)静态链接库和动态链接库生成过程中出现的.dll .lib .def 和 .exp文件类型.windows平台上可执行文件可能是一个.exe文件也可能四个.dl ...
- C#窗体四边框阴影效果的实现
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 解决json_encode中文UNICODE转码问题
用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式,如果想汉字不进行转码,这里提供三种方法 .升级PHP,在PHP5., 这个问题终于得以解 ...
- ListableBeanFactory
ListableBeanFactory public interface ListableBeanFactory extends BeanFactory 该接口中定义了可以获取配置中所有bean的信息 ...
- 建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。
package exception; public class TestException { public static void main(String[] args) { for(int i=0 ...
- [转]C# List<T>的详细用法
所属命名空间:System.Collections.Generic publicclassList<T> : IList<T>,ICollection<T>, IE ...
- 关于float的说明
关于float的说明 如图所示,蓝色的div和红色的div处于黑色的div(宽为500px)之中,其中蓝色的div设置了做浮动: 由图一(红色div的margin-top为10px,margi ...