腾讯2020届校园招聘后台开发方向笔试题-字符串的解密

#include<iostream>
#include<string>
#include<stack>
int main() {
    std::string str1;
    std::string temp_stack;
    std::stack<char> stack1;
    std::string temp;
    std::cin >> str1;//get input string
    int len = str1.size();
    for (int i = 0; i < len; i++) {
        if (stack1.empty()&&str1[i]!='[') {
            std::cout << str1[i];
        }
        //左括号和|和字母入栈
        else if(str1[i] == '['||str1[i] =='|'||(str1[i]<='Z'&&str1[i]>='A')||(str1[i]<'9'&&str1[i]>'0')) {
            stack1.push(str1[i]);
        }//右括号
        else if (str1[i] == ']'){
            int count=0;
            while (stack1.top()!='['){
                if ('A' <= stack1.top() <= 'Z') {
                    temp+= stack1.top();
                    stack1.pop();//弹出元素
                }
                if (stack1.top() == '|'){
                    stack1.pop();
                    count = stack1.top() - '0';
                    stack1.pop();
                }
            }
            stack1.pop();//弹出'['括号
            //判断栈是否为空
            for (int i = 0;i<count;i++) {
                temp_stack+=temp;
            }
            if (stack1.empty()) {
                for (int i=temp_stack.size(); i--; i >= 0) {
                    std::cout << temp_stack[i];
                }
            }
            //入栈
            else {
                for (int i =temp.size()-1;i>=0;i--) {
                    stack1.push(temp_stack[i]);
                }
                temp_stack.clear();//清空temp
            }
        }
    }
    system("pause");
    return 1;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值