Hiho for the 58th week. Beautiful String DP

Source: Internet
Author: User

Topic 1:beautiful String time limit:10000msSingle Point time limit:1000msMemory 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
43abc4aaab6abccde3abb
Sample output
Yesnoyesno
 Test instructions, given a string, whether there is a contiguous substring, satisfies that the substring has more than 3 characters, is in ascending order and the number of each character is the same, such as a. Ab... BC...C where a b c is equal in number and is incremented. Because, when more than 3 times, always 3 characters are satisfied, so only consider is the case of 3, Num[i] that the first position, the back up to Num[i] characters are the same. If, there is such a x1...x2...x3...,x1x2x3 increment, and num[x2] = = Num[x3], num[x3] >= num[x1] (because such a string can exist AABBCCC, so long as, num[x3] >= x[1]) , str[x1] = = Str[i] + 1 &&  str[x2] = = Str[i] + 2&& num[x1] = = Num[i] && num[x2] >= num[i]& nbsp; the description exists. 
#define N 10000050#define M 100005#define maxn 205#define MOD 1000000000000000007int n,num[n],len;char str[n];int main () {            while (s (n)!=eof) {while (n--) {S (len);            Gets (str);            FI (len) {str[i] = GetChar ();            } num[len-1] = 1;                for (int i = len-2; i>=0;i--) {if (str[i] = = str[i+1]) num[i] = num[i+1] + 1;            else num[i] = 1;            } bool flag = FALSE;                FI (len) {int x1 = i + num[i],x2 = i + 2 * num[i],x3 = i + 3 * num[i];  if (x3 <= len && str[x1] = = Str[i] + 1 && str[x2] = = Str[i] + 2 && num[x1] = =                    Num[i] && num[x2] >= Num[i]) {flag = true;                Break            }} if (flag) {printf ("yes\n"); } else {printf ("no\n "); }}} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hiho for the 58th week. Beautiful String DP

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.