快手2020测试类笔试题A卷
1. 规律字符串输出
1 => A
2 => ABA
3 => ABACABA
以此类推
根据题目要求输出,要注意的点是输入有误时的处理
2. 压缩字符串还原
ab2c4 => abbcccc
可能坑点:1. 题目中只说了字母,可能包括小写字母和大写字母 2. 数字的位数可能大于2位
3. 输出符合特定要求的元素所在下标
[1, 2, 3] => -1 (不存在)
[100, 99, 99, 98] => 1, 2
[10, 20, 9, 11] => 3
题目要求输出满足以下条件的元素下标
1. 该元素之前的元素中只有一个比它大,重复元素按重复次数计算
可能坑点:只记录最大元素及其次数是不行的,还需要记录第二大的元素,以此判断是否符合上面的条件
4. 模拟:计算并按照规则排序输出靓号
大致题意,11位手机号,可以分为靓号和非靓号,其中靓号包括顺子,如***123*****,和豹子,如***1111****,均是三位及以上才被考虑
可能坑点:排序略繁琐,细节较多,需细心;靓号规则细节较多,如顺子有递增递减两种,顺子和豹子只考虑3-11位,同等长度豹子比顺子价值更大,连续出现3位以上才被归为顺子和豹子,同时出现顺子和豹子取连续位数更长的那个计算等等
参考代码(C++):
#include <bits/stdc++.h>
using namespace std;
class Solution {
struct Phone { // 号码类,存储靓号及其价值
string number; // 手机号
bool isShunZi; // true:是顺子,false:是豹子
int cnt; // 连续位数
int i; // 输入顺序的下标
Phone(string _number, bool _isShunZi, int _cnt, int _i) {
number = move(_number);
isShunZi = _isShunZi;
cnt = _cnt;
i = _i;
}
bool operator<(const Phone &r) { // 按题意中的优先级写排序
if (cnt > r.cnt) {
return true;
} else if (cnt < r.cnt) {
return false;
}
if (!isShunZi && r.isShunZi) {
return true;
} else if (isShunZi && !r.isShunZi) {
return false;
}
return i < r.i;
}
};
vector<string> v; // 所有电话号码
void input() {
string s;
cin >> s;
int l = s.size();
for (int i = 0; i < l; i += 12) {
v.push_back(s.substr(i, 11));
}
}
Phone cal(string s, int index) { // 计算豹子、顺子、连续位数等价值信息
int shunzi = 0; // 不知道顺子的英文是啥,只好写成拼音,下同
for (int i = 3; i < 9; ++i) {
char ch = s[i];
int j = i;
int cnt = 1;
while (i < 11 && s[i + 1] == ch + (i + 1 - j)) { // 递增判断,如123
++i;
++cnt;
}
shunzi = max(shunzi, cnt);
}
for (int i = 3; i < 9; ++i) {
char ch = s[i];
int j = i;
int cnt = 1;
while (i < 11 && s[i + 1] == ch - (i + 1 - j)) { // 递减判断,如321
++i;
++cnt;
}
shunzi = max(shunzi, cnt);
}
int baozi = 0;
for (int i = 3; i < 9; ++i) {
char ch = s[i];
int cnt = 1;
while (i < 11 && ch == s[i + 1]) { // 连续判断,如111
++i;
++cnt;
}
baozi = max(baozi, cnt);
}
bool isShunZi = shunzi > baozi; // 因为同等长度豹子的价值更大,所以这里用<而不是<=
return Phone(move(s), isShunZi, max(shunzi, baozi), index);
}
vector<Phone> filter() {
vector<Phone> res; // 符合靓号要求的电话号码
for (int i = 0; i < v.size(); ++i) {
Phone tmp = move(cal(v[i], i));
if (tmp.cnt >= 3) {
res.emplace_back(tmp);
}
}
sort(res.begin(), res.end());
return res;
}
void print(const vector<Phone> &ans) {
if (ans.empty()) {
cout << "null" << endl;
return;
}
bool flag = false;
for (const Phone &tmp : ans) {
if (flag) cout << ","; else flag = true;
cout << tmp.number;
}
cout << endl;
}
public:
Solution() {
input();
print(filter());
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Solution solution = Solution();
return 0;
}
PS:可能坑点就是可能是坑点的坑点,因为笔试中我是按自己对题目的理解写的,不保证评测数据一定包括这些坑点。欢迎牛友补充
