csuoj 1334: 好老师

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1334

1334: 好老师

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 525  Solved: 235
[Submit][Status][Web Board]

Description

我想当一个好老师,所以我决定记住所有学生的名字。可是不久以后我就放弃了,因为学生太多了,根本记不住。但是我不能让我的学生发现这一点,否则会很没面子。所以每次要叫学生的名字时,我会引用离他最近的,我认得的学生。比如有10个学生:

A ? ? D ? ? ? H ? ?

想叫每个学生时,具体的叫法是:

位置

叫法

1

A

2

right of A (A右边的同学)

3

left of D (D左边的同学)

4

D

5

right of D (D右边的同学)

6

middle of D and H (D和H正中间的同学)

7

left of H (H左边的同学)

8

H

9

right of H (H右边的同学)

10

right of right of H (H右边的右边的同学)

Input

输入只有一组数据。第一行是学生数n(1<=n<=100)。第二行是每个学生的名字,按照从左到右的顺序给出,以空格分隔。每个名字要么是不超过3个英文字母,要么是问号。至少有一个学生的名字不是问号。下一行是询问的个数q(1<=q<=100)。每组数据包含一个整数p(1<=p<=n),即要叫的学生所在的位置(左数第一个是位置1)。

Output

对于每个询问,输出叫法。注意"middle of X and Y"只有当被叫者有两个最近的已知学生X和Y,并且X在Y的左边。

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

HINT

分析:

左右逐个扩展,知道找到!?时。

一开始wa了一次,因为忘了考虑这种数据:? ? ? ? asdf(就是当字符开始就是?的时候)

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7 #include <stdlib.h>
 8 #include <queue>
 9 #include <stack>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <vector>
15 #pragma comment(linker, "/STACK:1024000000,1024000000")
16 #pragma warning(disable:4786)
17 
18 using namespace std;
19 
20 const int INF = 0x3f3f3f3f;
21 const int MAX = 1000 + 10;
22 const double eps = 1e-8;
23 const double PI = acos(-1.0);
24 
25 string str;
26 map<int , string>ma;
27 int main()
28 {
29     int n , i;
30     while(~scanf("%d",&n))
31     {
32         for(i = -110;i <= 210;i ++)
33             ma[i] = "?";
34         for(i = 1;i <= n;i ++)
35         {
36             cin >> str;
37             ma[i] = str;
38         }
39         int m;
40         scanf("%d",&m);
41         int temp;
42         while(m --)
43         {
44             scanf("%d",&temp);
45             if(ma[temp] != "?")
46                 cout << ma[temp] << endl;
47             else
48             {
49                 int j;
50                 for(j = 1; ; j ++)
51                 {
52                     if(ma[temp + j] != "?" && ma[temp - j] != "?")
53                     {
54                         cout << "middle of " << ma[temp - j] << " and " << ma[temp + j] <<endl;
55                         break;
56                     }
57                     else if(ma[temp + j] != "?" && j + temp <= n)
58                     {
59                         for(int k = 0;k < j ;k ++)
60                             cout << "left of ";
61                         cout << ma[temp + j] << endl;
62                         break;
63                     }
64                     else if(ma[temp - j] != "?" && temp - j >= 0)
65                     {
66                         for(int k = 0;k < j ;k ++)
67                             cout << "right of ";
68                         cout << ma[temp - j] << endl;
69                         break;
70                     }
71                 }
72             }
73         }    
74     }
75     return 0;
76 }
View Code
原文地址:https://www.cnblogs.com/jeff-wgc/p/4456465.html