Microsoft 2014 algorithm Test Question 1 and answer, 2014 Test

Source: Internet
Author: User

Microsoft 2014 algorithm Test Question 1 and answer, 2014 Test
Question 1: Beautiful String time limit: milliseconds ms single point time limit: 256 Ms memory limit: MB

Description

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 ".

Input

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

Output

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

Prompt

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

Sample Input
43abc4aaab6abccde3abb
Sample output
YESNOYESNO


Analysis: using the 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)*4);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;}




1 + 2 + 3 + 4 ...... + 2014 simple algorithms

1 + 2 + 3 + 4 ...... + 2014
= (1 + 2014) + (2 + 2013) +... + (1007 + 1008)
= 2015*1007
= 2029105

The book says that this algorithm was first invented by the great mathematician Gauss.

Microsoft interview questions-help me answer ,!

Two "six" are called once, and six are eliminated. Two "three" are called the second time, and then three are eliminated. The remaining three are named as any two. Right? Friend. Because you already know the method, you should understand my answer. (I used to answer this question in 20 seconds. Don't get it wrong. It doesn't mean you can't get through it. I have a complete set of questions for the Microsoft job fair in Beijing)

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.