UVA 12662 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
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[n][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]);
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值