题解 | #数据分类处理#
数据分类处理
http://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
为什么我每次写的代码都好复杂呜呜呜,要多多学习大佬的思路啊
#include <string>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int main() {
string str, temp;
vector<string> v, vtemp;
set<int> s;
map<int, string> m;
int N1 = 0, N2 = 0, cnt = 0;
while (cin >> N1) {
for (int i = 0; i < N1; i++) {
cin >> str;
v.push_back(str);
}
cin >> N2;
for (int j = 0; j < N2; j++) {
cin >> str;
int x = stoi(str);
s.insert(x);
}
for (auto& it : s) {
vtemp.push_back(to_string(it));
}
for (int i = 0; i < vtemp.size(); i++) {
m.clear();
for (int j = 0; j < v.size(); j++) {
size_t n = v[j].find(vtemp[i]);
if (n != string::npos) {
m[j] = v[j];
}//if
}//内层for
if (m.size() != 0) {
temp += vtemp[i] + ' ' + to_string(m.size()) + ' ';
for (auto it : m) {
temp = temp + to_string(it.first) + ' ' + it.second + ' ';
}//范围for
cnt += m.size() * 2 + 2;
}//if
}//外层for
cout << to_string(cnt) + ' ' + temp << endl;
}//while
}