您的位置:首页 > 编程语言 > Go语言

UVA 12662 Good Teacher

2016-05-28 21:40 543 查看
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

Sample Output

left of D

H

middle of D and H
right of right of H

题目意思应该都能看懂 

思路:

 从查询的位置分别向左向右找第一个不是 ? 的名字

然后比较 left 和right的大小 再根据下面几种情况分别讨论

我分了这些情况来处理

1. A ? ? ? D ? ? ?H  比较正常的一种情况

2.? ? ? A ? ? ? ? ? 这就是一种不太正常的情况  开头和结尾也是 ?

需要对left和right 进行特别的处理

代码如下

#include<cstdio>

#include<cstring>

using namespace std;

char name[105][5];

int dp[105];

int min(int a,int b){
return a<b?a:b;

}

int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(name,0,sizeof(name));
memset(dp,105,sizeof(dp));
for(int i=1;i<=n;i++){
scanf("%s",name[i]);
}
int m;
scanf("%d",&m);
while(m--){
int q;
scanf("%d",&q);
if(name[q][0]!='?'){
printf("%s\n",name[q]);
continue;
}
//A ? ? D ? ? ? H ? ?
int left=0,right=0;
for(int i=q-1;i>0;i--){
if(name[i][0]=='?')
left++;
else
break;
if(i==1&&name[1][0]=='?') left=1000;
}

for(int i=q+1;i<=n;i++){
if(name[i][0]=='?')
right++;
else
break;
if(i==n&&name
[0]=='?') right=1000;
}

if(q==n) right=1000;
if(q==1) left=1000;
if(right==left){
printf("middle of %s and %s\n",name[q-left-1],name[q+right+1]);
continue;
}

if(right>left){
for(int i=0;i<=left;i++)
printf("right of ");
printf("%s\n",name[q-left-1]);
}
if(right<left){
for(int i=0;i<=right;i++)
printf("left of ");
printf("%s\n",name[q+right+1]);
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva ACM it