#第一题就餐问题def check_lunch (num, time, input): for i in range (num): if ((i + 1) % time != 0 and input[i] != (i + 1) % time): input[i] = 0 elif ((i + 1) % time == 0 and input[i] != time): input[i] = 0; return inputnum = 11; #第二题输入联想def auto_complete (str, tmp): arr = str.split (' ') n = len (TMP) m = len (arr) count = [0, 0] maxindex = 0 &nbSp; output = [] err =0 for i in range (N): for j in range (m): if (Tmp[i] == arr[j][i]): count[j] += 1 #print (Err) maxnum = max (count) for i in range (m): if (Tmp[0] != arr[i][0]): err +=1 if ( ERR&NBSP;==&NBSP;2):return "" if (count[i] == Maxnum): output.append (arr[i]+ " ") return output;def findstr (PATTERN,STR): lenstr = len (str) &Nbsp; lenpat = len (pattern) i=0 j=0 start = -1 step = 0 while (I<lenstr and j<lenpat): if (Str[i] != pattern[j]): i = i+1 j = 0 step =0 else: i = i+1 j = j+1 step =&nBsp;step+1 if (step == LENPAT): return i-step,i return -1,0pats=[["If", "then"], ["Switch", "case", "End"], ["Do", "while"]]def analysize2 (str): #第三题语法分析 lenstr = len (str ) lenpat = len (PATS) i =0; count = 0 results = [] while (i< LENSTR): for j in range (0,lenPat): result = findpat (PATS[J],STR) if (result[0]>0): results.append (Result) if (results ==[]): break minitem= (0,lenStr,0) for item in results: if (item[1]<minitem[1]): minItem = item if (MINITEM[0]==0&NBSP;OR&NBSP;MINITEM[2]>=LENSTR): break count += minitem[0] start = minitem[2] +1 &NBSP;STR&NBSP;=&NBSP;STR[START:]&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;I&NBSP;+=&NBSP;MINITEM[2] +1 results.clear () return Countdef findpat (PATTERN,STR): &NBSP;&NBSP;&NBSP;&NBSP;STR0&NBSP;=&NBSP;STR&NBSP;&NBSP;&NBSP;&NBSP;LENP = len (pattern) lenstr = len (str) k Count= 0,0 i=0;j=0 start =0 end = 0 while (K<LENP&NBSP;AND&NBSP;I<LENSTR): i1,j1=findstr (PATTERN[K],STR) if (i1!=-1 ): #start. Append (i1+j) start = i1 +j i=j+i1 j=j+j1 k=k+1 str = str0[j:] m,n=findstr (PATTERN[K],STR) if (m!=-1): k=k+1 i,j = j+m,j+n str = str0[j:] if (K==LENP): count =count+1; end=j-1 #k = 0 else: break return count,start,end;str = "Switch if sdfas then case then if adb switch case then "Print (Check_lunch (11,3,[ 1,2,3,3,1,3,1,1,1,1,2,3]) print (Check_lunch (11,4,[1,2,3,4,2,3,3,4,1,2,3])) Print (Auto_complete ("chengdu Chongqing "," C ")) Print (Auto_complete (" chengdu chongqing "," Che ")) Print (Auto_complete (" chengdu Chongqing "," Jing ")) Print (Analysize2 (" If then ")) Print (Analysize2 (" switch case aaa ")) print ( Analysize2 ("if (AAA) &NBSP;THEN&NBSP;DO&NBSP;BBB while switch cas "))
1. spot checks (in minutes)
Problem Description:
a company due to a number of people, lunch is divided into multi-batch dining, strict requirements for each batch of dining time. and regular spot checks on the dining situation. Please write a program to achieve the spot checks.
Required Implementation functions:
void check_lunch (int num, int time,int input[], int output[])
"input" int num, total number of diners inttime, number of meals in batches char input[], dining "Output" Char output[], meal violation "back" no
Note: 3 batches of meals in the case,a meal, the correct dining situation should be the following distribution [1,2,3,1,2,3,1,2,3,1,2,3], inconsistent with the distribution is the violation, The output is to the corresponding position 0.
Example
1) input:num =,time = 3,input =[1,2,3,3,1,3,1,1,1,1,2,3]
outputs: Output= [1,2,3,0,0,3,1,0,0,1,2,3]
2) input:num = one,time = 4,intput = [1,2,3,4,2,3,3,4,1,2,3]
outputs: Output= [1,2,3,4,0,0,3,4,1,2,3]
2. input Lenovo (max. )
Problem Description:
input Lenovo function is a very useful function, please program to implement similar functions.
Required Implementation functions:
void Auto_complete (char *str, char *tmp,char *output)
"Input" char *str, candidate string char *tmp, input string "Output" int *output, associative matching string "Back" none
Note: The candidate strings are separated by spaces, and the input string matches only from the beginning of the string. The matched substring output is also separated by a space. If no successful substring is matched, an empty string is output.
Example
1) input:str = Chengdu Chongqing,tmp = C
outputs: Output= Chengdu Chongqing
2) input:str = Chengdu Chongqing,tmp = che
output:end = Chengdu
3) Input:str = beijing Nanjing,tmp = Jing
output:end =
3. Grammar Analysis (max )
Problem Description:
The compiler detects some grammatical problems of the program through parsing. It is required to implement a simple parser to determine whether the input string has a syntax combination that conforms to the requirements.
The syntax combinations that need to be judged are: if Then
if () then switch case end switch () case end
Switch () Case default end does while
Required Implementation functions:
void Analysis (char *str,int *num)
"Input" char *str, the string to be parsed "output" int num, number of matched combinations "return" none
Note: The key words in the input string are separated by a space,"if","(" ")","case" and so on are the keywords, from left to right, to find a matching combination, the combination must be separated from each other, No nesting, no cross-up.
Example
1) input:str = If then,
output:num = 1
2) input:str = switch case AAA ,
output:num = 0
3) input:str = if (AAA) then does BBB while switch CAs ,
output:num = 2
This article is from "Tech" blog, please make sure to keep this source http://freemanwest.blog.51cto.com/1137962/1657486
2012 Huawei Programming Contest algorithm implementation