WHU ACM Team is working on a brand new web browser named “Whu-Super-Browser”. You’re in response for a powerful feature: recording the previous addresses. Moreover, when a string is inputted, the browser will display all the addresses start with it. The addresses shall be sorted by visited times, in descending order. This feature is very useful to users.
Can you complete it?
There’re two kinds of operations:
Visit [url_str]: visit a website with the URL: [url_str].
Display [str]: display all addresses start with [str] and sort them by visited times, in descending order. If two addresses have the same visited times, then sort them in the lexicographical order.
简单的结构体排序题。用好map可以事半功倍。顺便放一下样例:
input:
1
10
Visit http://acm.whu.edu.cn
Visit http://acm.pku.edu.cn
Visit http://acm.timus.ru
Visit http://acm.whu.edu.cn
Visit http://acm.whu.edu.cn
Visit http://acm.pku.edu.cn
Display http://acm
Visit baidu.com
Visit www.whu.edu.cn
Display b
output:
http://acm.whu.edu.cn
http://acm.pku.edu.cn
http://acm.timus.ru
baidu.com
代码:
#include
using namespace std;
struct url{string name; int cnt;};
bool cmp(url a,url b)
{
if(a.cnt != b.cnt) return a.cnt > b.cnt;
return a.name < b.name;
}
int main(){
int t, n;
cin >> t;
while(t--) {
map<string, int> sites;
string op, s;
cin >> n;
while(n--) {
cin>> op >> s;
vector v;
if(op == "Visit") ++sites[s];
if(op == "Display") {
for(map<string, int>::iterator it = sites.begin(); it != sites.end(); ++it)
if((*it).first.find(s) == 0)
v.push_back({(*it).first, (*it).second});
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < v.size(); ++i)
cout<< v[i].name << endl;
cout << endl;
}
}
}
return 0;
}