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]);
}
}
}
}
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]);
}
}
}
}
相关文章推荐
- 【CF 应用开发大赛】IT Share(IT分享网)
- IT职场英语,必须学会
- it人员要看饮食保健养生谚语
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- mysql 下载及安装方法
- ACM程序设计大赛题目分类
- 计算字符串最后一个单词长度
- 个人随笔——衰人自叙卷壹
- IT人员迅速提升自我效率的十大方法
- 搞IT的到底怎么了
- 史蒂夫-乔布斯十大励志名言(中英对照)
- IT大事件—分享IT界最新的IT大会——互联网大会,技术大会.................
- 大学生IT求职,不缺机会,积累项目实战经验是关键
- 职业规划和技术发展
- 奥运对中国IT行业的影响
- 某大型IT公司招网络工程师认证试题精选(要求:CCNA或HCNE以上)
- 全国信息化工程师院校IT双证书职业课程认证