关注
第一题: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;
}
查看原帖
点赞 评论
相关推荐
2025-12-31 13:48
门头沟学院 产品经理
一天代码十万三:但有没有可能,想进高质量实习也需要实习经历?有时候多段实习就是为了找到更好的实习,并不一定是为了写到简历 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 牛客吐槽大会 #
26192次浏览 327人参与
# 一份好的简历长什么样? #
23187次浏览 423人参与
# 材料专业就业可以去哪些企业岗位 #
55014次浏览 376人参与
# 为了减少AI幻觉,你注入过哪些设定? #
8062次浏览 220人参与
# 在大厂上班是一种什么样的体验 #
15819次浏览 225人参与
# 机械人避雷的岗位/公司 #
45043次浏览 320人参与
# 你的mentor是什么样的人? #
51704次浏览 741人参与
# 论秋招对个人心气的改变 #
16753次浏览 254人参与
# 我和mentor的爱恨情仇 #
106817次浏览 969人参与
# 牛客AI体验站 #
9827次浏览 234人参与
# 程序员找工作至少要刷多少题? #
25019次浏览 317人参与
# 本周投递记录 #
842393次浏览 12226人参与
# 制造业的秋招小结 #
142302次浏览 2086人参与
# 晒晒你司的新年福利 #
10674次浏览 191人参与
# 应届生进小公司有什么影响吗 #
119075次浏览 1162人参与
# AI Coding的使用心得 #
6708次浏览 142人参与
# 一张图晒一下你的AI员工 #
6886次浏览 153人参与
# 我现在比当时_,你想录用我吗 #
10631次浏览 160人参与
# 通信/硬件求职避坑tips #
140062次浏览 1087人参与
# 秋招想进国企该如何准备 #
125551次浏览 617人参与