ByteDance 2019 春招题目
牛客网字节跳动笔试真题:https://www.nowcoder.com/test/16516564/summary
分了 2 次做,磕磕碰碰才写完,弱鸡悲鸣。
1. 聪明的编辑
题目:Link .
两遍扫描
两遍扫描:第一次处理情况 1 ,第二次处理情况 2 。
// 万万没想到之聪明的编辑
// https://www.nowcoder.com/test/question/42852fd7045c442192fa89404ab42e92?pid=16516564&tid=40818118
#include <string>
#include <iostream>
using namespace std;
string stupid_check(string &s)
{
for (int i = 0; i + 1 < (int)s.length(); i++)
{
if (s[i] == s[i + 1])
{
if (i + 2 < (int)s.length() && s[i + 1] == s[i + 2])
s.erase(s.begin() + i + 2), i--;
}
}
for (int i = 0; i + 3 < (int)s.length(); i++)
{
if (s[i] == s[i + 1] && s[i + 2] == s[i + 3])
s.erase(s.begin() + i + 3);
}
return s;
}
int main()
{
int n;
cin >> n;
cin.ignore();
while (n--)
{
string s;
cin >> s;
cin.ignore();
cout << stupid_check(s) << endl;
}
}
自动机
由题意可得以下有限自动机:

