当前位置: 首页 > 面试经验 >

华为机试:数据分类处理

优质
小牛编辑
73浏览
2023-03-28

华为机试:数据分类处理


#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>

using namespace std;

bool match (string str1, string str2) {
if (str2.length() < str1.length()) return false;
for (int i = 0; i <= str2.length() - str1.length(); i++) {
if (str1 == str2.substr(i, str1.length())) {
return true;
}
}
return false;
}

int main() {
// int a, b;
// while (cin >> a >> b) { // 注意 while 处理多个 case
// cout << a + b << endl;
// }
vector<string> I;
set<int> R;
int N;
cin >> N;
while (N--) {
string str;
cin >> str;
I.push_back(str);
}
cin >> N;
while (N--) {
int M;
cin >> M;
R.insert(M);
}
vector<string> ans;
for (auto it = R.begin(); it != R.end(); it++) {
map<int, string> m;
for (int j = 0; j < I.size(); j++) {
if (match(to_string(*it), I[j])) {
m.insert(make_pair(j, I[j]));
}
}
if (!m.empty()) {
ans.push_back(to_string(*it));
ans.push_back(to_string(m.size()));
for (auto p = m.begin(); p != m.end(); p++) {
ans.push_back(to_string(p->first));
ans.push_back(p->second);
}
}
}
cout << ans.size() << " ";
for (int i = 0; i < ans.size() - 1; i++) {
cout << ans[i] << " ";
}
cout << ans[ans.size() - 1] << endl;
return 0;
}

 类似资料: