第一题

Q:

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

输入描述:

输入第一行包含一个字符串s,代表压缩后的字符串。

S的长度<=1000;

S仅包含大写字母、[、]、|;

解压后的字符串长度不超过100000;

压缩递归层数不超过10层;

输出描述:

输出一个字符串,代表解压后的字符串。

输入例子1:

HG[3|B[2|CA]]F

输出例子1:

HGBCACABCACABCACAF

例子说明1:

HG[3|B[2|CA]]F−>HG[3|BCACA]F−>HGBCACABCACABCACAF

A:

用栈:

int main(){
string s;
cin>>s;
stack<char> sta;
int i=0;
while(i<s.size()){
if(s[i]!=']'){
sta.push(s[i++]);
}
else{//s[i]==']'
string tmp="";
while(sta.top()!='|'){
tmp+=sta.top();
sta.pop();
}
reverse(tmp.begin(),tmp.end());
sta.pop(); //pop掉'|'
string str=tmp;
tmp="";
while(sta.top()!='['){
tmp+=sta.top();
sta.pop();
}
reverse(tmp.begin(),tmp.end());
int num=stoi(tmp);
sta.pop(); //pop '['
tmp="";
while(num>0){
tmp+=str;
num--;
}
for(char& c:tmp){
sta.push(c);
}
++i;
}
}
string res="";
while(!sta.empty()){
res+=sta.top();
sta.pop();
}
reverse(res.begin(),res.end());
cout<<res<<endl;
return 0;
}

递归:

string func(const string& s,int& index){
string res="";
while(index<s.size() and s[index]!=']'){
if(s[index]<='Z' and s[index]>='A'){
res+=s[index++];
}
else{ //s[index]=='['
index++;//指向第一个数字
int tmp=s.find(index,'|');
int num=stoi(s.substr(index,tmp-index));
index++;//指向第一个字母
string str=func(s,index);
index++;
while(num>0){
res+=str;
num--;
}
}
}
return res;
}
int main(){
int i=0;
string s="";
cin>>s;
cout<<func(s,i)<<endl;
return 0;
}

第二题

Q:

小Q在周末的时候和他的小伙伴来到大城市逛街,一条步行街上有很多高楼,共有n座高楼排成一行。

小Q从第一栋一直走到了最后一栋,小Q从来都没有见到这么多的楼,所以他想知道他在每栋楼的位置处能看到多少栋楼呢?(当前面的楼的高度大于等于后面的楼时,后面的楼将被挡住)

输入描述:

输入第一行将包含一个数字n,代表楼的栋数,接下来的一行将包含n个数字wi(1<=i<=n),代表每一栋楼的高度。

1<=n<=100000;

1<=wi<=100000;

输出描述:

输出一行,包含空格分割的n个数字vi,分别代表小Q在第i栋楼时能看到的楼的数量。

输入例子1:

6

5 3 8 3 2 5

输出例子1:

3 3 5 4 4 4

例子说明1:

当小Q处于位置3时,他可以向前看到位置2,1处的楼,向后看到位置4,6处的楼,加上第3栋楼,共可看到5栋楼。当小Q处于位置4时,他可以向前看到位置3处的楼,向后看到位置5,6处的楼,加上第4栋楼,共可看到4栋楼。

A:

#pragma warning(disable:4996)
#include <iostream>
#include<istream>
#include <string>
#include <cctype>
#include<vector>
#include<list>
#include<cstring>
#include<random>
#include<typeinfo>
#include<set>
#include<map>
#include<deque>
#include<regex>
#include<sstream>
#include<cstdlib>
#include<queue>
#include<stdlib.h>
#include<stdio.h>
#include<stack>
#include<algorithm>
#include<thread>
#include<mutex>
#include<assert.h>
#include<fstream>
#include<unordered_map>
#include<unordered_set>
using namespace std; int main(){
int N;
cin>>N;
vector<int> vec(N);
int i=0;
while(i<N and cin>>vec[i++]){;}
stack<int> sta;
vector<int> look_left(N,0);
//向左看,不包括所在的楼
for(int i=1;i<N;++i){
if(sta.empty() or sta.top()>vec[i-1]){
sta.push(vec[i-1]);
}
else{
while(!sta.empty() and sta.top()<=vec[i-1]){
sta.pop();
}
sta.push(vec[i-1]);
}
look_left[i]=sta.size();
}
//向右看,不包括所在的楼
vector<int> look_right(N,0);
sta=stack<int>();
for(int i=N-2;i>=0;--i){
if(sta.empty() or sta.top()>vec[i+1]){
sta.push(vec[i+1]);
}
else{
while(!sta.empty() and sta.top()<=vec[i+1]){
sta.pop();
}
sta.push(vec[i+1]);
}
look_right[i]=sta.size();
}
for(int i=0;i<N;++i){
cout<<1+look_left[i]+look_right[i]<<" ";
}
cout<<endl;
}

