33,311
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;
int main(int , char *[])
{
map<string, set<string> > buffer;
//首次读取
int nItems = 0;
cin>>nItems;
for (int i = 0; i < nItems; ++i)
{
int nNumbers = 0;
string name ;
cin >> name >> nNumbers;
for (int j = 0; j < nNumbers; ++j)
{
string num;
cin >> num;
buffer[name].insert(num);
}
}
//去除不含城市的短号
cout << buffer.size() << endl;
for (auto p = buffer.begin();p != buffer.end(); ++p)
{
const string & name = p->first;
const set<string> & nums = p->second;
//按照字符串长度由大到小排序
vector<string> tmp;
vector<string> final;
copy(nums.begin(),nums.end(),back_inserter(tmp));
sort(tmp.begin(),tmp.end(),[](const string &a,const string &b)->bool{
return a.size() > b.size(); });
//开始组合
const size_t sz_items = tmp.size();
string mask ;
for(size_t j = 0; j < sz_items; ++j)
{
const string & cur_test = tmp[j] + "|";
if (mask.find(cur_test)==mask.npos)
{
final.push_back(tmp[j]);
mask.append(cur_test);
}
}
//按照顺序排序
sort(final.begin(),final.end(),
[](const string &a,const string &b)->bool{
if (a.size() < b.size())
return true;
if (a.size() > b.size())
return false;
return a<b;
});
//输出
const size_t sz_final = final.size();
cout << name << " " << sz_final;
for(size_t j = 0; j < sz_final; ++j)
cout << " " << final[j];
cout<<endl;
}
return 0;
}
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
2
dasha 2 23 789
ivan 4 2 123 456 789
按 <RETURN> 来关闭窗口...