1153 Decode Registration Card of PAT (25 分)

\quad 熟练运用map和sort就很容易。需注意第四个测试点用map会超时,全部用unordered_map就好。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e4+10;

unordered_map<char, vector<pair<string, int> > > level;
unordered_map<string, int> site;
unordered_map<string, int> num;
unordered_map<string, unordered_map<string, int> > date;

bool cmp(pair<string, int> p1, pair<string, int> p2)
{
	if(p1.second!=p2.second) return p1.second>p2.second;
	else return p1.first<p2.first;
}

int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
    for (int i = 0; i < n; ++i)
    {
    	string s;
    	int score;
    	cin >> s >> score;
    	level[s[0]].push_back(make_pair(s, score));
        site[s.substr(1, 3)] += score;
        num[s.substr(1, 3)]++;
        date[s.substr(4, 6)][s.substr(1, 3)]++;
    }
    sort(level['T'].begin(), level['T'].end(), cmp);
    sort(level['A'].begin(), level['A'].end(), cmp);
    sort(level['B'].begin(), level['B'].end(), cmp);
    for (int i = 1; i <= m; ++i)
    {
    	string t1;
    	cin >> t1;
    	if(t1=="1")
    	{
    		char t2;
    		cin >> t2;
    		cout << "Case " << i << ": " << t1 << " " << t2 << endl;
    		if(level[t2].size()==0)
    		{
    			cout << "NA" << endl;
    			continue;
    		}
    		for (int j = 0; j < level[t2].size(); ++j)
    		{
    			cout << level[t2][j].first << " " << level[t2][j].second << endl;
    		}
    	}
    	else if(t1=="2")
    	{
    		string t2;
    		cin >> t2;
    		cout << "Case " << i << ": " << t1 << " " << t2 << endl;
    		if(num[t2]==0)
    		{
    			cout << "NA" << endl;
    			continue;
    		}
    		cout << num[t2] << " " << site[t2] << endl;
    	}
    	else if(t1=="3")
    	{
    		string t2;
    		cin >> t2;
    		cout << "Case " << i << ": " << t1 << " " << t2 << endl;
    		unordered_map<string, int> temp = date[t2];
    		vector<pair<string, int> > t;
    		unordered_map<string, int>::iterator iter;
    		for(iter=temp.begin(); iter!=temp.end(); iter++)
    		{
    			t.push_back(make_pair(iter->first, iter->second));
    		}
    		if(t.size()==0)
    		{
    			cout << "NA" << endl;
    		}
    		sort(t.begin(), t.end(), cmp);
    		for (int j = 0; j < t.size(); ++j)
    		{
    			cout << t[j].first << " " << t[j].second << endl;
    		}
    	}
    	else
    	{
    		string t2;
    		cin >> t2;
    		cout << "Case " << i << ": " << t1 << " " << t2 << endl;
    		string t = "ss";
    		cout << t << endl;
    	}
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40438165/article/details/90382473