EOJ 2607/HDU 2698/WOJ 1414/The 4th Baidu Cup URL

来源:互联网 发布:知豆电动车能上高速吗 编辑:程序博客网 时间:2024/05/16 20:27

题目简介


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:

110Visit http://acm.whu.edu.cnVisit http://acm.pku.edu.cnVisit http://acm.timus.ruVisit http://acm.whu.edu.cnVisit http://acm.whu.edu.cnVisit http://acm.pku.edu.cnDisplay http://acmVisit baidu.comVisit www.whu.edu.cnDisplay b

output:

http://acm.whu.edu.cnhttp://acm.pku.edu.cnhttp://acm.timus.rubaidu.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;}