Beautiful String-连续字符串

学习C++的过程中,浏览了一些csdn上关于C/C++的博客,作为一个菜鸟,一直诚惶诚恐,因为自己半路出家,学习的时间也较短,以至于连博客也不敢发,但是想想,发帖,以及被吐槽的过程其实就是我进步的过程,所以决定开始发帖,也希望在这个过程中自己能快速增长相关的知识。

别人问我的题,这是一个微软在hihocoder上的测试题,题目不难,但对我这样一个新手来说也是费了不少脑细胞。下面是原题:

 

题目1 : BeautifulString

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

We say a stringis beautiful if it has the equal amount of 3 or more continuous letters (inincreasing order.)

Here are some exampleof valid beautiful strings: "abc", "cde","aabbcc", "aaabbbccc".

Here are someexample of invalid beautiful strings: "abd", "cba","aabbc", "zab".

Given a stringof alphabets containing only lowercase alphabets (a-z), output "YES"if the string contains a beautiful sub-string, otherwise output "NO".

输入

The first linecontains an integer number between 1 and 10, indicating how many test cases arefollowed.

For each testcase: First line is the number of letters in the string; Second line is thestring. String length is less than 10MB.

输出

For each testcase, output a single line "YES"/"NO" to tell if the stringcontains a beautiful sub-string.

提示

Huge input. SlowIO method such as Scanner in Java may get TLE.
 

样例输入

4

3

abc

4

aaab

6

abccde

3

abb

样例输出

YES

NO

YES

NO

 

我的代码如下:

#include<stdio.h>
#include<stdlib.h>

void Beautiful_String(char *s,  int ai)
{
    int ibs = 0;
    int kbs = 0;
    int jbs = 0;
    while(ibs <= ai - 3)
    {
        kbs = 1;
        while( *(s+ibs) == *(s+ibs+kbs) )
        {
            kbs++;
        }
        
        while( (*(s+ibs) - *(s+ibs+kbs+jbs) == -1) && (*(s+ibs) - *(s+ibs+2*kbs+jbs) == -2) )//kbs != 0
        {
            if(jbs == kbs-1)
            {
                printf("YES\n");
                return;
            }
            jbs++;

        }
jbs = 0;
        ibs++;


    }

    printf("NO\n");
    return;
    
}

int main()
{
int num_test = 0;
int a[10] = {0};
char *s[10] = {'\0'};
int i = 0;

scanf("%d", &num_test);
if(num_test < 1 || num_test > 10)
return 0;

while(i < num_test)
{
scanf("%d", &a[i]);
        s[i] = (char *)malloc(a[i] + 1);

        fflush(stdin);
        gets(s[i]);
//        printf("%s", s[i]);//just for test
i++;
}


    for(i = 0; i < num_test; i++)
        Beautiful_String(s[i], a[i]);


return 0;
}

运行结果:

6
9
khgpslabc
8
aanoabcl
6
aabbcc
9
aaabbbccc
5
dshec
12
aaaabbbbcccc
YES
YES
YES
YES
NO
YES
Press any key to continue

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值