#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;
}
腾讯2020届校园招聘后台开发方向笔试题-字符串的解密
最新推荐文章于 2021-12-26 19:52:53 发布