小红书C++开发笔试8.19AK

第一题背单词

第一题用一个哈希表存每个单词对应的次数,维护一个count变量记录当前所需次数,只要当前单词次数大于count,count自增,然后用set将该单词记录避免重复统计,最后输出count

#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include<unordered_set>
using namespace std; 

int main(){
	int n;
	cin>>n;
	string s;
	int count = 1;
	unordered_map<string, int> map;
	unordered_set<string> isRem;
	for(int i=0;i<n;i++){
		cin>>s;
		map[s]++;
		if(map[s]>=count){
			if(isRem.count(s)==0){
				isRem.insert(s);
				count++;
			}
		}
	}
	cout<<count-1<<endl;
}

第二题回文串

这题有点脑筋急转弯,其实只要将m和w先拆开,然后左右指针判断即可

#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include<unordered_set>
using namespace std; 

void check(string s){
	int l = 0, r = s.size() - 1;
	while(l < r){
		if(s[l]==s[r]){
			l++;r--;
		}
		else if(s[l]=='b'||s[l]=='p'||s[l]=='q'||s[l]=='d'){
			if(s[r]=='b'||s[r]=='p'||s[r]=='q'||s[r]=='d'){
				l++;r--;
			}
			else{
				cout<<"NO"<<endl;
				return;
			}
		}
		else if(s[l]=='n'||s[l]=='u'){
			if(s[r]=='n'||s[r]=='u'){
				l++;r--;
			}
			else{
				cout<<"NO"<<endl;
				return;
			}
		}
		else{
			cout<<"NO"<<endl;
			return;
		}
	}
	cout<<"YES"<<endl;
}

int main(){
	int n;
	cin>>n;
	string s;
	for(int i=0;i<n;i++){
		cin>>s;
		string str = "";
		for(char ch:s){
			if(ch=='w'){
				str += "vv";
			}
			else if(ch=='m'){
				str += "nn";
			}
			else str.push_back(ch);
		}
		check(str);
	}
}

第三题旅游景点

只能经过三个城市,暴力dfs即可,有点坑的是不能用邻接矩阵表示城市间所需时间,这样好像会爆内存,但是赛码网不会提醒,得去提交记录里面看内存记录

#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include<unordered_set>
using namespace std; 

int main(){
	int n,m;
	long long k;
	int a,b,c;
	cin>>n>>m>>k;
	vector<int>sites(n+1);
	for(int i=1;i<=n;i++) cin>>sites[i];
	vector<int>times(n+1);
	for(int i=1;i<=n;i++) cin>>times[i];
	
	unordered_map<int,vector<pair<int,int>>> roads; 
	for(int i=0;i<m;i++){
		cin>>a>>b>>c;
		roads[a].push_back({b,c});
		roads[b].push_back({a,c});
	}
	long long maxvalue = 0; 
	for(int i=1;i<=n;i++){
		if(times[i]>k) continue;
		maxvalue = max(maxvalue, (long long)sites[i]);
		// 第一步 从i出发 
		vector<pair<int,int>> next = roads[i];
		for(pair<int,int> p:next){
			long long time = times[i];
			long long value = sites[i];
			int j = p.first;
			int road = p.second;
			
			if(i!=j){
				time += (times[j] + road);
				if(time>k) continue;
				value += sites[j];
				maxvalue = max(maxvalue, value);
				/// 第二步 从j出发 
				vector<pair<int,int>> next1 = roads[j];
				for(pair<int,int> q:next1){
					long long time2 = time;
					long long value2 = value;
					int x = q.first;
					int road1 = q.second;
					
					if(x!=j&&x!=i){
						time2 += (times[x] + road1);
						if(time2>k) continue;
						value2 += sites[x];
						maxvalue = max(maxvalue, value2); 
					}
				}
			}
		} 
	}
	cout<<maxvalue<<endl;
}

感觉比小红书提前批的题简单多了

#小红书#
全部评论
爆内存可太艹了,我就是临接表存的,麻了
2
送花
回复
分享
发布于 2023-08-19 18:41 广东
第三题求个代码,只过了45%
1
送花
回复
分享
发布于 2023-08-19 17:55 上海
滴滴
校招火热招聘中
官网直投
第三题cout << 9得18%,写了一大堆还是18%
1
送花
回复
分享
发布于 2023-08-19 18:06 北京
第二题是回溯吗 一直超时 人麻了。
1
送花
回复
分享
发布于 2023-08-19 18:09 福建
为什么我只能过20%的测例QAQ,有解析嘛
点赞
送花
回复
分享
发布于 2023-08-19 17:54 上海
第一题咋做啊
点赞
送花
回复
分享
发布于 2023-08-19 17:58 北京
6
点赞
送花
回复
分享
发布于 2023-08-19 18:02 山东
第三题怎么做的,我理解是树上dp还得判断有没有环?
点赞
送花
回复
分享
发布于 2023-08-19 18:03 北京
小红书的cpp开发是那个岗位啊
点赞
送花
回复
分享
发布于 2023-08-19 18:05 广东
有代码嘛,第二题,我也是这个思路,只有18%
点赞
送花
回复
分享
发布于 2023-08-19 18:12 浙江
第一题不用考虑一个字符串里面有多个单词吗?
点赞
送花
回复
分享
发布于 2023-08-20 00:13 四川
给大佬点个赞 代码的思路清晰
点赞
送花
回复
分享
发布于 2023-08-20 15:23 安徽

相关推荐

11 29 评论
分享
牛客网
牛客企业服务