PAT 1052 (20 points)

1052 selling cute (20 points)

Cute emoticons are usually composed of three parts: hand, eye and mouth. For simplicity, let's assume that an emoticon is output in the following format:

[left hand] ([left eye] [mouth] [right eye]) [right hand]

Now we give the optional symbol set. Please output the expression according to the user's request.

Input format:

Input the optional symbol set of hand, eye and mouth in the first three lines. Each symbol is enclosed in a bracket []. The title ensures that each set has at least one symbol and no more than 10 symbols; each symbol contains 1 to 4 non empty characters.

The next line gives a positive integer K, which is the number of user requests. Then line K, each line gives a user's symbol selection, in the order of left hand, left eye, mouth, right eye and right hand - here only the number of symbols in the corresponding set (starting from 1) is given, and the numbers are separated by spaces.

Output format:

For each user request, the generated emoticons are output on one line. If the sequence number selected by the user does not exist, are you killing me? @ / @.

Input example:

[][][o][~\][/~]  [<][>]
 [][][^][-][=][>][<][@][]
[Д][][_][ε][^]  ...
4
1 1 2 2 2
6 8 1 5 5
3 3 4 3 3
2 10 3 9 3

Output example:

(╯▽╰)<(=)/~
o(^ε^)o
Are you kidding me? @\/@

Ideas:

The difficulty of string processing is input.
It should also be noted that the '\' symbol of are you killing me? @ / @ is an escape character and needs to be written as' \ '
The user's sequence number subscript starts from 1.

Statistics:

Solution method 1:

#include <cstdio>
#include <cstring>
using namespace std;

char emoj[5][20][20],st=0,tmpc,tmpemoj[10],tmpa;
int n,a,b,c,d,e;

int main(){
    while(st<3){
        scanf("%c",&tmpc);
        if(tmpc == 10 || tmpc == 13) st++;
        if(tmpc =='['){
            tmpa = 0;
            emoj[st][0][0]++;
            while(1){
                scanf("%c",&tmpc);
                if(tmpc==']') break;
                emoj[st][emoj[st][0][0]][tmpa++]=tmpc;
            }
            emoj[st][emoj[st][0][0]][tmpa++]='\0';
        }
    }
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
        if(a<=0 || b<=0 || c<=0 || d<=0 || e<=0 || a>emoj[0][0][0] || b>emoj[1][0][0] || c>emoj[2][0][0] || d>emoj[1][0][0] || e>emoj[0][0][0])
            printf("Are you kidding me? @\\/@\n");
        else
            printf("%s(%s%s%s)%s\n",emoj[0][a],emoj[1][b],emoj[2][c],emoj[1][d],emoj[0][e]);
    }
    return 0;
}

Solution method 2:

Flexible use of regular expressions

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

char emj[5][99][99],imp[1000],tmp[100],cnt[5],tmpa;
int a,b,c,d,e,n;

int main(){
    for(int i=0;i<3;++i){
        scanf("%[^\n]%c",imp,&tmpa); //tmp is to absorb line breaks equivalent to getchar();
        for(char *p=imp;*p;p++)
            if(*p=='['){
                sscanf(p+1,"%[^]]",tmp); //Regular expressions are a great use.
                strcpy(emj[i][++cnt[i]],tmp);
            }
    }
    scanf("%d",&n);
    while(n--){
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
        if(a<=0 || b<=0 || c<=0 || d<=0 || e<=0 || a>cnt[0] || b>cnt[1] || c>cnt[2] || d>cnt[1] || e>cnt[0])
            printf("Are you kidding me? @\\/@\n"); //Note that in the upper line, a * b * c * D * e < = 0 is used to judge whether they have a method less than 0, which is not feasible
        else
            printf("%s(%s%s%s)%s\n",emj[0][a],emj[1][b],emj[2][c],emj[1][d],emj[0][e]);
    }
    return 0;
}

Solution method 3:

Carry the code of a day's heavyweight

#include <bits/stdc++.h>
using namespace std;
using gg = long long;
int main() {
    ios::sync_with_stdio(false);
    array<vector<string>, 3> e{};  //Store emoticons
    for (auto& v : e) {
        string line;
        getline(cin, line);  //Read a line of string
        // i is responsible for finding the subscript of '[', j is responsible for finding the subscript of ']'
        for (auto i = line.find('['); i != -1;) {
            auto j = line.find(']', i);
            v.push_back(line.substr(i + 1, j - i - 1));
            i = line.find('[', j);
        }
    }
    gg k, a;
    cin >> k;
    while (k--) {
        string out;  //Store expression string to output
        bool f = true;  //Indicates whether the subscript is illegal
        for (auto i = 0; i < 5; ++i) {
            cin >> a;
            // v is responsible for obtaining the subscript of which expression array a is in the hand eye mouth
            const auto& v = i == 2 ? e[2] : (i == 0 or i == 4) ? e[0] : e[1];
            if (a - 1 >= v.size()) {  //Illegal subscript
                f = false;
            } else {
                out += v[a - 1] + (i == 0 ? "(" : i == 3 ? ")" : "");
            }
        }
        f ? cout << out << '\n' : cout << "Are you kidding me? @\\/@\n";
    }
    return 0;
}

Tags: less iOS

Posted on Sat, 27 Jun 2020 03:41:07 -0400 by programmermatt