Problem G
Good Teacher
I want to be a good teacher, so at least I need to remember all the student names. However, there are too many students, so I failed. It is a shame, so I don't want my students to know this. Whenever I need to call someone, I call his CLOSEST student instead. For example, there are 10 students:
A ? ? D ? ? ? H ? ?
Then, to call each student, I use this table:
Pos | Reference |
1 | A |
2 | right of A |
3 | left of D |
4 | D |
5 | right of D |
6 | middle of D and H |
7 | left of H |
8 | H |
9 | right of H |
10 | right of right of H |
Input
There is only one test case. The first line contains n, the number of students (1<=n<=100). The next line contains n space-separated names. Each name is either ? or a string of no more than 3 English letters. There will be at least one name not equal to ?. The next line contains q, the number of queries (1<=q<=100). Then each of the next q lines contains the position p (1<=p<=n) of a student (counting from left).
Output
Print q lines, each for a student. Note that "middle of X and Y" is only used when X and Y are both closest of the student, and X is always to his left.
Sample Input
10 A ? ? D ? ? ? H ? ? 4 3 8 6 10
Output for the Sample Input
left of D H middle of D and H right of right of H
The Ninth Hunan Collegiate Programming Contest (2013) Problemsetter: Rujia Liu Special Thanks: Feng Chen, Md. Mahbubul Hasan
打好基础,顺序查找,没有什么高端算法,就是希望在某些情况下少用各种break ,多用函数return ,这样好一点,少犯些错误。
#include <iostream> #include <stdio.h> #include <queue> #include <stdio.h> #include <string.h> #include <vector> #include <queue> #include <set> #include <algorithm> #include <map> #include <stack> #include <math.h> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std ; typedef long long LL ; int N ; string str[108] ; void gao(int id){ if(str[id]!="?"){ cout<<str[id]<<endl ; return ; } int L ,R ; L=R=1 ; int left=id-1 ; int right=id+1 ; str[0]=str[N+1]="?" ; while(left>=1&&str[left]=="?"){ R++ ; left-- ; } while(right<=N&&str[right]=="?"){ L++ ; right++ ; } if(L==R&&str[left]!="?"&&str[right]!="?"){ printf("middle of %s and %s ",str[left].c_str(),str[right].c_str()) ; return ; } if((str[left]!="?"&&str[right]!="?"&&L>R)||str[right]=="?"){ for(int i=1;i<=R;i++) printf("right of ") ; cout<<str[left]<<endl ; return ; } if((str[left]!="?"&&str[right]!="?"&&L<R)||str[left]=="?"){ for(int i=1;i<=L;i++) printf("left of ") ; cout<<str[right]<<endl ; return ; } } int main(){ int M ,id ; cin>>N ; for(int i=1;i<=N;i++) cin>>str[i] ; cin>>M ; while(M--){ cin>>id ; gao(id) ; } return 0 ; }