微軟2014 演算法筆試題目1及答案,2014筆試
題目1 : Beautiful String時間限制:10000ms單點時限:1000ms記憶體限制:256MB
描述
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".
輸入
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 10MB.
輸出
For each test case, output a single line "YES"/"NO" to tell if the string contains a beautiful sub-string.
提示
Huge input. Slow IO method such as Scanner in Java may get TLE.
-
範例輸入
-
43abc4aaab6abccde3abb
-
範例輸出
-
YESNOYESNO
解析:採用狀態機器模型
答案
#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的簡便演算法
1+2+3+4……+2014
= (1+2014) + (2+2013) + ... + (1007 + 1008)
= 2015 * 1007
= 2029105
書上說這種演算法最早是大數學家高斯發明的.
微軟面試題 - 大家幫我解答一下,!
兩個“六”稱一次,淘汰六個。兩個“三”稱第二次,再淘汰三個。剩下的三個中取任意兩個稱。對嗎朋友。因為你已經知道方法了,所以你應該看明白我比較建華的回答。(這道題我曾經用20秒答出,別誤會。沒有與你過不去的意思。我這有北京微軟招聘會的一整套題)