题目大意

题目连接: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. 2016年11月3日 星期四 --出埃及记 Exodus 19:19

    2016年11月3日 星期四 --出埃及记 Exodus 19:19 and the sound of the trumpet grew louder and louder. Then Moses s ...

  2. LYK 快跑!(run)

    LYK 快跑!(run)Time Limit:5000ms Memory Limit:64MB[题目描述] LYK 陷进了一个迷宫! 这个迷宫是网格图形状的. LYK 一开始在(1,1)位置, 出口在 ...

  3. C堆栈

    C堆栈实现的表达式求值 //Luangeng #include<stdio.h> #include<conio.h> #include<windows.h> #de ...

  4. MySQL基础(二)——DDL语句

    MySQL基础(二)--DDL语句 1.什么是DDL语句,以及DDL语句的作用 DDL语句时操作数据库对象的语句,这些操作包括create.drop.alter(创建.删除.修改)数据库对象. 2.基 ...

  5. OLTP基准测试脚本

    关键语句:ll /local/sysbenchtest/sysbench-0.5/sysbench/tests/db--查看lua脚本/usr/local/mysql/bin/mysql -u roo ...

  6. SD卡驱动分析(二)

    三.下面分析一下高通的android2.3的代码中SD卡驱动的流程. 在kernel中,SD卡是作为平台设备加入到内核中去的,在/kernel/arch/arm/mach-msm/devices-ms ...

  7. 我的android学习经历20

    WebView的使用 WebView既可以和Intent一样实现界面跳转一样,让系统浏览器打开页面,也可以在应用程序中打开页面 注意用WebView时,需要注册网络服务 代码如下: package c ...

  8. maquee 无缝轮播

    页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...

  9. MySQL用户名和密码问题

    MySQL使用脚本的方法: source d:\datafilename.sql # mysql -uroot -p Enter password: ERROR 1045 (28000): Acces ...

  10. jsp利用cookie记住用户名,下次登录时显示在文本框中(仅仅一个Cookie就整了将近三个小时,⊙﹏⊙b汗)

    <%@page import="java.net.URLDecoder"%> <%@page import="sun.security.util.Len ...