CF780D Innokenty and a Football League

1 CF780D Innokenty and a Football League

2 题目描述

时间限制 \(2s\) | 空间限制 \(256M\)

Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.

Each club's full name consist of two words: the team's name and the hometown's name, for example, "DINAMO BYTECITY". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that:

the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN",
or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "DIB".
Apart from this, there is a rule that if for some club x the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club x. For example, if the above mentioned club has short name "DIB", then no club for which the first option is chosen can have short name equal to "DIN". However, it is possible that some club have short name "DIN", where "DI" are the first two letters of the team's name, and "N" is the first letter of hometown's name. Of course, no two teams can have the same short name.

Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen.

数据范围:\(1 ≤ n ≤ 1000\)

3 题解

容易想到,如果对于当前球队,第一种起名方式已经存在一个球队了,那么我们当前球队就必须取第二种起名方式。此时,我们应当让其他第一种起名方式相同的球队也取第二种起名方式,所以我们先遍历一遍,用 \(Map\) 记录下当前第一种起名方式的每一种缩写有几个球队。如果超过一个,那么当前球队就应该取第二种起名方式。我们这里选择将第二种起名方式当成第一种起名方式存储。随后,由于可能存在某个队的第一种起名方式与当前队的第二种起名方式相同这一情况,所以我们要跑 \(n\) 次。在这 \(n\) 次后,如果仍然存在冲突的名字,那么当前肯定没有可行的构造方案,否则输出我们找到的名字即可。

4 代码(空格警告):

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int n;
string s, t, x, y;
map<string, int> M;
vector<pair<string, string> > v;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> s >> t;
        x = s.substr(0, 3);
        y = s.substr(0, 2) + t.substr(0, 1);
        v.push_back(make_pair(x, y));
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j < n; j++) M[v[j].first]++;
        for (int j = 0; j < n; j++) if (M[v[j].first] > 1) v[j].first = v[j].second;
        M.clear();
    }
    for (int i = 0; i < n; i++) M[v[i].first]++;
    for (int i = 0; i < n; i++)
    {
        if (M[v[i].first] > 1)
        {
            cout << "NO";
            return 0;
        }
    }
    cout << "YES" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << v[i].first << endl;
    }
    return 0;
}

欢迎关注我的公众号:智子笔记

posted @ 2021-04-02 21:39  David24  阅读(56)  评论(0编辑  收藏  举报