题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
#include <iostream> #include <string> #include <vector> using namespace std; struct SJRecord { SJRecord(): mIs(), mIndexs() {} vector<int> mIs; vector<int> mIndexs; int mCount=0; int mR; }; bool BInclude(int item, int num) { string item_str = to_string(item); string num_str = to_string(num); string::size_type tempresult = num_str.find(item_str); return tempresult != string::npos; } void TM24() { int Inum, Rnum, ii, rr; cin >> Inum; vector<int> inIs;////////////////////////////////////// I for (int i = 0; i < Inum; i++) { int tempI; cin >> tempI; inIs.push_back(tempI); } cin >> Rnum; vector<int> inRs;/////////////////////////////////////// R for (int i = 0; i < Rnum; i++) { int tempR; cin >> tempR; inRs.push_back(tempR); } //数据输入完毕 //首先对R排序去重 vector<int> afterRs; afterRs.push_back(inRs[0]); for (int i = 1; i < inRs.size(); i++) { for (int j = 0; j < afterRs.size(); j++) { if (inRs[i] == afterRs[j]) { break; } else if (inRs[i] < afterRs[j]) { afterRs.insert(afterRs.begin() + j, inRs[i]); break; } if (j == afterRs.size()-1) afterRs.push_back(inRs[i]); } } //排序去重完毕 //查找元素 vector<SJRecord> seachRecords; for (int i : afterRs) { SJRecord tempResult; tempResult.mR = i; for (int j = 0; j < inIs.size(); j++) { if (BInclude(i, inIs[j])) { tempResult.mCount++; tempResult.mIndexs.push_back(j); tempResult.mIs.push_back(inIs[j]); } } if (tempResult.mCount) seachRecords.push_back(tempResult); } //输出 int alloutcount = 0; for (auto item : seachRecords) { alloutcount += (2 * item.mCount + 2); } cout << alloutcount << " "; for (int i = 0; i < seachRecords.size(); i++) { cout << seachRecords[i].mR << " " << seachRecords[i].mCount << " "; for (int j = 0; j < seachRecords[i].mCount; j++) { cout << seachRecords[i].mIndexs[j] << " " << seachRecords[i].mIs[j] << " "; } } } int main() { TM24(); } // 64 位输出请用 printf("%lld")