题目大意:给出一堆名字和电话号码,如果同一个人的电话中有一个号码是另一个号码的后缀,可以视为同一个号码,输出电话本
用map存储,然后写一个检验函数进行暴力
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
map<string,set<string> > m;
bool check(string s,string t) {
for(int i = 0;i < s.size();i++) {
if(s[s.size()-1-i] != t[t.size()-1-i]) {
return false;
}
}
return true;
}
bool cmp(string s,string t) {
return s.size() < t.size();
}
int main() {
ios::sync_with_stdio(false);
int n;
set<string> name;
cin >> n;
for(int i = 0;i < n;i++) {
string s_1;
string s_2;
int t;
cin >> s_1 >> t;
name.insert(s_1);
for(int j = 0;j < t;j++) {
cin >> s_2;
m[s_1].insert(s_2);
}
}
map<string,set<string> >::iterator it_1;
set<string>::iterator it_2;
vector<string> v;
set<string> s;
for(it_1 = m.begin();it_1 != m.end();it_1++) {
v.clear();
s.clear();
for(it_2 = it_1->second.begin();it_2 != it_1->second.end();it_2++) {
v.push_back(*it_2);
}
sort(v.begin(),v.end(),cmp);
for(int i = 0;i < v.size();i++) {
bool flag = true;
for(int j = i + 1;j < v.size();j++) {
if(check(v[i],v[j])) {
flag = false;
}
}
if(flag) {
s.insert(v[i]);
}
}
it_1->second = s;
}
cout << name.size() << endl;
for(it_1 = m.begin();it_1 != m.end();it_1++) {
cout << it_1->first << " " << it_1->second.size();
for(it_2 = it_1->second.begin();it_2 != it_1->second.end();it_2++) {
cout << " " << *it_2;
}
cout << endl;
}
return 0;
}