关注
第一题:map搞搞
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
char s[100005];
map<char,int> m;
int main()
{
while(gets(s))
{
int cnt = -1;
int pos = 100000005;
char ans;
m.clear();
int len = strlen(s);
for(int i = 0; i < len; ++i)
{
if(s[i] >= 'a' && s[i] <= 'z')
{
s[i] = s[i] - 32;
}
if(s[i] >='A' && s[i] <= 'Z')
{
m[s[i]]++;
}
if(m[s[i]] >= cnt)
{
cnt = m[s[i]];
ans = s[i];
//pos = i;
}
}
for(int i = 0; i < len; ++i)
{
if(m[s[i]] == cnt)
{
ans = s[i];
break;
}
}
cout<<ans<<cnt<<endl;
}
return 0;
}
第二题 哈弗曼树:DFS+优先队列模拟 #include <cstdio>
#include <iostream>
#include <set>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 1e5;
vector<int> num[300];
struct Node{
Node *left;
Node *right;
int id;
int code;
int num;
friend bool operator < (const Node &a ,const Node &b){
return a.num > b.num;
}
}a[maxn];
priority_queue< Node > q;
string Code[300];
void dfs(Node *root,string now){
if (root->left == NULL && root->right == NULL){
Code[root->code] = now;
return;
}
string Now = now;
if (root->left != NULL) dfs(root->left,Now + "0");
if (root->right != NULL) dfs(root->right,Now + "1");
}
int main(){
string s;
cin >> s;
for (int i = 0; i < (int)s.size(); i++){
num[s[i]].push_back(i);
}
int cnt = 0;
while (!q.empty()) q.pop();
for (int i = 0; i < 300; i++){
if ((int)num[i].size() > 0){
a[cnt].code = i;
a[cnt].id = cnt;
a[cnt].num = (int)num[i].size();
a[cnt].left = NULL;
a[cnt].right = NULL;
q.push(a[cnt]);
cnt++;
}
}
while (q.size() > 1){
Node fr1 = q.top();
q.pop();
Node fr2 = q.top();
q.pop();
a[cnt].left = &a[fr1.id];
a[cnt].right = &a[fr2.id];
a[cnt].id = cnt;
a[cnt].num = fr1.num + fr2.num;
q.push(a[cnt]);
cnt++;
}
Node f = q.top();
q.pop();
Node *Root = &f;
dfs(Root,"");
for (int i = 0; i < (int) s.size(); i++){
cout << Code[s[i]];
}
cout << endl;
}
第三题:题意不清的模拟题,过不去,过了40% 请AC的大佬指正还需要注意那些细节,谢谢!
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
int main() { int n, x; scanf("%d", &n); bool head = true, flag = true; int pre = 0; std::vector< int > ans; while(flag && n--) { scanf("%x", &x); if(!x) { if(!n) { flag = false; break; } scanf("%x", &x); --n; if(x > 15) flag = false; else if(head) { pre = x; head = false; } else if((pre + 1) % 16 != x) flag = false; else pre = x; if(!n) { flag = false; break; } scanf("%x", &x); --n; int t = x >> 4, len = x & 15; std::vector< int > d; if(len > n) { flag = false; break; } while(len--) { scanf("%x", &x); d.push_back(x); --n; } if(t == 1 || t == 2) std::sort(d.begin(), d.end()); if(t == 2) ans.insert(ans.end(), d.begin(), d.end()); else ans.insert(ans.end(), d.rbegin(), d.rend()); } } if(flag) { for(int i = 0; i < ans.size(); ++i) { if(i) printf(" "); printf("0x%x", ans[i]); } puts(""); } else puts("FALSE"); return 0;
}
查看原帖
点赞 评论
相关推荐
查看10道真题和解析 点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 2025年终总结 #
147500次浏览 2517人参与
# 秋招落幕,你是He or Be #
3204次浏览 76人参与
# 应届生进小公司有什么影响吗 #
109078次浏览 1116人参与
# 比亚迪工作体验 #
70157次浏览 254人参与
# 你面试体验感最差/最好的公司 #
3032次浏览 56人参与
# 工作中听到最受打击的一句话 #
2525次浏览 61人参与
# 大厂VS公务员你怎么选 #
71155次浏览 660人参与
# 重来一次,你会对开始求职的自己说 #
2963次浏览 71人参与
# 一人说一个提前实习的好处 #
3350次浏览 70人参与
# 团建是“福利”还是是 “渡劫” #
4021次浏览 110人参与
# 实习没事做是福还是祸? #
8261次浏览 135人参与
# 如何排解工作中的焦虑 #
243781次浏览 2241人参与
# 从顶到拉给所有面过的公司评分 #
144796次浏览 518人参与
# 今年你最想重开的一场面试是? #
1406次浏览 25人参与
# 你小心翼翼的闯过多大的祸? #
6919次浏览 109人参与
# 联影求职进展汇总 #
123831次浏览 781人参与
# OPPO求职进展汇总 #
755896次浏览 5390人参与
# 互联网公司爆料 #
158576次浏览 724人参与
# 产品实习,你更倾向大公司or小公司 #
189097次浏览 2053人参与
# 秋招结束之后的日子 #
113911次浏览 1039人参与
