A question for taking part in Microsoft Online Testing

Source: Internet
Author: User

Question: beautifulstring

Time Limit: 10000 ms

Single-point time limit: 1000 ms

Memory limit: 256 MB


Description

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

Here are someexample of 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 ".

Input

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 10 MB.

Output

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

Prompt

Huge input. slowio method such as needed in Java may get TLE.

Sample Input

4

3

ABC

4

Aaab

6

Abccde

3

ABB

Sample output

Yes

No

Yes

No


The answers I submitted and AC are:

# Include <iostream>

# Include <stdio. h>

Using namespace STD;


Int main ()

{

Int I;

Int T;

Int N;

Scanf ("% d", & T );

While (t --)

{

Scanf ("% d", & N );

Getchar ();


Int CNT [3], Loc = 0;

Char CH, pre = '\ 0 ';

Bool flag = false;

// Memset (CNT, 0, sizeof (CNT ));

For (I = 0; I <n; I ++)

{

Scanf ("% C", & Ch );

// If (CH <'A' | ch> 'Z') {I --; continue ;}

If (FLAG) {continue ;}

If (CH = pre) {CNT [loc] ++ ;}

Else {

If (loc = 2 & CNT [0]> = CNT [1] & CNT [2]> = CNT [1]) {

Flag = true;

Continue;

}

Else if (loc = 2 & (pre + 1 = CH )){

CNT [0] = CNT [1], CNT [1] = CNT [2];

CNT [2] = 1;

}

Else if (pre + 1 = CH ){

CNT [++ loc] = 1;

}

Else {

Loc = 0;

CNT [loc] = 1;

}

Pre = CH;

}

}

If (loc = 2 & CNT [0]> = CNT [1] & CNT [2]> = CNT [1]) {flag = true ;}

If (FLAG) {printf ("Yes \ n ");}

Else {printf ("NO \ n ");}

} // End while

Return 0;

}


The basic idea is: to form a beautiful string, it must be composed of three consecutive letters, and the number of intermediate letters must be smaller than the number of front and back letters, so you just need to find the combination.


A question for taking part in Microsoft Online Testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.