华为笔试-2020.4.15

华为软件开发2021届暑期实习笔试。

大佬全AC链接:https://www.nowcoder.com/discuss/409299

第一题

结果:AC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
int main() {
string str;
map<string, int> m;
cin >> str;
string temp = "";
bool flag = true;
for (int i = 0; i < str.size(); ++i) {
if ((str[i] <= 'z' && str[i] >= 'a') || (str[i] <= 'Z' && str[i] >= 'A') || str[i] == ',' || str[i] == '\0') {
if (str[i] != ',') {
if (flag == true) {
if (str[i] <= 'z' && str[i] >= 'a') {
cout << "error.0001" << endl;
return 0;
}
flag = false;
}
else {
if (str[i] <= 'Z' && str[i] >= 'A') {
cout << "error.0001" << endl;
return 0;
}
}
temp += str[i];
}
else if (str[i] == ',' || str[i] == '\0') {
++m[temp];
temp = "";
flag = true;
}
}
else {
cout << "error.0001" << endl;
return 0;
}
}
++m[temp];
int maxVal = -1;
string maxStr;

for (auto item : m) {
if (item.second > maxVal) {
maxStr = item.first;
maxVal = item.second;
}
}

vector<string> vec;
for (auto item : m) {
if (item.second == maxVal) {
vec.emplace_back(item.first);
}
}
sort(vec.begin(), vec.end());

cout << vec[0] << endl;
return 0;
}

第二题

结果: 75

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

int main() {
string str, pattern;
cin >> str;
cin >> pattern;
int strLen = str.size();
int index = pattern.find(str);
if (index == -1)
cout << "FAIL" << endl;
while (index < pattern.size()) {
//pattern.substr(index+str.size()+1,)
string subStr = pattern.substr(index + strLen, 7);
//cout << subStr << endl;
if (subStr == "[addr=0"){
//if (pattern[index + strLen] == '[') {
//cout << index << endl;
int i = index + strLen + 6;
string temp = "";
bool flag = true;
vector<string> print;
while (i < pattern.size() && pattern[i] != ']') {
if (pattern[i] == ',') {
//cout << temp << " ";
print.emplace_back(temp);
temp = "";
if (flag == true) {
i += 5;
flag = false;
}
else
i += 4;

}
else {
temp += pattern[i];
}
++i;
}
for (int j = 0; j < print.size(); ++j)
cout << print[j] << " ";
cout << temp << "\r\n";
index = i;
//cout << endl;
}
else {
int i = index + strLen;
while (i < pattern.size() && pattern[i] != ']') {
++i;
}
index = i;
//cout << pattern[i] << endl;
}
index = pattern.find(str, index);
}

return 0;
}

第三题

结果:30

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;

void dfs(int index, map<int, int> & stackSize, map<int, vector<int>>& m, vector<int> & res, int & temp) {
if (m[index].empty()) {
res.emplace_back(temp);
}

for (auto item : m[index]) {
temp += stackSize[item];
dfs(item, stackSize, m, res, temp);
temp -= stackSize[item];
}

}

//这个函数存在问题
/*
bool cycle(set<int> & s, int index, map<int, vector<int>> & m) {
if (m[index].empty()) {
return false;
}
for (auto item : m[index]) {
if (s.find(item) != s.end())
return true;
s.insert(item);
cycle(s, item, m);
s.erase(item);
}
return false;
}*/

int main() {
int n;
cin >> n;
vector<int> vec;
int temp;
for (int i = 0; i < n; ++i) {
cin >> temp;
vec.emplace_back(temp);
}
map<int, vector<int>> m;
vector<int> num;
map<int, int> stackSize;
for (int i = 0; i < n; ++i) {
int index;
cin >> index;
num.emplace_back(index);
int s = -1;
cin >> s;
if (s == -1) {
cout << "NA" << endl;
return 0;
}
stackSize[index] = s;
int t;
vector<int> nums;
for (int j = 0; j < vec[index - 1]; ++j) {
cin >> t;
nums.emplace_back(t);
}
m[index] = nums;
}
//for (int i = 0; i < 5; ++i)
// cout << stackSize[i] << endl;
//for (int i = 0; i < 5; ++i) {
// for (int j = 0; j < m[i + 1].size(); ++j) {
// cout << m[i + 1][j] << " ";
// }
// cout << endl;
//}
vector<int> res;
for (int i = 0; i < n; ++i) {
/*
set<int>s{num[i]};
if (cycle(s, num[i], m)) {
cout << "R" << endl;
return 0;
}*/
if (!(m[i+1].empty()))
dfs(num[i], stackSize, m, res, stackSize[i]);
}
int maxVal;
for (int i = 0; i < res.size(); ++i)
maxVal = max(maxVal, res[i]);
cout << maxVal << endl;
// cout << res[i] << endl;
return 0;
}

欢迎评论区分享正确答案,十分感谢!

zxp wechat
欢迎关注微信公众号!