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
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
Sample Outputleft of D
H
middle of D and H
right of right of H
题意:
找离字母最近的位置来表示此位置的所在地方,题目的a string of no more than 3 English letters,我认为应该是说有可能是字符串吧,不像样例的单个字符。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
#define T 1000100
typedef long long ll;
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
int n,m,t,i,j,k;
char str[500][5];
while(~scanf("%d",&n))
{
for(i=1;i<=n;++i){
scanf(" %s",&str[i]);
}
scanf("%d",&m);
int f1,f2,c1,c2;
for(i=0;i<m;++i){
scanf("%d",&k);
f1 = f2 = -1;
c1 = k-1,c2 = k+1;
if(strcmp(str[k],"?")!=0){
printf("%s\n",str[k]);
}
else {
while(c1>0){
if(strcmp(str[c1],"?")!=0){
f1 = c1;
break;
}
c1--;
}
while(c2<=n){
if(strcmp(str[c2],"?")!=0){
f2 = c2;
break;
}
c2++;
}
if(f1!=-1&&f2!=-1){//两边都找到名字
if(k-f1==f2-k){//距离相等
printf("middle of %s and %s\n",str[f1],str[f2]);
}
else if(k-f1<f2-k){//左边距离小
int tmp = k - f1;
while(tmp--){
printf("right of ");
}
printf("%s\n",str[f1]);
}
else if(k-f1>f2-k){//右边距离小
int tmp = f2 - k;
while(tmp--){
printf("left of ");
}
printf("%s\n",str[f2]);
}
}
else if(f1==-1){//只找到右边
int tmp = f2 - k;
while(tmp--){
printf("left of ");
}
printf("%s\n",str[f2]);
}
else {//只找到左边
int tmp = k - f1;
while(tmp--){
printf("right of ");
}
printf("%s\n",str[f1]);
}
}
}
}
return 0;
}