题目传送门
题意: 给你一个压缩过的密码,要你还原。压缩规则:相同的串压缩成一个小串,比如CBCB就压缩成[2CB],ABCABCABC就是[3ABC],可能有多重压缩。
**思路: ** 起初是想模拟,但是要处理很多层,并不好做,然后想到递归,但是如果整个串一起处理有些复杂,我在题解里看到一种巧妙的方法。我们每次碰到’[’,就说明遇到一个压缩串,然后这个‘[’之后必然会跟一个数字,我们可以单独把它提出来,再看后面那个串,最后把需要加的加上去就行了。
代码:
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=1e5+5;
const int inf=0x7fffffff;
const int mod=998244353;
const double eps=1e-6;
const double PI=acos(-1);
string dfs()
{
string s;int t;string p;
char x;
while(cin>>x)
{
if(x=='[')
{
cin>>t;
p=dfs();
while(t--)
s+=p;
}
else
{
if(x==']')
return s;
else
s+=x;
}
}
return s;
}
signed main()
{
cout<<dfs();
}