微软校招2015 Beautiful String

时间限制: 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.

提示

Huge input. Slow IO 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>

bool IsBeautiful(char *str, int length) {
	if(str == NULL || length < 3)	return false;
	char c[3];
	int count[3] = {0, 0, 0};
	int i  = 0;

	for(int j = 0; j < 2; j++){
		if(i >= length)	return false;
		c[j] = str[i];
		while(i < length && str[i] == c[j]){
			count[j]++;
			i++;
		}
	}
	
	for( ;i < length;) {
		c[2] = str[i];
		count[2] = 0;
		while(i < length && str[i] == c[2]) {
			count[2]++;
			i++;
		}
		if(c[0]+1 == c[1] && c[1]+1 == c[2] && count[0] >= count[1] && count[2] >= count[1]) {
			return true;
		}
		c[0] = c[1];
		c[1] = c[2];
		count[0] = count[1]; 
		count[1] = count[2];
	}
	return false;
}

char buffer[10000005];
int main(void) {
	int tests;
	int length;

	//freopen("beautiful_string.txt", "r", stdin);
	
	scanf("%d", &tests);
	while (tests--) {
		scanf("%d", &length);
		getchar();
		gets(buffer);
		if(IsBeautiful(buffer, length)) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}

	}
   	return 0;
}
 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值