例如:as[2|as[2|cd]]bb[2|a]解压缩就是as[2|as[2|cd]]bb[2|a] -> as[2|ascdcd]bb[2|a] -> asascdcdascdcdbbaa
思路:用一个栈保存‘[’出现的位置,遇到‘]’时就开始找到需要复制的串,复制的数量,弹栈
#include <iostream>
#include <vector>
using namespace std;
int main(){
string s;
cin>>s;
string re;
vector<int>ta;
for(int i=0;i<s.size();i++){
if(s[i]=='['){
ta.push_back(re.size());
re+=s[i];
}
else if(s[i]==']'){
int start=ta.back();
ta.pop_back();
int ti=0;
int j;
// cout<<re<<" ";
// cout<<start+1<<" "<<re.size()<<" ";
for(j=start+1;j<re.size()&&re[j]!='|';j++)
ti=ti*10+re[j]-'0';
// cout<<j<<" ";
string temp(re.begin()+j+1,re.end());
// cout<<temp<<endl;
re=string(re.begin(),re.begin()+start);
for(int k=0;k<ti;k++)
re+=temp;
}
else
re+=s[i];
}
cout<<re<<endl;
return 0;
}