题目简介
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<bits/stdc++.h>
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<url> 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;
}
本文介绍了一个实用的浏览器功能——地址记录与展示的实现方法。该功能能够记录网站访问历史并根据输入的字符串显示所有匹配的地址,按访问次数降序排序。若访问次数相同,则按字典序排序。
478

被折叠的 条评论
为什么被折叠?



