Microsoft 2014 algorithm written question 1 and answer

Source: Internet
Author: User

topic 1:beautiful Stringtime limit:10000ms single point time limit:1000ms memory limit:256MB
Description

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


Analysis: Using state machine model


Answer

#include <cstdio> #include <string> #include <iostream>using namespace std;int isbeautifulstring (int num,char* str); int main (void) {int n;scanf ("%d", &n); int *sample_size=new Int[n];char **sample=new char*[n];for (int i=0;i<n;i++) {scanf ("%d", &sample_size[i]), Sample[i]=new char[sample_size[i]];scanf ("%s", Sample[i]);} for (int i=0;i<n;i++) {if (isbeautifulstring (Sample_size[i],sample[i]) ==1) printf ("yes\n"); elseprintf ("no\n");} Delete [] sample_size;delete [] Sample;return 0;} int isbeautifulstring (int num,char* str) {int flag=0;int l[4]={0};int count=0;l[count]=1;for (int i=1;i<num;i++) {if ( STR[I]==STR[I-1]) L[count]++;else if (str[i]==str[i-1]+1) L[++count]++;else{count=0;memset (l,0,sizeof (int)); l[ Count]=1;continue;} if (count==3| | I==NUM-1) {if (l[0]>=l[1]&&l[1]<=l[2]&&l[1]>0) {flag=1;break;} ELSE{COUNT-=1;L[0]=L[1];L[1]=L[2];L[2]=L[3];}}} return flag;}



Microsoft 2014 algorithm written question 1 and answer

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.