P1928 外星密码(递归)

题目传送门

题意: 给你一个压缩过的密码,要你还原。压缩规则:相同的串压缩成一个小串,比如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();
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值