C++题目:Egg Drop

题目描述

For years, before computers, Merriam Webster maintained a Backward Index of all of the words in the English language. They had all of the words on cards, one word per card, backwards. The cards were alphabetized. This was very useful, before computers, to determine such things as ‘How many words end with ‘TION’?’ Given a list of words consisting of only capital letters, create a Backward Index by reversing them and printing the reversals in alphabetical order.

输入

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input will contain an integer n (1≤n≤1,000) indicating the number of words. On the following n lines will be the words, one per line. The words will be from 1 to 100 letters long. The words will consist of only capital letters, and there will be no spaces or blank lines.

输出

Output the words, reversed and sorted, one word per line.

样例输入

2 10

4 SAFE

7 BROKEN

样例输出

5 6

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<cstring>

using namespace std;

int main()
{
    int n , k ,f ,x1 = 1, x2; //x1为鸡蛋掉落可能安全的最高楼层,x2为鸡蛋掉落可能碎的最低楼层。
    string a= "SAFE" , b = "BROKEN" , c;
    cin >> n >> k;
    x2 = k;//重要重要。
    for(int i = 1;i <= n;++i)
    {
        cin >> f;
        cin >> c;
        if(c == a && f >= x1)
        {
            x1 = f;
        }
        else if(c == b && f <= x2)
        {
            x2 = f;
        }
    }
    if(x1 + 1== x2)//重要重要
    {
        cout << x2 << " " << x1 << endl;
    }
    else
    {
        cout << x1 + 1 << " " << x2 - 1 << endl;
    }
    return 0;
}