每个状态的转移条件为:当前字符与上一个字符是否相等。
// 万万没想到之聪明的编辑
// https://www.nowcoder.com/test/question/42852fd7045c442192fa89404ab42e92?pid=16516564&tid=40818118
#include <string>
#include <iostream>
using namespace std;
string stupid_check(const string &s)
{
int len = s.length();
string result = "";
char cur = s[0], last = s[0];
int state = 0;
result.append(1, cur);
for (int i = 1; i < len; i++)
{
cur = s[i];
switch (state)
{
case 0:
{
if (cur == last) state = 1;
else state = 0;
result.append(1, cur);
break;
}
case 1:
{
if (cur == last) state = 1;
else state = 2, result.append(1, cur);
break;
}
case 2:
{
if (cur == last) state = 2;
else state = 0, result.append(1, cur);
break;
}
}
last = cur;
}
return result;
}
int main()
{
int n;
cin >> n;
cin.ignore();
while (n--)
{
string s;
cin >> s;
cin.ignore();
cout << stupid_check(s) << endl;
}
}
2. 抓捕孔连顺
题目:Link.
滑动窗口的思想,\(O(n^2)\) 的解法超时。
#include <iostream>
#include <vector>
using namespace std;
const uint64_t mod = 99997867;
int main()
{
ios::sync_with_stdio(0);
int n, d;
cin >> n >> d;
cin.ignore();
vector<int> pos(n, 0);
for (int i = 0; i < n; i++)
cin >> pos[i];
cin.ignore();
uint64_t ans = 0;
int i = 0;
while (i + 2 < n)
{
int j = i + 2;
while (j < n && ((pos[j] - pos[i]) <= d))
j++;
uint64_t t = j - i - 1;
ans = (ans + t * (t - 1) / 2) % mod;
i++;
}
cout << ans << endl;
}
优化一下,变成 \(O(n)\) . 注意有的地方需要用 uint64_t,用 int 会溢出导致结果错误。
#include <iostream>
#include <vector>
using namespace std;
const uint64_t mod = 99997867;
int main()
{
ios::sync_with_stdio(0);
int n, d;
cin >> n >> d;
cin.ignore();
vector<int> pos(n, 0);
uint64_t ans = 0;
for (int i = 0, j = 0; i < n; i++)
{
cin >> pos[i];
while (i >= 2 && pos[i] - pos[j] > d) j++;
uint64_t t = i - j;
if (t >= 2)
ans = (ans + t * (t - 1) / 2) % mod;
}
cout << ans << endl;
}
3. 雀魂
题目:Link.
考虑暴力枚举方法(模拟法):
- 利用哈希表
table计算 13 个数字的频率 - 枚举
1 - 9加入table,检查 14 张牌是否能和牌
检查和牌的函数 check(table):
- 在
table选取次数大于等于 2 的作为雀头 - 检查剩下的 12 张牌是否能组成 4 对顺子/刻子
检查顺子/刻子的函数 sub_check(table, n),n 表示剩下多少张牌可以检查。枚举 1 - 9:
- 如果该牌数目大于等于 3 ,说明可以组成刻子,继续检查
sub_check(table, n-3). - 如果
table[i], table[i+1], table[i+2]的数量都大于 1 ,说明可以组成顺子,继续检查sub_check(table, n-3).
代码:
// 雀魂启动!
// https://www.nowcoder.com/question/next?pid=16516564&qid=362291&tid=40818118
#include <iostream>
#include <array>
#include <vector>
using namespace std;
// 剩下的 n 张是否能组成顺子或者刻子
bool sub_check(array<int, 10> &table, int n)
{
if (n == 0) return true;
for (int i = 1; i <= 9; i++)
{
if (table[i] >= 3)
{
table[i] -= 3;
if (sub_check(table, n - 3)) return true;
table[i] += 3;
}
if (i + 2 <= 9 && table[i] >= 1 && table[i + 1] >= 1 && table[i + 2] >= 1)
{
table[i]--, table[i + 1]--, table[i + 2]--;
if (sub_check(table, n - 3)) return true;
table[i]++, table[i + 1]++, table[i + 2]++;
}
}
return false;
}
// 任意选取次数 >= 2 的牌作为雀头
bool check(array<int, 10> table)
{
for (int i = 1; i <= 9; i++)
{
if (table[i] >= 2)
{
table[i] -= 2;
if (sub_check(table, 12)) return true;
table[i] += 2;
}
}
return false;
}
int main()
{
vector<int> result;
array<int, 10> table = {0};
int val;
for (int i = 0; i < 13; i++)
{
cin >> val;
table[val]++;
}
for (int x = 1; x <= 9; x++)
{
if (table[x] >= 4) continue;
table[x]++;
if (check(table)) result.push_back(x);
table[x]--;
}
if (result.size() == 0) result.push_back(0);
for (int x : result) cout << x << ' ';
cout << endl;
}
4. 特征提取
题目:Link.
用 map 记录连续出现的次数。
具体实现细节:set 记录本次出现的 (x, y) 。每次来一帧,如果 map 中的 feature 不在 set 中出现,说明该 feature 不是连续出现的,置为 0 。
代码:
// 特征提取
// https://www.nowcoder.com/question/next?pid=16516564&qid=362292&tid=40818118
#include <iostream>
#include <map>
#include <set>
using namespace std;
struct feature
{
int x, y;
feature(int _x, int _y) : x(_x), y(_y) {}
bool operator<(const feature &f) const { return x < f.x || (x == f.x && y < f.y); }
};
int main()
{
ios::sync_with_stdio(0);
int n;
cin >> n;
cin.ignore();
map<feature, int> a;
set<feature> s;
a.clear(), s.clear();
int ans = 1;
while (n--)
{
int frames;
cin >> frames;
cin.ignore();
while (frames--)
{
int k, x, y;
cin >> k;
for (int i = 0; i < k; i++)
{
cin >> x >> y;
a[feature(x, y)]++;
s.insert(feature(x, y));
}
for (auto &[key, val] : a)
{
ans = max(ans, val);
if (s.count(key) == 0)
a[key] = 0;
}
s.clear();
}
}
cout << ans << endl;
}
5. 毕业旅行问题
题目:Link.
居然是 TSP 问题(离散数学中称之为哈密顿回路),我记得上课学的时候只会写贪心 。
看了题解,可以用状态压缩的 DP 求解。此处的最后一道题也是状压 DP 。
状态定义 \(dp[s,v]\) ,\(s\) 表示没去过的城市集合,\(v\) 表示当前所在城市。因此,\(dp[0,0]\) 表示所有城市去过,并在所在地为 0 城市,即结束状态;\(dp[2^n-1, 0]\) 表示所有城市都没去过,当前在 0 号城市,即开始状态。
定义 is_visited(s, u) 为集合 s 是否包含了城市 u, 即当前状态是否已经访问过 u .
定义 set_zero(s, k) 把 s 的从左往右数的第 k 比特置为 1 ,表示城市 k 已访问。
时间复杂度 \(O(n^2\cdot2^n)\),空间复杂度 \(O(n \cdot 2^n)\) .
代码:
#include <iostream>
#include <vector>
using namespace std;
const int N = 21;
int graph[N][N] = {{0}};
int tsp(int n)
{
vector<vector<int>> dp((1 << n), vector<int>(n, 0x3f3f3f3f));
auto is_visited = [](int s, int u) { return ((s >> u) & 1) == 0; };
auto set_zero = [](int s, int k) { return (s & (~(1 << k))); };
dp[(1 << n) - 1][0] = 0;
// double 'for' loop to fill the dp
for (int s = (1 << n) - 1; s >= 0; s--)
{
for (int v = 0; v < n; v++)
{
// for current 's', try all the cities
for (int u = 0; u < n; u++)
{
if (!is_visited(s, u)) // if 'u' has not been visited
{
int state = set_zero(s, u);
dp[state][u] = min(dp[state][u], dp[s][v] + graph[v][u]);
}
}
}
}
return dp[0][0];
}
int main()
{
int n;
cin >> n;
cin.ignore();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cin >> graph[i][j];
cin.ignore();
}
cout << tsp(n) << endl;
}
6. 找零
题目:Link.
老水题了。不过要注意的是:这里没有 2 和 8 面值的硬币(原来写了个 for 循环,浪费了一次提交 )。
// 硬币找零
// https://www.nowcoder.com/question/next?pid=16516564&qid=362294&tid=40818118
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
int n;
cin >> n;
cin.ignore();
n = 1024 - n;
int ans = n / 64;
n %= 64;
ans += n / 16;
n %= 16;
ans += n / 4;
n %= 4;
ans += n;
cout << ans << endl;
}
ByteDance 2019 春招题目的更多相关文章
- 2019春招——Vivo大数据开发工程师面经
Vvio总共就一轮技术面+一轮HR面,技术面总体而言,比较宽泛,比较看中基础,面试的全程没有涉及简历上的东西(都准备好跟他扯项目了,感觉是抽取的题库...)具体内容如下: 1.熟悉Hadoop哪些组件 ...
- 京东2019春招Java工程师编程题题解
生成回文串 题目描述 对于一个字符串,从前开始读和从后开始读是一样的,我们就称这个字符串是回文串. 例如"ABCBA","AA","A"是回 ...
- 2019春招面试高频题(Java版),持续更新(答案来自互联网)
第一模块--并发与多线程 Java多线程方法: 实现Runnable接口, 继承thread类, 使用线程池 操作系统层面的进程与线程(对JAVA多线程和高并发有了解吗?) 计算机资源=存储资源+计算 ...
- Java开发面试题整理(2019春招)
一.Java基础部分 1. HashMap和Hashtable各有什么特点,它们有什么区别?(必背题,超级重要) HashMap和Hashtable都实现了Map接口,但决定用哪一个之前先要弄清楚它们 ...
- [找工作] 2019秋招|从春招到秋招,Java岗经验总结(收获AT)
转自(有更多) https://blog.csdn.net/zj15527620802/article/month/2018/10 前言 找工作是一件辛酸而又难忘的历程.经历过焦虑.等待.希望,我们最 ...
- 2018春招实习笔试面试总结(PHP)
博主双非渣本计算机软件大三狗一枚,眼看着春招就要结束了,现将自己所经历的的整个春招做一个个人总结. 首先就是关于投递计划,博主自己整理了一份各大公司的春招信息,包括网申地址,开始时间,结束时间,以及自 ...
- 2018春招-今日头条笔试题-第四题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) #-*- coding:utf-8 -*- class Magic: ''' a:用于存储数组a b:用于存储数组b num:用于 ...
- 2018春招-今日头条笔试题-第三题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 本题的做法最重要的应该是如何拼出‘1234567890’,对于输入表达试获得对应的结果利用python内置函数eval ...
- 2018春招-今日头条笔试题-第二题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 利用深度优先搜索 #-*- coding:utf-8 -*- class DFS: ''' num:用于存储最后执行次 ...
随机推荐
- 连接数据库查询 将查询结果写入exce文件中
package com.cn.peitest.connectDatabase; import java.io.File; import java.lang.reflect.Field; import ...
- 在wildfly 21中搭建cluster集群
目录 简介 下载软件和相关组件 配置domain 创建应用程序 部署应用程序 集群配置 总结 简介 wildfly是一个非常强大的工具,我们可以轻松的使用wildfly部署应用程序,更为强大的是,wi ...
- 冰河又一MySQL力作出版(文末送书)!!
写在前面 继<海量数据处理与大数据技术实战>之后,冰河的又一力作<MySQL技术大全:开发.优化与运维实战>出版,相信这本书对任何想系统学习MySQL的小伙伴来说,都会带来实质 ...
- 你说一下Redis为什么快吧,怎么实现高可用,还有持久化怎么做的?
前言 作为Java程序员,在面试过程中,缓存相关的问题是躲不掉的,肯定会问,例如缓存一致性问题,缓存雪崩.击穿.穿透等.说到缓存,那肯定少不了Redis,我在面试的时候也是被问了很多关于Redis相关 ...
- [LeetCode]86. Partition List分离链表
/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...
- 指令重排序 as-if-serial
笔者认为看完一本书或刚要了解完一个知识点 最好自己先运行一些DEMO 自己尝试着去了解下各种意思 这样知识点最终一定是你的.靠死记硬背的讨论或简单的粗暴的看下资料 脑子里肯定还是一团浆糊. p.p ...
- Codefroces 1328E Tree Querie(dfs序)
Codefroces 1328E Tree Querie 题目 给出一棵1为根,n个节点的树,每次询问\(k_i\) 个节点,问是否存在这样一条路径: 从根出发,且每个节点在这条路径上或者距离路径的距 ...
- node获取请求参数的方法get与post请求
1.get请求 get的请求参数是携带在url中的,因此需要引入url模块对请求进行解析,再使用url.parse()方法,get请求多用于页面跳转.表单等请求中,例如page页码.表单账号密码等 先 ...
- ZooKeeper集群“脑裂”
ZooKeeper 集群节点为什么要部署成奇数ZooKeeper 容错指的是:当宕掉几个ZooKeeper节点服务器之后,剩下的个数必须大于宕掉的个数,也就是剩下的节点服务数必须大于n/2,这样Zoo ...
- Docker环境下升级PostgreSQL
查阅PostgreSQL官方文档可以得知,官方提供了两种方式对数据库进行升级--pg_dumpall与pg_upgrade. pg_dumpall是将数据库转储成一个脚本文件,然后在新版数据库中可以直 ...