题解 | #数据分类处理#
数据分类处理
http://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
思路
将输入数据采用字符串接收,然后根据 string 的 find 方法来查找是否有子串的方式来判断 l 中是否包含 r。
代码
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
using namespace std;
struct strComp
{
bool operator() (const string& s1, const string& s2)
{
return stoi(s1) < stoi(s2);
}
};
void parse(vector<string>& l, vector<string>& r)
{
set<string, strComp> newR(r.begin(), r.end());
int count = 0;
map<string, vector<int>> res;
for( auto it = newR.begin(); it != newR.end(); it++ )
{
bool flag = false;
for( int i = 0; i < l.size(); i++ )
{
if( l[i].find(*it) != l[i].npos )
{
res[*it].push_back(i);
flag = true;
}
}
if( flag )
count = res[*it].size() * 2 + 2 + count;
}
cout << count;
for( auto it=newR.begin(); it != newR.end(); it++ )
{
int len = res[*it].size();
if( 0 == len )
continue;
cout << " " << *it << " " << len;
for(int i = 0; i < len; i++)
cout << " " << res[*it][i] << " " << l[res[*it][i]];
}
cout << endl;
}
int main()
{
int n, i;
while( cin >> n )
{
vector<string> l(n);
for( i = 0; i < n; i++ )
cin >> l[i];
cin >> n;
vector<string> r(n);
for( i = 0; i < n; i++)
cin >> r[i];
parse(l, r);
}
return 0;
}