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

题目简介


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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值