4

#pragma warning(disable:4996)
#include <iostream>
#include<istream>
#include <string>
#include <cctype>
#include<vector>
#include<list>
#include<cstring>
#include<random>
#include<typeinfo>
#include<set>
#include<map>
#include<deque>
#include<regex>
#include<sstream>
#include<cstdlib>
#include<queue>
#include<stdlib.h>
#include<stdio.h>
#include<stack>
#include<algorithm>
#include<thread>
#include<mutex>
#include<assert.h>
#include<fstream>
#include<unordered_map>
#include<unordered_set>
#include<limits.h>
using namespace std; int main(){
int n;
cin>>n;
vector<int> company(n),gym(n);
for(int i=0;i<n;++i){
cin>>company[i];
}
for(int i=0;i<n;++i){
cin>>gym[i];
}
vector<vector<int> >dp(n,vector<int>(3,INT_MAX));//dp[i][0]截止第i天,第i天去公司最少休息天数,dp[i][1]表示第i天去健身
//dp[i][2]是截止到第i天,第i天休息的最少休息天数
if(company[0]==1){
dp[0][0]=0;
}
if(gym[0]==1){
dp[0][1]=0;
}
dp[0][2]=1;
for(int i=1;i<n;++i){
if(company[i]){//上班
dp[i][0]=min(dp[i-1][2],dp[i-1][1]);
}
if(gym[i]){//锻炼
dp[i][1]=min(dp[i-1][0],dp[i-1][2]);
}
dp[i][2]=min(min(dp[i-1][2],dp[i-1][1]),dp[i-1][0])+1;
}
cout<<*min_element(dp[n-1].begin(),dp[n-1].end());
}

5

#pragma warning(disable:4996)
#include <iostream>
#include<istream>
#include <string>
#include <cctype>
#include<vector>
#include<list>
#include<cstring>
#include<random>
#include<typeinfo>
#include<set>
#include<map>
#include<deque>
#include<regex>
#include<sstream>
#include<cstdlib>
#include<queue>
#include<stdlib.h>
#include<stdio.h>
#include<stack>
#include<algorithm>
#include<thread>
#include<mutex>
#include<assert.h>
#include<fstream>
#include<unordered_map>
#include<unordered_set>
#include<limits.h>
using namespace std; struct range{
int le;
int ri;
range():le(0),ri(0){}
// range(int a,int b):le(a),ri(b){}
bool operator<(const range& x){
return le<x.le or (le==x.le and ri<x.ri);
}
}; int main(){
int n,l;
cin>>n>>l;
if(n<=0){
cout<<-1;
return 0;
}
vector<range> data(n);
for(int i=0;i<n;++i){
cin>>data[i].le>>data[i].ri;
}
sort(data.begin(),data.end());
if(data[0].le!=0){
cout<<-1;
return 0;
}
int cur_reach=data[0].ri,cur_base=data[0].le;
int i=1,cnt=1;
while(data[i].le==0){
cur_reach=max(cur_reach,data[i++].ri);
}
while(i<n and cur_reach<l){
int farthest=0,base;
while(i<n and data[i].le<=cur_reach){
if(data[i].ri>farthest){
farthest=data[i].ri;
base=data[i].le;
}
++i;
}
if(farthest<cur_reach){
cout<<-1;
return 0;
}
cur_base=base;
cur_reach=farthest;
cnt++;
}
if(cur_reach>=l){
cout<<cnt;
}
else{
cout<<-1;
}
return 0;
}

