PAT 1052 卖个萌 (字符串 难度2) - 详细题解

在这里插入图片描述
题本身并不是很难, 只是其中涉及的知识, 如果学习时候不细心的话…还真不一定知道
比如题上的这些特殊字符, 不能够用一个char来存, 必须用char数组或者是string来存
主要就是输入有点麻烦, 但语言用熟练的话也就好了

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const LL maxn = 8;

int main()
{
    vector<vector<string> > organ; //一个特殊字符视为一个字符串
    for(int i = 0; i < 3; i++){
        string input;
        vector<string> curRow;

        getline(cin, input);
        for(int j = 0, k = 0; j < input.length(); j++){
            if(input[j]=='['){
                while(k++ < input.length()) //向后遍历提取字符
                    if(input[k] == ']'){
                        curRow.push_back(input.substr(j+1, k-j-1));
                        break;
                    }
            }
        }
        organ.push_back(curRow); //当前行提取出的字符存入
    }

    int k, q[12];
    cin >> k;
    for(int i = 0; i < k; i++){
        for(int j = 1; j <= 5; j++)
            cin >> q[j];
        if(q[1]>organ[0].size() || q[5]>organ[0].size() || q[2]>organ[1].size() || q[4]>organ[1].size() || q[3]>organ[2].size()
            || q[1]<1 || q[2]<1 || q[3]<1 || q[4]<1 || q[5]<1)
            cout << "Are you kidding me? @\\/@" <<endl;
        else
            printf("%s(%s%s%s)%s\n",organ[0][q[1]-1].c_str(),organ[1][q[2]-1].c_str(),organ[2][q[3]-1].c_str(),organ[1][q[4]-1].c_str(),organ[0][q[5]-1].c_str());
    }

    return 0;
}
cpp

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/84841870