hiho_1061_beautiful_string
题目大意
题目连接:beautiful string
写代码之前,考虑清楚流程,以及需要维护的变量....
实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #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; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步