笔试题一定要善用STL,直接用sort函数和lambda表达式排序,用Unique函数去重
数据分类处理
http://www.nowcoder.com/questionTerminal/9a763ed59c7243bd8ab706b2da52b7fd
#include <bits/stdc++.h> using namespace std; int main() { vector<string> inputI; vector<string> inputR; int nI(0); int nR(0); while(cin>>nI){ inputI.clear(); inputR.clear(); while(nI--){ string t;cin>>t; inputI.push_back(t); } cin>>nR; while(nR--){ string t;cin>>t; inputR.push_back(t); } sort(inputR.begin(),inputR.end(),[](string a,string b)->bool{ unsigned int una(0),unb(0); istringstream iseama(a); istringstream iseamb(b); iseama>>una;iseamb>>unb; if(una<unb){ return true; } return false; }); // 排序结束 // 去重 int k = unique(inputR.begin(),inputR.end())-inputR.begin(); inputR.resize(k); string records; int allstatistics(0); for(int i=0;i<inputR.size();i++){ string temp; int statistics(0); for(int j=0;j<inputI.size();j++){ int pos = inputI[j].find(inputR[i]); if(pos!=inputI[j].npos){ temp += to_string(j)+" "; temp += inputI[j]+" "; statistics+=1; } } if(statistics!=0){ allstatistics += 2*statistics+2; // 原数字+统计+索引加实际数字(staticstics*2) temp = inputR[i]+" "+to_string(statistics)+" "+temp; records+=temp; } } records = to_string(allstatistics)+" "+records.substr(0,records.length()-1); cout<<records<<endl; records.clear(); } return 0; }