hiho一下:Beautiful String

记不清这是 hiho一下第几周的题目了,题目不难,不过对于练习编程,训练思维很有帮助。况且当时笔者处于学习算法的早期,
所以也希望刚接触算法的同学能多去练习一下。

题目介绍

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
We say a string is beautiful if it has the equal amount of 3 or more continuous letters (in increasing order.)
Here are some example of valid beautiful strings: “abc”, “cde”, “aabbcc”, “aaabbbccc”.
Here are some example of invalid beautiful strings: “abd”, “cba”, “aabbc”, “zab”.
Given a string of alphabets containing only lowercase alphabets (a-z), output “YES” if the string contains a
beautiful sub-string, otherwise output “NO”.
输入
The first line contains an integer number between 1 and 10, indicating how many test cases are followed.
For each test case: First line is the number of letters in the string; Second line is the string. String length
is less than 10MB.
输出
For each test case, output a single line “YES”/”NO” to tell if the string contains a beautiful sub-string.

其实网站上面的题目分析写得很好了(还有翻译),要去看的,点击这里

AC 源码

 1	#include <stdio.h>
2
3 struct chnum
4 {
5 char c;
6 int n;
7 };
8
9 struct chnum *col (int l, char s[])
10 {
11 int i, j = 0;
12 int len = l;
13 struct chnum cn[len];
14
15 cn[0].c = s[0];
16 cn[0].n = 1;
17
18 for (i=1; i<len; i++)
19 {
20 char t = s[i];
21
22 if (t != s[i-1])
23 {
24 j++;
25 cn[j].c = s[i];
26 cn[j].n = 1;
27 }
28 else
29 cn[j].n++;
30 }
31
32 if (j < len - 1)
33 {
34 cn[++j].c = ' ';
35 }
36
37 return cn;
38 }
39
40 int check (struct chnum *cn, int len)
41 {
42 for (int i=1; i<=len-2; i++)
43 {
44 if ((cn->c + 1 == (cn+1)->c
45 && (cn+1)->c + 1 == (cn+2)->c)
46 && (cn->n >= (cn+ 大专栏  hiho一下:Beautiful String1)->n
47 && (cn+1)->n <= (cn+2)->n)
48 )
49 return 0;
50 else if (cn->c == ' ')
51 break;
52 else
53 cn++;
54 }
55
56 return 1;
57 }
58
59 int main ()
60 {
61 int num;
62
63 scanf ("%d", &num);
64
65 int ret[num];
66 for (int i=0; i<num; i++)
67 {
68 int len;
69 scanf ("%d", &len);
70
71 char str[len + 1];
72 scanf ("%s", str);
73
74 struct chnum *cn = col (len, str);
75
76 if (check (cn, len) == 0)
77 ret[i] = 1;
78 else
79 ret[i] = 0;
80 }
81
82 for (int i=0; i< num; i++)
83 {
84 if (ret[i])
85 printf ("YESn");
86 else
87 printf ("NOn");
88 }
89
90 return 0;
91 }

这种小代码我就不怎么搞注释了,我相信 main 函数的流程还是能被一眼看出来的,而且模块化的编程风格应该更好理解。下面讲下我
第一次提交遇到的 WA 错误。

Handle 一切你能 Handle 的地方

开源项目有一条哲学:只要眼球多,bug 都好捉。对于个人写程序来说,撑死了也就俩眼球,所以你必须要尽可能地 handle 一切,
比如变量该初始化就初始化,数组长度一定要考虑等等。

第一次提交,上面代码中没有

 32		if (j < len - 1)
33 {
34 cn[++j].c = ' ';
35 }

 50			else if (cn->c == ' ')
51 break;

这两处的。看了代码就知道 cn 数组只有在字符串中每两个相邻字符都不相同的情况下才会全部被赋值。而其他的情况下,长度根本
没有到 len。那么问题来了,没有被赋值到的 cn 数组元素的值会是什么呢,鬼知道!然后就有可能本来输出该是 NO 的输出了 YES。
所以我决定手动给出结束标志,一个空格,这当然是一种权宜之策。

