Hihocoder1061-beautiful String

Source: Internet
Author: User

Time limit: 10000ms
Single point time limit: 1000ms
Memory Limit: 256MB

Describe

We say a string is beautiful if it have the equal amount of 3 or more continuous letters (in increasing order.)
Here is some example of valid beautiful strings: "abc", "CDE", "AABBCC", "AAABBBCCC".
Here is 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".

Input

The first line contains an integer number between 1 and ten, indicating how many test cases is followed.
For each test Case:first the number of letters in the string; Second is the string. String length is less than 10MB.

Output

For each test case, output A, "YES" or "NO" to-tell if the string contains a beautiful sub-string.

Tips

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

Sample input

4 3 ABC 4 AAAB 6 ABCCDE 3 ABB

Sample output

Yes No Yes No

Test instructions

If the given string has a continuous increment of three letters or a constant increment of three letters, then the output is yes, in conjunction with the sample understanding.

Ideas

We only need to determine the 3, with a CNT array to record the number of corresponding letters, the middle of the comparison, that is, the middle of less than equal to the two sides and is the middle of the letter is less than or equal to the same conditions

Code
#include <bits/stdc++.h>using namespacestd;#defineMAXN 10000000intCNT[MAXN];intMain () {intT; CIN>>T;  while(t--) {        intN; CIN>>N; strings; CIN>>s; intFlag =0; intCur =0; Cnt[cur]=1;  for(inti =1; I < n; i++) {            if(S[i] = = S[i-1]) Cnt[cur]++; Else{s[++cur] =S[i]; Cnt[cur]=1; }        }         for(inti =1; i < cur; i++) {            if(S[i] = = S[i-1] +1&& S[i] +1= = S[i +1] && Cnt[i] <= cnt[i-1] && Cnt[i] <= cnt[i +1]) {flag=1;  Break; }} puts (flag?"YES":"NO"); }    return 0;}

?

Hihocoder1061-beautiful String

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.