Skip to content

Latest commit

 

History

History
105 lines (88 loc) · 2.54 KB

A1153.md

File metadata and controls

105 lines (88 loc) · 2.54 KB

A1153 Decode Registration Card of PAT

1.题意理解

给出一堆pat考号,和对应成绩(别紧张,高分不是说你),然后有三种查询,针对每种查询给出对应的结果。

2.思路分析

把信息存储起来后,分别根据三种要求,编写相应的排序和查询逻辑即可。

3.参考代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<string, int> psi;

const int N = 10010, M = 128;
vector<psi> vec;

bool cmp(const psi &a, const psi &b)
{
    if(a.second != b.second)
        return a.second > b.second;
    else
        return a.first < b.first;
}

int main()
{
    int n, m, score, typ;
    string card, query;

    cin >> n >> m;
    for(int i = 0; i < n; i++)
    {
        cin >> card >> score;
        vec.push_back(make_pair(card, score));
    }

    for(int i = 1; i <= m; i++)
    {
        cin >> typ >> query;
        printf("Case %d: %d %s\n", i, typ, query.c_str());

        if(typ == 1)
        {
            vector<psi> ans;
            for(auto it : vec)
                if(it.first.substr(0, 1) == query)
                    ans.push_back(it);

            if(ans.size())
            {
                sort(ans.begin(), ans.end(), cmp);
                for(auto it : ans)
                    printf("%s %d\n", it.first.c_str(), it.second);
            }
            else
                printf("NA\n");
        }

        if(typ == 2)
        {
            int tot_n = 0, tot_s = 0;
            for(auto it : vec)
                if((it.first.substr(1, 3)) == query)
                {
                    tot_n++;
                    tot_s += it.second;
                }

            if(tot_n)
                printf("%d %d\n", tot_n, tot_s);
            else
                printf("NA\n");
        }

        if(typ == 3)
        {
            vector<psi> ans;
            unordered_map<string, int> mp;

            for(auto it : vec)
            {
                string site = it.first.substr(1, 3);
                string date = it.first.substr(4, 6);

                if((date) == query)
                    mp[site]++;
            }

            if(mp.size())
            {
                for(auto it : mp)
                    ans.push_back(it);
                sort(ans.begin(), ans.end(), cmp);
                for(auto it : ans)
                    printf("%s %d\n", it.first.c_str(), it.second);
            }
            else
                printf("NA\n");
        }
    }

    return 0;
}