网易游戏2016校园招聘“游戏研发&平台开发”在线笔试——B题 Best Compression Algorithms

这篇博客主要分析了网易游戏2016年校园招聘中‘游戏研发&平台开发’在线笔试的B题,题目涉及Best Compression Algorithms。虽然博主未参与实际笔试,但提供了问题解析,采用模拟和递归降维的方法,指出最坏情况下的时间复杂度为O(N^2),由于字符串长度限制在100以内,因此复杂度在可接受范围内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意,lz并没有参加在线笔试,只是拿来练习一下,由于在hihocoder上并不能提交,只能用样例测试,所以不保证答案完全正确

题目:http://hihocoder.com/contest/ntest2015septdev/problem/2

分析:模拟题,递归降解即可,最坏复杂度为O(N^2),串的长度不超过100,所以复杂度可以接受

#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

const char* FindMatchedRight(const char* p)
{
	int left = 1;
	for(++p; ; ++p){
		if(*p == '(') ++left;
		else if(*p == ')' && !--left) break;
	}
	return p;
}
int GetOriginalLength(const string& s)
{
	int tot = 0;
	const char *t = s.c_str(), *p = t, *q;
	for(; *p; p = q){
		if(isupper(*p)){//*p = 'X'
			q = p + 1;
			if(!isdigit(*q)) ++tot;
			else{//it is like X123 here
				for(++q; isdigit(*q); ++q) ;
				//now *q is not a digit
				tot += atoi(p + 1);
			}
		}
		else{//*p = '(', it is like (ABC)123 here
			q = FindMatchedRight(p);
			//*q = ')' now, find out patten length
			int patlen = GetOriginalLength(s.substr(p + 1 - t, q - p - 1));
			//find out the repeats of the patten
			const char* h = q+1;//h points to the first digit now
			for(q = h+1; isdigit(*q); ++q) ;
			tot += patlen * atoi(h);
		}
	}
	return tot;
}

int main()
{
	int test;
	string s;
	for(cin >> test; test--; ){
		cin >> s;
		cout << GetOriginalLength(s) << "\n";
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值