CDZSC_2016寒假个人赛(2)-G(模拟)

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 ? ? 

10

Sample Output

left of D 

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值