腾讯2020校园招聘-后台&综合-第一次笔试题目解析

[编程题]压缩算法

时间限制:C/C++ 2秒,其他语言4秒

空间限制:C/C++ 256M,其他语言512M

小Q想要给他的朋友发送一个神秘字符串,但是他发现字符串的过于长了,于是小Q发明了一种压缩算法对字符串中重复的部分进行了压缩,对于字符串中连续的m个相同字符串S将会压缩为[m|S](m为一个整数且1<=m<=100),例如字符串ABCABCABC将会被压缩为[3|ABC],现在小Q的同学收到了小Q发送过来的字符串,你能帮助他进行解压缩么? 

思路:

大模拟,递归的将所有[]区间的内容暴力转换就好了,不得不说这个题放第一个比较折磨人

#include <bits/stdc++.h>
using namespace std;

string str;
const int maxn=1e5+10;
int match[maxn];
int getint(string s){
    int len=s.size();
    int ans=0;
    for(int i=0;i<len;i++){
        ans+=(s[i]-'0')*pow(10,len-i-1);
    }
    return ans;
}
//得到l,r这个区间的串
string solve(int l,int r){
    string num="";
    int pos=l+1;
    while(pos<r&&str[pos]!='|'){
        num+=str[pos];
        pos++;
    }
    string temp="";
    pos++;
    while(pos<r){
        if(str[pos]=='['){
            int newl=pos;
            int newr=match[pos];
            //cout<<"debug "<<temp<<endl;
            temp+=solve(newl,newr);
            //cout<<"what "<<solve(newl,newr)<<endl;
            pos=match[pos]+1;
            //printf("pos %d\n",pos);
        }
        else{
            temp+=str[pos];
            pos++;
        }
        //cout<<"debug "<<temp<<endl;
    }
    int cnt=getint(num);
    //printf("cnt %d\n",cnt);
    //cout<<"debug "<<temp<<endl;
    string temp2=temp;
    for(in
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值