牛客腾讯2020校园招聘-后台&综合-第一次笔试的更多相关文章

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

    对数据结构和算法感兴趣的可以关注一下https://github.com/MCQ1999/Datastructure_Algorithm_Solutions,分享算法题的解题思路和代码~ 1.压缩算法 ...

  2. 前端 9.16腾讯-2019校园招聘(正式卷)编程题题解(js)

    第一题 和谐的数字 牛牛很喜欢研究数字.一天,他发明了一种数字,叫做“和谐的数字”. 和谐的数字定义如下: 定义S(n)为数字n各位数字之和,如果S(n)能够整除n,那么就称n为一个“和谐的数字”. ...

  3. 牛客网-C++-2020.9.2

    1. for循环语句能够被改写成(D)语句 A. 复合 B. if C. switch D. while 解析: for循环可以写成while控制循环的次数,同时也可以被改写成do while语句 2 ...

  4. Solution -「LOCAL」「cov. 牛客多校 2020 第五场 C」Easy

    \(\mathcal{Description}\)   Link.(完全一致)   给定 \(n,m,k\),对于两个长度为 \(k\) 的满足 \(\left(\sum_{i=0}^ka_i=n\r ...

  5. Solution -「LOCAL」「cov. 牛客多校 2020 第三场 I」礼物

    \(\mathcal{Description}\)   给定排列 \(\{a_n\}\),求字典序第 \(K\) 大的合法排列 \(\{b_n\}\).称一个排列 \(\{p_n\}\) 合法,当且仅 ...

  6. 微软2016校园招聘4月在线笔试 A FontSize

    题目链接:http://hihocoder.com/problemset/problem/1288 分析:题目中所求的是最大的FontSize(记为S),其应该满足P*[W/S]*[H/S] > ...

  7. 微软2016校园招聘4月在线笔试 ABC

    题目链接:http://hihocoder.com/contest/mstest2016april1/problems 第一题:输入N,P,W,H,代表有N段文字,每段有ai个字,每行有⌊W/S⌋个字 ...

  8. hihocoder 1288 : Font Size (微软2016校园招聘4月在线笔试)

    hihocoder 1288 笔试第一道..wa了好几次,也是无语..hihocoder错了不会告诉你失败的时候的测试集,这样有时候就很烦.. 遍历所有的字体,从min(w,h)开始逐渐变小开始遍历. ...

  9. 微软2016校园招聘4月在线笔试 hihocoder 1289 403 Forbidden

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 Little Hi runs a web server. Sometimes he has to deny acces ...

随机推荐

  1. jQuery 判断页面上下滚动

    var t = 0, b = 0; $(window).scroll(function(){ t = $(this).scrollTop(); if(b < t){ console.log('向 ...

  2. dev 控件用法2 之repositoryItemSearchLookUpEdit

    repositoryItemSearchLookUpEdit var y = userinfo.Select.ToList( a => new { userid = a.userid, code ...

  3. java开发就业招聘管理系统 ssh源码

    开发环境:    Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MySql数据库 此项目分为 用户 企业  管理员三种角色 运行效果图

  4. 洛谷P1551 亲戚 (并查集模板题)

    链接 https://www.luogu.org/problemnew/show/P1551 代码 #include<bits/stdc++.h> using namespace std; ...

  5. 《深入理解java虚拟机》读书笔记八——第九章

    第九章 类加载及执行子系统的案例与实战 Q:如果有10个WEB应用程序都是用Spring来进行组织管理的话,可以把Spring放到Common或Shared目录下(Tomcat5.0)让这些程序共享. ...

  6. ALSA lib-io plugin

    https://www.alsa-project.org/alsa-doc/alsa-lib/pcm_external_plugins.html External Plugin: I/O Plugin ...

  7. vue使用kkfileview文件预览功能

    1.环境要求: java 1.8+ 2.部署运行: 本机以及虚拟机上运行: 1.从https://gitee.com/kekingcn/file-online-preview/releases地址下载 ...

  8. QT版本

    最近在linux下安装qt:发现主要的问题是qt的版本问题:下面来谈谈各个版本的理解 Qt 的版本是按照不同的图形系统来划分的,目前分为五个版本: Win: 适用于Miccrosoft Windows ...

  9. R parallel包学习笔记2

    这个部分我在datacamp上面学习笔记,可视化的性能很差,使用的函数也很少. 可以参考一下大佬的博客园个人感觉他们讲的真的很详细 https://cosx.org/2016/09/r-and-par ...

  10. RMQ(区间最值问题)

    问题: RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大) ...