PAT

A1164 Good in C (20 分)

When your interviewer asks you to write "Hello World" using C, can you do as the following figure shows?

HWC.jpg

Input Specification:

Each input file contains one test case. For each case, the first part gives the 26 capital English letters A-Z, each in a 7×5 matrix of C's and .'s. Then a sentence is given in a line, ended by a return. The sentence is formed by several words (no more than 10 continuous capital English letters each), and the words are separated by any characters other than capital English letters.

It is guaranteed that there is at least one word given.

Output Specification:

For each word, print the matrix form of each of its letters in a line, and the letters must be separated by exactly one column of space. There must be no extra space at the beginning or the end of the word.

Between two adjacent words, there must be a single empty line to separate them. There must be no extra line at the beginning or the end of the output.

Sample Input:

..C..
.C.C.
C...C
CCCCC
C...C
C...C
C...C
CCCC.
C...C
C...C
CCCC.
C...C
C...C
CCCC.
.CCC.
C...C
C....
C....
C....
C...C
.CCC.
CCCC.
C...C
C...C
C...C
C...C
C...C
CCCC.
CCCCC
C....
C....
CCCC.
C....
C....
CCCCC
CCCCC
C....
C....
CCCC.
C....
C....
C....
CCCC.
C...C
C....
C.CCC
C...C
C...C
CCCC.
C...C
C...C
C...C
CCCCC
C...C
C...C
C...C
CCCCC
..C..
..C..
..C..
..C..
..C..
CCCCC
CCCCC
....C
....C
....C
....C
C...C
.CCC.
C...C
C..C.
C.C..
CC...
C.C..
C..C.
C...C
C....
C....
C....
C....
C....
C....
CCCCC
C...C
C...C
CC.CC
C.C.C
C...C
C...C
C...C
C...C
C...C
CC..C
C.C.C
C..CC
C...C
C...C
.CCC.
C...C
C...C
C...C
C...C
C...C
.CCC.
CCCC.
C...C
C...C
CCCC.
C....
C....
C....
.CCC.
C...C
C...C
C...C
C.C.C
C..CC
.CCC.
CCCC.
C...C
CCCC.
CC...
C.C..
C..C.
C...C
.CCC.
C...C
C....
.CCC.
....C
C...C
.CCC.
CCCCC
..C..
..C..
..C..
..C..
..C..
..C..
C...C
C...C
C...C
C...C
C...C
C...C
.CCC.
C...C
C...C
C...C
C...C
C...C
.C.C.
..C..
C...C
C...C
C...C
C.C.C
CC.CC
C...C
C...C
C...C
C...C
.C.C.
..C..
.C.C.
C...C
C...C
C...C
C...C
.C.C.
..C..
..C..
..C..
..C..
CCCCC
....C
...C.
..C..
.C...
C....
CCCCC
HELLO~WORLD!

Sample Output:

C...C CCCCC C.... C.... .CCC.
C...C C.... C.... C.... C...C
C...C C.... C.... C.... C...C
CCCCC CCCC. C.... C.... C...C
C...C C.... C.... C.... C...C
C...C C.... C.... C.... C...C
C...C CCCCC CCCCC CCCCC .CCC.

C...C .CCC. CCCC. C.... CCCC.
C...C C...C C...C C.... C...C
C...C C...C CCCC. C.... C...C
C.C.C C...C CC... C.... C...C
CC.CC C...C C.C.. C.... C...C
C...C C...C C..C. C.... C...C
C...C .CCC. C...C CCCCC CCCC.

题目大意:

题目会给出A~Z的26个字母的字符串组合,然后最后一行会给出一个英文字符串str,str中的大写的英文字符用所给的字母组合打印出来,每个字母要留一个空格,其他字符不用打印,用空行代替。

思路

  1. unordered_map<char, vector<string> >存储题目中所给出的字母组合,然后跟着str中出现的字符打印即可
  2. 注意点:

    • 在读入输入的句子str时,直接读入一整行getline(cin, str)(因为str中间可能会出现空格,用普通方法处理可能会导致没有把句子输入完整)
    • 最后一个word可能会打印漏掉(当str的末尾不是以除大写的英文字母的其他字符结尾时),我的处理方法是在str的结尾多加一个其他字符。

AC代码

#include <bits/stdc++.h>
using namespace std;

unordered_map<char, vector<string> > alphabet;

int main() {
    string line;
    for (int i = 0; i < 26; ++i) {
        for (int j = 0; j < 7; ++j) {
            cin >> line;
            alphabet['A' + i].emplace_back(line);
        }
    }
    string res;
    getchar();
    getline(cin, res);
    res += "!";
    vector<char> print;
    bool isbr = false;
    for (char &c : res) {
        if ('A' <= c && c <= 'Z') {
            print.emplace_back(c);
        } else {
           if (print.empty()) continue;
            if (isbr) printf("\n");
            if (!isbr) isbr = true;
            for (int i = 0; i < 7; ++i) {
                bool isblank = false;
                for (char &t : print) {
                    if (isblank) printf(" ");
                    if (!isblank) isblank = true;
                    cout << alphabet[t][i];
                }
                printf("\n");
            }
            print = vector<char>();
        }
    }
    return 0;
}
This is just a placeholder img.