第一题:
小Q想要给他的朋友发送一个神秘字符串,但是他发现字符串的过于长了,于是小Q发明了一种压缩算法对字符串中重复的部分进行了压缩,对于字符串中连续的m个相同字符串S将会压缩为m|S(m为一个整数且1<=m<=100),例如字符串ABCABCABC将会被压缩为[3|ABC],现在小Q的同学收到了小Q发送过来的字符串,你能帮助他进行解压缩么?
思路:这里直接dfs栈模拟即可
#include<bits/stdc++.h>
using namespace std;
string strr;
int len=0;//字符串长度
int pos = 1;//遍历到字符串的位置
string dfs(){
string s="";
if(pos==len) return s;
int number = 0;
string ss="";
while(pos<len){
if(strr[pos]=='['){
++pos;
ss+=dfs();
}
//数字的处理
else if(strr[pos]>='0'&&strr[pos]<='9'){
while(strr[pos]!='|'&&pos<len){
number = number*10+(strr[pos]-'0');
pos++;
}
pos++;
}
//字母
else if(strr[pos]>='A'&&strr[pos]<='Z'){
ss+=strr[pos];
pos++;
}
else if(strr[pos]==']'){
s=ss;
while(--number){
s+=ss;
}
pos++;
return s;
}
}
return ss;
}
int main(){
cin>>strr;
len = strr.length()-1;
cout<<dfs