而且字符串数组声明的时候,长度一定得是串长度加一。我做实验来看,就算 strlen 访问 str[5]=”hello”,越界了也还是正常输出
。但注意一下总是好的。

In Wuhan 337 Prison

hiho一下:Beautiful String的更多相关文章

  1. Codeforces Round #604 (Div. 2) A. Beautiful String

    链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...

  2. CF1328B K-th Beautiful String

    CF1328B K-th Beautiful String,然而CF今天却上不去了,这是洛谷的链接 题意 一个长度为\(n\)的字符串,有2个\(\texttt{b}\)和\(n-2\)个\(\tex ...

  3. hihocoder 1061.Beautiful String

    题目链接:http://hihocoder.com/problemset/problem/1061 题目意思:给出一个不超过10MB长度的字符串,判断是否里面含有一个beautiful strings ...

  4. Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)

    题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...

  5. HackerRank beautiful string

    问题 https://vjudge.net/problem/HackerRank-beautiful-string 给一个字符串S,可以任意取走S中的两个字符从而得到另外一个字符串P,求有多少种不同的 ...

  6. B. Pasha and String

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>

    L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 43000/43000KB (Java/ ...

  8. Pasha and String(思维,技巧)

    Pasha and String Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  9. Nikita and string [思维-暴力] ACM

    codeforces Nikita and string time limit per test   2 seconds memory limit per test   256 megabytes O ...

随机推荐

  1. 理论优美的深度信念网络--Hinton北大最新演讲

    什么是深度信念网络 深度信念网络是第一批成功应用深度架构训练的非卷积模型之一. 在引入深度信念网络之前,研究社区通常认为深度模型太难优化,还不如使用易于优化的浅层ML模型.2006年,Hinton等研 ...

  2. JavaScript学习笔记 - 进阶篇(4)- 函数

    什么是函数 函数的作用,可以写一次代码,然后反复地重用这个代码. 如:我们要完成多组数和的功能. var sum; sum = 3+2; alert(sum); sum=7+8 ; alert(sum ...

  3. FullPage.js中文帮助文档API

    fullPage.js的方法: 1. moveSectionUp() 功能:向上滚动一页. 2. moveSectionDown() 功能:向下滚动一页. 3. moveTo(section, sli ...

  4. Covisibility Graph

    在Orb-Slam中有三个地图分别是Covisibility Graph,Spanning Graph,以及Essential Graph,它们三个分别是什么意思呢? 首先,图优化是目前视觉SLAM里 ...

  5. python爬虫破解带有CryptoJS的aes加密的反爬机制

    发现问题 在一次偶然中,在爬取某个公开网站(非商业型网站)时,老方法,打开调试工具查看请求方式,请求拦截,是否是异步加载,不亦乐乎,当我以为这个网站非常简单的时候,发现二级网页的地址和源码不对应 Aj ...

  6. a标签的一些特殊使用

    <a href="tel:10086">10086</a> //点击后直接拨打10086  <a href="mailto:c1586@qq ...

  7. 2019-2020-1 20199324《Linux内核原理与分析》第三周作业

    第二章 操作系统是如何工作的 一.知识点总结 1.计算机的三个法宝 存储程序计算机 函数调用堆栈机制.堆栈:是C语言程序运行时必须使用的记录函数调用路径和参数存储的空间. 中断 2.堆栈相关的寄存器和 ...

  8. 1)PHP,数据库操作类网址

    (1)脚本之家 http://www.jb51.net/article/94347.htm (2)一个博客 http://www.cnblogs.com/lvchenfeng/p/5003629.ht ...

  9. 05 - Tomcat 线程池的配置与优化

    添加 Executor 在server.xml中的Service节点里面,增加executor节点,然后配置connector的executor属性,如下: <Executor name=&qu ...

  10. 《C程序设计语言》练习1-10

    #include<stdio.h> main() { int c; c=getchar(); while (c !=EOF) { if (c=='\t') { c='\\'; putcha ...