题解 | #数据分类处理#
数据分类处理
http://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
为什么我每次只知道用暴力破解呢,没任何的算法思想
#include <vector>
#include <set>
#include <string>
#include <sstream>
#include <memory.h>
using namespace std;
struct saveData{
string findValue;
int count;
vector<int> index;
vector<string> data;
};
int main()
{
int n, allCount, num;
string data;
vector<string> numList;
set <int> inputValueList;
vector<saveData*> result;
while(cin >> n) {
numList.clear();
result.clear();
inputValueList.clear();
allCount = 0;
//序列I
for(int i = 0; i < n; i++) {
cin >> data;
numList.push_back(data);
}
cin >> n;
//序列R
for (int i = 0; i < n; i++) {
cin >> num;
inputValueList.insert(num);
}
//找值
for (auto c = inputValueList.begin(); c != inputValueList.end(); ++c) {
data = to_string(*c);
saveData *dataNode = new saveData;
memset(dataNode, 0, sizeof(saveData));
for (int i = 0; i < numList.size(); i++) {
if(string::npos != numList[i].find(data)) {
dataNode->count += 1;
dataNode->index.push_back(i);
dataNode->data.push_back(numList[i]);
}
}
if (dataNode->count > 0) {
dataNode->findValue = data;
result.push_back(dataNode);
}
}
// 准备输出内容,
for (int i = 0; i < result.size(); i++) {
allCount += ( result[i]->count * 2 + 2);
}
//输出全部个数
cout << allCount << " ";
//输出每一项
for (int i = 0; i < result.size(); i++) {
cout << result[i]->findValue << " " << result[i]->count << " ";
for (int j = 0; j < result[i]->index.size(); j++) {
cout << result[i]->index[j] << " " << result[i]->data[j] << " ";
}
}
cout << endl;
}
}